|
| 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.
|
|
any & | swap (any &rhs) |
| Exchanges the contents of two any's.
|
|
template<typename ValueType > |
any & | operator= (const ValueType &rhs) |
|
any & | operator= (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.
|
|
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;
oarc << i;
}
iarc << i;
}
}
If an any object contains the struct, it will be serializable.
UserStruct us;
us.i = 10;
std::ofstream fout("test.bin");
oarc << a;
To deserialize, I will open an input archive and stream into an any.
std::ifstream fin("test.bin");
iarc >> a;
Now, unusually, the above code will fail, while the following code will succeed
std::ifstream fin("test.bin");
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.