Skip to content

4.6 dataContainer reference

David Romero Antequera edited this page Aug 25, 2016 · 1 revision

The dataContainer class is used across the simplified sensor and analytics interface as a convenience container to store information as <key, value> pairs. Also, the units could be specified as well. We will refer as data item to the combination of key, value and unit. This class is somewhat "derived" from the STL map class.

The following public methods are available for the dataContainer class:

template<typename T> void put(const std::string& key, const T &value, const std::string &units)

Insert a value into the container. key is a label string for identifying the data item and it must be unique in the container. value is the metric to be stored, see the list of supported data types at the end. units is a string for specifying units of the metric.

template<typename T> T getValue(const std::string& key)

Returns the value for a given key. The value is casted into the template specified type. A unableToFindKey exception is thrown if there is no item with the specified key.

template<typename T> T getValue(const dataContainer::iterator& it)

Returns the value for the iterator it (see the Iterators section below). The value is casted into the template specified type.

void erase(const std::string& key)

Removes the data item specified by key. A unableToFindKey exception is thrown if there is no item with the specified key.

void erase(const dataContainer::iterator& it)

Removes the data item pointed by the iterator it (see the Iterators section below).

std::string& getUnits(const std::string& key)

Returns the units for the data item specified by key. A unableToFindKey exception is thrown if there is no item with the specified key.

std::string& getUnits(const iterator& it)

Returns the units of the data item referred by it (see the Iterators section below).

std::string& getDataTypeName(const std::string& key)

Returns the descriptor string for the data item specified by key. A unableToFindKey exception is thrown if there is no item with the specified key.

std::string& getDataTypeName(const iterator& it)

Returns the descriptor string for the data item referred by it (see the Iterators section below).

size_t getDataSize(const std::string& key)

Returns the data size for the data item specified by key. A unableToFindKey exception is thrown if there is no item with the specified key.

template<typename T> inline bool matchType(const std::string& key)

Returns true if the data item specified by key stored type match the one of the template. A unableToFindKey exception is thrown if there is no item with the specified key.

template<typename T> bool matchType(const iterator& it)

Returns true if the stored type of the data item referred by it matches the one of the template.

size_t count()

Returns the number of data items in the dataContainer.

dataContainer::iterator begin()

Returns an iterator referring to the first item of the dataContainer (see the Iterators section below).

dataContainer::iterator end()

Returns an iterator referring to the past-the-end item of the dataContainer (see the Iterators section below).

std::string& getKey(const iterator& it)

Returns the key of the data item referred by it (see the Iterators section below).

bool containsKey(const std::string& key)

Returns true if there is an item in the container specified by key. This function should be used to check for unexisting keys.

void* getRawDataPtr(const iterator& it)

Returns a pointer to the raw data of the data item referred by it (see the Iterators section below).

Supported data types

  • bool
  • Signed integer types: int8_t, int16_t, int32_t, int64_t
  • Unsigned integer types: uint8_t, uint16_t, uint32_t, uint64_t
  • Floating-point types: float, double
  • struct timeval
  • string

dataContainer::iterator

The dataContainer iterator is a wrapper for the underlying STL map container:

typedef std::map<std::string, dataHolder>::iterator iterator;

An iterator is an object referring to a data item that is part of the dataContainer class. For referring to the next element, the incremental operator can be used, e.g. ++it. Assuming cnt as a dataContainer pointer, is possible to loop through all of the data items as follows:

for (dataContainer::iterator it = cnt->begin(); it != cnt->end(); ++it)
Clone this wiki locally