#include <any.hpp>
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) | |
| bool | empty () const |
| Returns true if the object does not contain any stored data. | |
| const std::type_info & | type () const |
| Returns the type information of the stored data. | |
| 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. | |
| template<typename ValueType > | |
| ValueType & | as () |
| Extracts a reference to the contents of the any as a type of ValueType. | |
| template<typename ValueType > | |
| const ValueType & | as () const |
| Extracts a constant reference to the contents of the any as a type of ValueType. | |
| any & | swap (any &rhs) |
| Exchanges the contents of two any's. | |
| template<typename ValueType > | |
| any & | operator= (const ValueType &rhs) |
| any & | operator= (const any &rhs) |
| std::ostream & | print (std::ostream &out) const |
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:
On deserialization:
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 172 of file any.hpp.
1.7.1