Content  


Examples

The main function in ViennaData is the access() function. From a user perspective, it returns a reference to the data identified by a key and stored for an arbitrary object. Two template arguments have to be specified: The first argument denotes the key type, while the second argument denotes the data type. Two pairs of parentheses have to be provided: The first provides the key object, the second denotes the object the data is associated with. Consider the following code:

Basic usage of ViennaData
    SomeClass obj1;
    SomeClass obj2;
 
    //
    // Store a bit of data on obj1
    //
 
    //using key of type char, data of type double
    viennadata::access<char, double>('c')(obj1) = 3.1415;
    //using key of type char, data of type std::string                   
    viennadata::access<char, std::string>('c')(obj1) = "Hello";
    //using key of type std::string, data of type char
    viennadata::access<std::string, char>("some_character")(obj1) = 'a';  
 
    //
    // Store a bit of data on obj2
    //
 
    //using key of type long, data of type long
    viennadata::access<long, long>(42)(obj2) = 360;                        
    //using key of type double, data of type std::string
    viennadata::access<double, std::string>(3.1415)(obj2) = "World";
    //using key of type std::string, data of type char       
    viennadata::access<std::string, char>("some_character")(obj2) = '!';

Two objects of arbitrary type (here SomeClass) are created and some data is associated with them. Then, at any other place in the application, one can easily access the data:

Retrieving Data and Printing it to std::cout
//output the stored data
std::cout << "--- Data for obj1 ---" << std::endl;
std::cout << "Data (type 'double') for key 'c' (type 'char'): "
          << viennadata::access<char, double>('c')(obj1) << std::endl;
std::cout << "Data (type 'std::string') for key 'c' (type 'char'): "
          << viennadata::access<char, std::string>('c')(obj1) << std::endl;
std::cout << "Data (type 'char') for key 'some_character' (type 'std::string'): "
          << viennadata::access<std::string, char>("some_character")(obj1) << std::endl;
 
std::cout << "--- Data for obj2 ---" << std::endl;
std::cout << "Data (type 'long') for key '42' (type 'long'): " 
          << viennadata::access<long, long>(42)(obj2) << std::endl;
std::cout << "Data (type 'std::string') for key '3.1415' (type 'double'): "
          << viennadata::access<double, std::string>(3.1415)(obj2) << std::endl;
std::cout << "Data (type 'char') for key 'some_character' (type 'std::string'): "
          << viennadata::access<std::string, char>("some_character")(obj2) << std::endl;

In  a similar manner, data can be moved, copied and erased. Fore more details, please consult the documentation.