Image Component Library (ICL)
Public Member Functions | Static Public Member Functions | Private Member Functions | Friends | List of all members
icl::utils::Any Struct Reference

Simple generic data type implementation that uses a string based data representation. More...

#include <Any.h>

Inheritance diagram for icl::utils::Any:

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 >
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...
 

Detailed Description

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.

std::string representation

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.

Representing Pointers

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.

encoding vectors

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.

Benchmarks:

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

Constructor & Destructor Documentation

◆ Any() [1/3]

icl::utils::Any::Any ( )
inline

Empty constructor.

◆ Any() [2/3]

template<class T >
icl::utils::Any::Any ( const T &  t)
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.

◆ Any() [3/3]

icl::utils::Any::Any ( size_t  n,
char  c 
)
inlineprivate

for direct creation of the parent string (used in the static ptr methods)

Member Function Documentation

◆ as()

template<class T >
T icl::utils::Any::as ( ) const
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.:

Any myInt(7);
int i = myInt; // Works -> implicit cast to int
int j;
j = myInt; // Error: cast is ambiguous
// instead:
j = myInt.as<int>(); // Works -> explicit cast to int

◆ operator T()

template<class T >
icl::utils::Any::operator T ( ) const
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.

◆ ptr() [1/2]

template<class T >
static T* icl::utils::Any::ptr ( const Any any)
inlinestatic

this method can be used to extract a pointer that was encoded as Any

See also
Representing Pointers

◆ ptr() [2/2]

template<class T >
static Any icl::utils::Any::ptr ( const T *  p)
inlinestatic

this method can be used to create an any that contains a binary encoded pointer

See also
Representing Pointers

Friends And Related Function Documentation

◆ operator<<

std::ostream& operator<< ( std::ostream &  s,
const Any any 
)
friend

remembers the ostream-operator<< that Any is of type std::string

◆ operator>>

std::istream& operator>> ( std::istream &  s,
Any any 
)
friend

remembers the istream-operator<< that Any is of type std::string


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