Turi Create  4.0
turi::any Class Reference

#include <core/util/any.hpp>

Classes

class  holder
 

Public Types

typedef iholder *(* deserialize_function_type) (iarchive_soft_fail &arc)
 

Public Member Functions

 any ()
 default constructor. Creates an empty any
 
template<typename ValueType >
 any (const ValueType &value)
 Creates an any which stores the value.
 
 any (const any &other)
 Construct an any from another any.
 
 ~any ()
 Destroy the contentss of this any.
 
bool empty () const
 Returns true if the object does not contain any stored data.
 
template<typename ValueType >
ValueType & as ()
 
template<typename ValueType >
const ValueType & as () const
 
template<typename ValueType >
bool is () const
 Returns true if the contained type is of type ValueType.
 
anyswap (any &rhs)
 Exchanges the contents of two any's.
 
template<typename ValueType >
anyoperator= (const ValueType &rhs)
 
anyoperator= (const any &rhs)
 
const std::type_info & type () const
 Returns the type information of the stored data.
 
const std::string type_name () const
 Return the name of the internal type as a string.
 
void load (iarchive &arc)
 loads the any from a file.
 
void save (oarchive &arc) const
 Saves the any to a file. Caveats apply. See the main any docs.
 

Static Public Member Functions

static registry_map_type & get_global_registry ()
 

Detailed Description

A generic "variant" object obtained from Boost::Any and modified to be serializable. A variable of type "any" can store any datatype (even dynamically changeable at runtime), but the caveat is that you must know the exact stored type to be able to extract the data safely.

To serialize/deserialize the any, regular serialization procedures apply. However, since a statically initialized type registration system is used to identify the type of the deserialized object, so the user must pay attention to a couple of minor issues.

On serialization:

  • a) If an any contains a serializable type, the any can be serialized.
  • b) If an any contains an unserializable type, the serialization will fail at runtime.

On deserialization:

  • c) An empty any can be constructed with no type information and it can be deserialized from an archive.
  • d) However, the deserialization will fail at runtime if the true type of the any is never accessed / instantiated anywhere in the code.

Condition d) is particular unusual so I will illustrate with an example.

Given a simple user struct:

struct UserStruct {
int i;
void save (turi::oarchive& oarc) const {
oarc << i;
}
void load (turi::iarchive& iarc) {
iarc << i;
}
}

If an any object contains the struct, it will be serializable.

UserStruct us;
us.i = 10;
any a = us;
// output file
std::ofstream fout("test.bin");
turi::oarchive oarc(fout);
oarc << a; // write the any

To deserialize, I will open an input archive and stream into an any.

// open input
std::ifstream fin("test.bin");
turi::iarchive iarc(fin);
// create an any and read it
any a;
iarc >> a;

Now, unusually, the above code will fail, while the following code will succeed

// open input
std::ifstream fin("test.bin");
turi::iarchive iarc(fin);
// create an any and read it
any a;
iarc >> a;
std::cout << a.as<UserStruct>().i;

The a.as<UserStruct>() forces the instantiation of static functions which allow the any deserialization to identify the UserStruct type.

Definition at line 122 of file any.hpp.

Member Typedef Documentation

◆ deserialize_function_type

typedef iholder*(* turi::any::deserialize_function_type) (iarchive_soft_fail &arc)

This section contain the global registry used to determine the deserialization code for a particular type. Essentially the registry is a global map in which all subtypes of iholder register a deserialization function with their type.

Definition at line 258 of file any.hpp.

Member Function Documentation

◆ as() [1/2]

template<typename ValueType >
ValueType& turi::any::as ( )
inline

Extracts a reference to the contents of the any as a type of ValueType

Definition at line 162 of file any.hpp.

◆ as() [2/2]

template<typename ValueType >
const ValueType& turi::any::as ( ) const
inline

Extracts a constant reference to the contents of the any as a type of ValueType

Definition at line 171 of file any.hpp.

◆ get_global_registry()

static registry_map_type& turi::any::get_global_registry ( )
static

The get registry routine is a static method that gets a reference to the global registry. It is very important that this be a static method and not a static member to ensure that the global registry is defined before each holders try to register. This is accomplished by having get_registry statically declare the global registry

◆ operator=() [1/2]

template<typename ValueType >
any& turi::any::operator= ( const ValueType &  rhs)
inline

Update the contents of this any. If a new type is used than the type of this any will change.

Definition at line 195 of file any.hpp.

◆ operator=() [2/2]

any& turi::any::operator= ( const any rhs)
inline

Update the contents of this any to match the type of the other any.

Definition at line 206 of file any.hpp.


The documentation for this class was generated from the following file: