cumbia 1.x
general purpose multi threaded library
Loading...
Searching...
No Matches
CuData Class Reference

container for data exchange for the cumbia library and its clients More...

#include <cudata.h>

Public Member Functions

virtual ~CuData ()
 the class destructor More...
 
 CuData ()
 constructor of an empty CuData More...
 
 CuData (const CuVariant &v)
 constructor of a CuData initialised with one value (key: emtpy string) More...
 
 CuData (const std::string &key, const CuVariant &v)
 constructor initialising a CuData with one key-value pair More...
 
 CuData (const CuData &other)
 the copy constructor More...
 
 CuData (CuData &&other)
 c++11 move constructor More...
 
virtual CuDataoperator= (const CuData &other)
 assignment operator, copies data from another source More...
 
virtual CuDataoperator= (CuData &&other)
 move assignment operator, moves contents from another source More...
 
CuDataset (const std::string &key, const CuVariant &value)
 
CuDatamerge (const CuData &other)
 
CuDatamerge (const CuData &&other)
 
CuData clone () const
 clone this data, without sharing More...
 
CuDataremove (const std::string &key)
 
CuData remove (const std::string &key) const
 
CuDataremove (const std::vector< std::string > &keys)
 
CuData remove (const std::vector< std::string > &keys) const
 
size_t size () const
 returns the number of key-value pairs stored More...
 
CuVariant value () const
 return the value associated to the empty key More...
 
CuVariant value (const std::string &key) const
 return a copy of the value associated with the given key, or an invalid and null CuVariant More...
 
void add (const std::string &key, const CuVariant &value)
 insert the key/value into the bundle More...
 
bool containsKey (const std::string &key) const
 returns true if the bundle contains the given key More...
 
bool has (const std::string &key, const std::string &value) const
 returns true if the specified key has the given string value More...
 
CuVariantoperator[] (const std::string &key)
 array subscript write operator; pushes a new key-value pair into the bundle More...
 
const CuVariantoperator[] (const std::string &key) const
 array subscript read operator: get a reference to a value given the key More...
 
virtual bool operator== (const CuData &other) const
 equality relational operator. Returns true if this CuData equals another More...
 
virtual bool operator!= (const CuData &other) const
 inequality relational operator. Returns true if the equality operator CuData::operator == returns false More...
 
bool isEmpty () const
 returns true if there are no key-value pairs stored More...
 
void print () const
 prints the representation of the object provided by toString using printf More...
 
std::string toString () const
 write a representation of this object into a std::string More...
 
void putTimestamp ()
 put date and time information into this object More...
 
std::vector< std::string > keys () const
 
std::string s (const std::string &key) const
 
double d (const std::string &key) const
 
int i (const std::string &key) const
 
unsigned int u (const std::string &key) const
 
bool b (const std::string &key) const
 
double D (const std::string &key) const
 
int I (const std::string &key) const
 
unsigned int U (const std::string &key) const
 
bool B (const std::string &key) const
 
std::vector< double > DV (const std::string &key) const
 
std::vector< int > IV (const std::string &key) const
 
std::vector< long long > LLV (const std::string &key) const
 
std::vector< unsigned int > UV (const std::string &key) const
 
std::vector< unsigned long int > ULV (const std::string &key) const
 
std::vector< unsigned long long int > ULLV (const std::string &key) const
 
std::vector< bool > BV (const std::string &key) const
 

Detailed Description

container for data exchange for the cumbia library and its clients

CuData is a bundle that stores key-value pairs. The key is a std::string, the value is of type CuVariant. A copy of the value associated to a key can be obtained with CuData::value(const std::string& key). A value can be accessed with the [] operator. [] operators are defined in the const and non const flavours. The non const version allows to insert new CuVariant values with a string key within the square brackets.

The parameterless CuData::value version returns the value associated to an empty key, if there is one, created for example with the CuData::CuData(const CuVariant &v) constructor.

The number of key-value pairs stored by CuData can be queried with CuData::size. CuData::isEmpty returns true if the CuData has no key/value pair. CuData::containsKey tells whether a given key is or not in the bundle. CuData::add can be used to add a key-value pair. The result is equivalent to using the [] operator.

Two CuData can be compared with the == and != operators. The == operator returns true if the two bundles contain the same keys and each of their values correspond. The operator != returns the opposite value of the operator ==

The CuData::toString method returns a std::string representation of its contents. The contents of a CuData can be printed on the console by means of CuData::print

The CuVariant variant type
Please read the CuVariant documentation to understand the cumbia CuVariant type.
Implicit sharing
Since
1.4.0

Data is implicitly shared, enabling copy on write. Atomic reference counters make the class reentrant, meaning that methods can be called from multiple threads, but only if each invocation uses its own data. Atomic reference counting is used to ensure the integrity of the shared data. Since cumbia 1.4.0, CuData uses implicit sharing as well.

Constructor & Destructor Documentation

◆ ~CuData()

CuData::~CuData ( )
virtual

the class destructor

CuData class destructor

◆ CuData() [1/5]

CuData::CuData ( )

constructor of an empty CuData

builds a CuData with no key/value

Referenced by remove().

◆ CuData() [2/5]

CuData::CuData ( const CuVariant v)

constructor of a CuData initialised with one value (key: emtpy string)

Parameters
va CuVariant used to initialise a key/value pair where the key is an empty string.

The parameter-less of CuData::value can be used to get the value associated to an empty key

◆ CuData() [3/5]

CuData::CuData ( const std::string &  key,
const CuVariant v 
)

constructor initialising a CuData with one key-value pair

Parameters
keya string used as a key
vthe value, as CuVariant

The new CuData will be initialised with a key/v pair

◆ CuData() [4/5]

CuData::CuData ( const CuData other)

the copy constructor

Parameters
otheranother CuData used as the source of the copy

copies all the contents from the other CuData into the new object

◆ CuData() [5/5]

CuData::CuData ( CuData &&  other)

c++11 move constructor

Parameters
otheranother CuData used as the source of the move operation

Contents of other are moved into this CuData

Member Function Documentation

◆ add()

void CuData::add ( const std::string &  key,
const CuVariant value 
)

insert the key/value into the bundle

Parameters
keythe new key, as a std::string
valuethe new value bound to key
Note
The effect is exactly the same as using the operator [] (const std::string& key)

References value().

Referenced by putTimestamp().

◆ b()

bool CuData::b ( const std::string &  key) const

References containsKey(), CuVariant::d(), and operator[]().

Referenced by B().

◆ B()

bool CuData::B ( const std::string &  key) const

◆ BV()

std::vector< bool > CuData::BV ( const std::string &  key) const

◆ clone()

CuData CuData::clone ( ) const

clone this data, without sharing

Returns
a clone of this data, bypassing the copy-on-write share

References d().

◆ containsKey()

bool CuData::containsKey ( const std::string &  key) const

returns true if the bundle contains the given key

Parameters
keythe key to be searched, std::string
Returns
true if the bundle contains the given key, false otherwise

Referenced by b(), B(), BV(), D(), DV(), I(), IV(), LLV(), CuDataSerializer::serialize(), U(), ULLV(), ULV(), and UV().

◆ d()

double CuData::d ( const std::string &  key) const

References CuVariant::d(), and operator[]().

Referenced by clone().

◆ D()

double CuData::D ( const std::string &  key) const

◆ DV()

std::vector< double > CuData::DV ( const std::string &  key) const

◆ has()

bool CuData::has ( const std::string &  key,
const std::string &  value 
) const

returns true if the specified key has the given string value

This method is a shortcut to test if to a given key is associated a string with the given value.

Parameters
keythe key
valuethe value as string
Returns
true if data[key].toString() == value false otherwise

References value().

◆ i()

int CuData::i ( const std::string &  key) const

References CuVariant::i(), and operator[]().

◆ I()

int CuData::I ( const std::string &  key) const

◆ isEmpty()

bool CuData::isEmpty ( ) const

returns true if there are no key-value pairs stored

Returns
true there is no data stored, false if there is at least one key-value pair
See also
size

◆ IV()

std::vector< int > CuData::IV ( const std::string &  key) const

◆ keys()

std::vector< std::string > CuData::keys ( ) const

Referenced by merge(), and remove().

◆ LLV()

std::vector< long long > CuData::LLV ( const std::string &  key) const

◆ merge() [1/2]

CuData & CuData::merge ( const CuData &&  other)

◆ merge() [2/2]

CuData & CuData::merge ( const CuData other)

References keys(), and value().

◆ operator!=()

bool CuData::operator!= ( const CuData other) const
virtual

inequality relational operator. Returns true if the equality operator CuData::operator == returns false

Returns
true if the equality operator CuData::operator== returns false, false otherwise

References operator==().

◆ operator=() [1/2]

CuData & CuData::operator= ( const CuData other)
virtual

assignment operator, copies data from another source

Parameters
otheranother CuData which values will be copied into this

◆ operator=() [2/2]

CuData & CuData::operator= ( CuData &&  other)
virtual

move assignment operator, moves contents from another source

Parameters
otheranother CuData which values will be moved into this

◆ operator==()

bool CuData::operator== ( const CuData other) const
virtual

equality relational operator. Returns true if this CuData equals another

Parameters
otheranother CuData (const reference)
Returns
true if other contains the same keys as this and every key of other has the same value as the corresponding key in this

the inequality operator is also defined

Referenced by operator!=().

◆ operator[]() [1/2]

CuVariant & CuData::operator[] ( const std::string &  key)

array subscript write operator; pushes a new key-value pair into the bundle

Use the insert operator to add a new key/value pair to the bundle

Example
CuData at("src", source.getName()); // string
at["device"] = source.getDeviceName(); // string
at["activity"] = "E"; // string
at["period"] = source.period(); // integer
at["err"] = false; // bool
container for data exchange for the cumbia library and its clients
Definition: cudata.h:48

Referenced by b(), B(), BV(), d(), D(), DV(), i(), I(), IV(), LLV(), s(), u(), U(), ULLV(), ULV(), and UV().

◆ operator[]() [2/2]

const CuVariant & CuData::operator[] ( const std::string &  key) const

array subscript read operator: get a reference to a value given the key

Parameters
keythe key to search for
Returns
reference to the value for the given key or a reference to an empty CuVariant if the key is not found.
Note
An empty CuVariant::isValid method returns false and CuVariant::isNull returns true

◆ print()

void CuData::print ( ) const

prints the representation of the object provided by toString using printf

Prints a string representation of the contents of this object on the console, using printf.

See also
toString

References toString().

◆ putTimestamp()

void CuData::putTimestamp ( )

put date and time information into this object

writes the following keys into this object, taking the system clock time from the gettimeofday call.

References add().

◆ remove() [1/4]

CuData & CuData::remove ( const std::string &  key)

◆ remove() [2/4]

CuData CuData::remove ( const std::string &  key) const

References CuData().

◆ remove() [3/4]

CuData & CuData::remove ( const std::vector< std::string > &  keys)

References keys().

◆ remove() [4/4]

CuData CuData::remove ( const std::vector< std::string > &  keys) const

References CuData(), and keys().

◆ s()

std::string CuData::s ( const std::string &  key) const

◆ set()

CuData & CuData::set ( const std::string &  key,
const CuVariant value 
)

References value().

Referenced by CuDataSerializer::deserialize().

◆ size()

size_t CuData::size ( ) const

returns the number of key-value pairs stored

Returns
the number of key-value pairs stored within this object
See also
isEmpty

◆ toString()

std::string CuData::toString ( ) const

write a representation of this object into a std::string

Returns
a std::string with a representation of this object.

All keys are printed with their values (CuVariant is always converted to string with CuVariant::toString). At last, the number of key/values stored and the flag returned by CuData::isEmpty are printed.

See also
print

References i.

Referenced by print().

◆ u()

unsigned int CuData::u ( const std::string &  key) const

References operator[](), and CuVariant::u().

◆ U()

unsigned int CuData::U ( const std::string &  key) const

◆ ULLV()

std::vector< unsigned long long > CuData::ULLV ( const std::string &  key) const

◆ ULV()

std::vector< unsigned long > CuData::ULV ( const std::string &  key) const

◆ UV()

std::vector< unsigned int > CuData::UV ( const std::string &  key) const

◆ value() [1/2]

CuVariant CuData::value ( ) const

return the value associated to the empty key

Returns
the value associated to an empty key, if there's one empty key with a value (for example, created with the CuData::CuData(const CuVariant &v) ) constructor, or an empty CuVariant

If there's no value associated to an empty key (i.e. an emtpy string, ""), an invalid and null CuVariant is returned. See CuVariant::isValid and CuVariant::isNull

Referenced by add(), has(), merge(), and set().

◆ value() [2/2]

CuVariant CuData::value ( const std::string &  key) const

return a copy of the value associated with the given key, or an invalid and null CuVariant

Returns
a copy of the value associated with the given key, or an invalid and null CuVariant. See CuVariant::isValid and CuVariant::isNull

See also CuData::operator [](const std::string &key) const


The documentation for this class was generated from the following files: