Bifrost SDK
Bifrost SDK documentation
Amino::Any Class Reference

Generic value class that allows for storage of a value of any type. More...

#include <Any.h>

Inheritance diagram for Amino::Any:

Public Member Functions

 Any () noexcept=default
 Default constructor. More...
 
 Any (Any const &other)=default
 Copy constructor. More...
 
 Any (Any &&other) noexcept=default
 Move constructor. More...
 
template<typename ValueType , typename = Traits::enable_if_valid<ValueType>>
 Any (ValueType &&v) noexcept
 Move constructor. More...
 
 ~Any ()=default
 Destructor. More...
 
Anyoperator= (Any const &rhs)=default
 Copy assignment operator. More...
 
Anyoperator= (Any &&rhs) noexcept=default
 Move assignment operator. More...
 
template<typename ValueType , typename = Traits::enable_if_valid<ValueType>>
Anyoperator= (ValueType &&rhs) &noexcept
 Move assignment conversion. More...
 
template<typename ValueType , typename... Args, typename = Traits::enable_if_valid<ValueType>, typename = Traits::enable_if_constructible<ValueType, Args...>>
void emplace (Args &&... args) noexcept
 Replaces the contained object of this Any. More...
 
template<typename ValueType , typename Up , typename... Args, typename IL = std::initializer_list<Up>, typename = Traits::enable_if_valid<ValueType>, typename = Traits::enable_if_constructible<ValueType, IL&, Args...>>
void emplace (std::initializer_list< Up > il, Args &&... args) noexcept
 Replaces the contained object of this Any. More...
 
void reset () noexcept
 Reset this Any object. More...
 
Anyswap (Any &rhs) noexcept
 Swap payloads with another Any object. More...
 
bool has_value () const noexcept
 Return true if this Any object contains a payload. More...
 
 AMINO_INTERNAL_DEPRECATED ("Use any.type() == other.type() instead.") bool has_same_type(Any const &other) const noexcept
 
TypeId type () const noexcept
 Returns the TypeId of the value in this Any, or the TypeId of void if this Any does not have a value. More...
 

Detailed Description

Generic value class that allows for storage of a value of any type.

Warning
Amino::Any is NOT a replacement for standard anys (like std::any). It is only meant to be used within Amino graphs. Therefore if a value never needs to flow directly into a graph, a standard any (like std::any) should be used instead.

This follows the interface of std::any reasonably closely with the primary difference being no exceptions being thrown – in the case of an invalid cast operation, a default value of the target type will be created and returned. (This event - invalid_cast - may be logged). This has the consequence, in contrast to std::any, that the payload type of Any must have a default constructor. An additional constraint vs std::any is the constness requirements of the payload. Details of this are discussed in the notes below.

Variables of type Any are not completely unlimited in what they can store; there are some minimal preconditions. A value stored in a variable of type Any must be copy-constructible. Therefore, it is not possible for example to store a C array, since C arrays are not copy-constructible.

Unlike std::any, the internals of this class are well defined and known to the Amino compiler, so that it can issue code to properly manipulate these (correctly apply copy, move and destroy semantics) as they are passed into, out from, and through the Amino graph.

Note
As the Amino Graph execution model does not support C++ exceptions, Any should not be used to store objects that throw exceptions when being constructed, copied, moved or deleted.
Any has stricter alignment requirements than those satisfied by default allocators for std containers (like std::vector). This is so that SSE vectors can be stored in an Any without performance penalties from misalignment.
Objects that have overridden allocators (override operator new and delete) must have those allocators public if they are to be stored in an Any.
In the Amino system, Array is intended to be managed via Ptr – to enforce this, their allocators are overridden and not part of the public interface, and as a result you will not be able to store an "unwrapped" Array in Any – you will need to wrap them in an Ptr before storing in an Any

For example for an 'elem_count' array of T stored in an Any named 'anyArray', you could use a statement like this;

\ref Any anyArray(Amino::newClassPtr<Amino::Array<T>>(elem_count));
Ptr< T > newClassPtr(Args &&... args)
Creates a Ptr holding a new T constructed from the given arguments.
Definition: Ptr.h:1913
Generic value class that allows for storage of a value of any type.
Definition: Any.h:180
Define a Amino array of elements of type T.
Definition: Array.h:105

To retrieve the Ptr to the Array stored in an Any;

auto arrayPtr = Amino::any_cast<Amino::Ptr<Amino::Array<T>>>(anyArray);
Warning
Any does not reference count its payload or attempt any optimizations like copy-on-write. When you copy an Any, the payload is copied immediately. In Amino, the means for getting copy-on-write behaviour is to use Ptr. If you want reference counted, copy-on-write behaviour, and the genericity of Any, you can store a Ptr as the payload of an Any – then you get the best of both worlds.
Note
For the purposes of RTTI, Amino::Any discards const and volatile type qualifiers on the payload type when storing/extracting payloads in Any instances.
There are five ways to extract the payload of an Any (see any_cast). They either return a value of the payload type or a pointer (const if the Any is const) to the payload of the Any.

Definition at line 180 of file Any.h.

Constructor & Destructor Documentation

◆ Any() [1/4]

Amino::Any::Any ( )
defaultnoexcept

Default constructor.

Constructs an empty Any.

Postcondition
has_value() == false

Referenced by operator=().

◆ Any() [2/4]

Amino::Any::Any ( Any const &  other)
default

Copy constructor.

Constructs an Any object as a copy of another Any. A copy of the payload of other will be contained in this Any.

Postcondition
has_value() == other.has_value()
Parameters
otherThe Any to copy construct this Any from.

◆ Any() [3/4]

Amino::Any::Any ( Any &&  other)
defaultnoexcept

Move constructor.

Constructs an Any object by moving the contents of another Any into this instance.

Postcondition
other.has_value() == false

◆ Any() [4/4]

template<typename ValueType , typename = Traits::enable_if_valid<ValueType>>
Amino::Any::Any ( ValueType &&  v)
inlineexplicitnoexcept

Move constructor.

Constructs an Any object from something that is not also an Any. This will move the object into this instance of Any as its payload.

Note
Used only if v is not an Any.
Template Parameters
ValueTypeThe payload type
Parameters
vThe value this Any will contain.

Definition at line 221 of file Any.h.

◆ ~Any()

Amino::Any::~Any ( )
default

Destructor.

Destroys the payload, if any (using reset)

Member Function Documentation

◆ AMINO_INTERNAL_DEPRECATED()

Amino::Any::AMINO_INTERNAL_DEPRECATED ( ) const &
inlinenoexcept

Definition at line 322 of file Any.h.

References type().

◆ emplace() [1/2]

template<typename ValueType , typename... Args, typename = Traits::enable_if_valid<ValueType>, typename = Traits::enable_if_constructible<ValueType, Args...>>
void Amino::Any::emplace ( Args &&...  args)
inlinenoexcept

Replaces the contained object of this Any.

Changes the contained object to one of type std::decay_t<ValueType> constructed from the given arguments Args.

First destroys the current contained object (if any) by reset(), then: constructs an object of type std::decay_t<ValueType>, direct-non-list-initialized from std::forward<Args>(args)..., as the contained object.

This overload only participates in overload resolution if ValueType is a valid payload type to be stored in an Any (see Any for details about constraints on payload types) and is constructible from the given arguments args.

Definition at line 277 of file Any.h.

◆ emplace() [2/2]

template<typename ValueType , typename Up , typename... Args, typename IL = std::initializer_list<Up>, typename = Traits::enable_if_valid<ValueType>, typename = Traits::enable_if_constructible<ValueType, IL&, Args...>>
void Amino::Any::emplace ( std::initializer_list< Up >  il,
Args &&...  args 
)
inlinenoexcept

Replaces the contained object of this Any.

Changes the contained object to one of type std::decay_t<ValueType> constructed from the given arguments Args.

First destroys the current contained object (if any) by reset(), then: constructs an object of type std::decay_t<ValueType>, direct-non-list-initialized from il, std::forward<Args>(args)..., as the contained object.

This overload only participates in overload resolution if ValueType is a valid payload type to be stored in an Any (see Any for details about constraints on payload types) and is constructible from the given initializer_list il and arguments args.

Definition at line 302 of file Any.h.

◆ has_value()

bool Amino::Any::has_value ( ) const
inlinenoexcept

Return true if this Any object contains a payload.

Definition at line 320 of file Any.h.

◆ operator=() [1/3]

Any & Amino::Any::operator= ( Any &&  rhs)
defaultnoexcept

Move assignment operator.

The payload of rhs will be stolen from rhs and assigned to this Any.

Postcondition
rhs.has_value() == false
Parameters
rhsThe Any to move assign to this Any.

◆ operator=() [2/3]

Any & Amino::Any::operator= ( Any const &  rhs)
default

Copy assignment operator.

The payload of rhs will be copied and the copy will be assigned to this Any.

◆ operator=() [3/3]

template<typename ValueType , typename = Traits::enable_if_valid<ValueType>>
Any & Amino::Any::operator= ( ValueType &&  rhs) &
inlinenoexcept

Move assignment conversion.

Note
Used only if rhs is not an Any (or anything that decays into an Any) and is a valid payload type (see Any for details about constraints on payload types).
Template Parameters
ValueTypeThe payload type
Parameters
rhsThe value to assign to this Any

Definition at line 253 of file Any.h.

References Any().

◆ reset()

void Amino::Any::reset ( )
inlinenoexcept

Reset this Any object.

Causes the contained object to be destroyed (if present).

Postcondition
has_value() == false

Definition at line 311 of file Any.h.

◆ swap()

Any & Amino::Any::swap ( Any rhs)
inlinenoexcept

Swap payloads with another Any object.

Definition at line 314 of file Any.h.

References Amino::swap().

◆ type()

TypeId Amino::Any::type ( ) const
inlinenoexcept

Returns the TypeId of the value in this Any, or the TypeId of void if this Any does not have a value.

Definition at line 329 of file Any.h.

Referenced by AMINO_INTERNAL_DEPRECATED(), and Bifrost::hasProperty().