Image Component Library (ICL)
|
Simple generic data type implementation that uses a string based data representation. More...
#include <Any.h>
Public Member Functions | |
Any () | |
Empty constructor. More... | |
template<class T > | |
Any (const T &t) | |
real constructor with any given source type More... | |
template<class T > | |
operator T () const | |
implicit cast operator that allows for casts into any type More... | |
template<class T > | |
T | as () const |
explict cast method (if implicit cast is ambiguous) More... | |
Static Public Member Functions | |
template<class T > | |
static T * | ptr (const Any &any) |
this method can be used to extract a pointer that was encoded as Any More... | |
template<class T > | |
static Any | ptr (const T *p) |
this method can be used to create an any that contains a binary encoded pointer More... | |
Private Member Functions | |
Any (size_t n, char c) | |
for direct creation of the parent string (used in the static ptr methods) More... | |
Friends | |
std::ostream & | operator<< (std::ostream &s, const Any &any) |
remembers the ostream-operator<< that Any is of type std::string More... | |
std::istream & | operator>> (std::istream &s, Any &any) |
remembers the istream-operator<< that Any is of type std::string More... | |
Simple generic data type implementation that uses a string based data representation.
Instances of class Any can simply be created from any data type that implements the ostream-operator<< and it can be converted implicitly to any data type that implements the istream-operator>>. For cases, where C++ cannot infer the correct type an explicit conversion template-method as<T>() is also provided. Any does not allow for type checks.
Internally, an instance of type Any contains not more than the string representation of the serialized type. Since Any inherits the std::string class, the string-serialized version is also available.
The Any class can also be used to represent pointers. though the default machanism (i.e. operators >> and <<) could be overloaded to work with ascii-encoded pointers as well. The Any class provides a more efficient implementation for this. The static template functions Any::ptr can be used to create a binary encoded pointer representation.
If you want to use any instance of Any to pass a pointer, it is strongly recommended to use the binary representation instead of using ascii encoded pointers. The only drawback for the binary encoded pointers is that they cannot be used as std::string anymore.
An a very few cases, one might want to encode vector data as an Any instance This would, however entail having to concatenate the whole vector data int a string first, and later having to perform an expensive string parsing. To avoid this, the Any classe's "Constructor(T)" and "as<T>" are specialized for the basic vector types "std::vector<float>" and "std::vector<int>". These are simply encoded in a binary manner, leading to an incredible performance boost of about factor 1000.
Times were taken on an Intel 1.6GHz Core2Duo (ULV) laptop on 32Bit ubuntu. The times are given per million calls, and we did not use gcc optimizations because most operations were optimized out in this case. However the speed advatange seemed to less then about 30%.
ASCII Encoded Pointers
int p = new int[5]; Any a(int)p; int *p2 = (int)a.as<int>();
Time: ~ 1/250 ms
Binary Encoded Pointers
int *p = new int[5]; Any a = Any::ptr(p); int *p2 = Any::ptr<int>(a);
Time: ~ 1/3000 ms
Here, we also benchmarked the steps separatly and we found out, that the creation of the any instance took more than 99.9% of the time. Therefore, we can conclude that parsing a binary endocded Any instance is close the using a raw-pointer instead:
C++ Pointers:
Time: ~ 1/10000 ms
Performance measurement for vector types (Core2Duo 2.4GHz, Ubuntu 12.04 32bit), std::vector<float> V, with 1000 entries.
x = icl::utils::cat(V,","); icl::parseVecStr<float>(x); : 2.5ms Any a = v; std::vector<float> b = a; : 2.3 *10^3ms
|
inline |
Empty constructor.
|
inline |
real constructor with any given source type
This constructor can only be used for types that implement the ostream-operator<<. If this is not true, you can either overload the ostream-operator>> for instances of type T, or you can pass an already serialized (as std::string) version of T.
|
inlineprivate |
for direct creation of the parent string (used in the static ptr methods)
|
inline |
explict cast method (if implicit cast is ambiguous)
In many situations, C++ can not automatically infer the correct destination type if the implicit cast operator is implemented as a template method. E.g.:
|
inline |
implicit cast operator that allows for casts into any type
This operator does only work if the istream-operator>> is overloded for instances of type T. If this operator does not exist, you can either implement it, or parse this instance of type Any, which is also an instance of type std::string manually.
|
inlinestatic |
this method can be used to extract a pointer that was encoded as Any
|
inlinestatic |
this method can be used to create an any that contains a binary encoded pointer
|
friend |
remembers the ostream-operator<< that Any is of type std::string
|
friend |
remembers the istream-operator<< that Any is of type std::string