GraphLab: Distributed Graph-Parallel API  2.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
graphlab::any Class Reference

#include <graphlab/util/generics/any.hpp>

List of all members.

Classes

class  holder

Public Types

typedef iholder *(* deserialize_function_type )(iarchive_soft_fail &arc)
typedef std::map< uint64_t,
deserialize_function_type
registry_map_type

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
anyswap (any &rhs)
 Exchanges the contents of two any's.
template<typename ValueType >
anyoperator= (const ValueType &rhs)
anyoperator= (const any &rhs)
std::ostream & print (std::ostream &out) const
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 ()
template<typename ValueType >
static boost::disable_if_c
< boost::is_output_streamable
< ValueType >::value, void >
::type 
print_type_or_contents (std::ostream &out, const ValueType &h)
template<typename ValueType >
static boost::enable_if_c
< boost::is_output_streamable
< ValueType >::value, void >
::type 
print_type_or_contents (std::ostream &out, const ValueType &h)

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 (graphlab::oarchive& oarc) const {
oarc << i;
}
void load (graphlab::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");
graphlab::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");
graphlab::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");
graphlab::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 139 of file any.hpp.


Member Typedef Documentation

typedef iholder*(* graphlab::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 268 of file any.hpp.


Member Function Documentation

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

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

Definition at line 179 of file any.hpp.

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

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

Definition at line 188 of file any.hpp.

any::registry_map_type & graphlab::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

Define the static registry for any

Definition at line 32 of file any.cpp.

template<typename ValueType >
any& graphlab::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 205 of file any.hpp.

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

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

Definition at line 216 of file any.hpp.


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