new initializer list ctor

This commit is contained in:
Robin Müller 2020-05-08 14:38:10 +02:00
parent 6dc05e4951
commit fadebe2eb4
2 changed files with 62 additions and 35 deletions

View File

@ -2,9 +2,22 @@
#include <framework/serviceinterface/ServiceInterfaceStream.h> #include <framework/serviceinterface/ServiceInterfaceStream.h>
template <typename T> template <typename T>
PoolEntry<T>::PoolEntry( T* initValue, uint8_t set_length, uint8_t set_valid ) : length(set_length), valid(set_valid) { PoolEntry<T>::PoolEntry(std::initializer_list<T> initValue, uint8_t set_length,
uint8_t set_valid ) : length(set_length), valid(set_valid) {
this->address = new T[this->length]; this->address = new T[this->length];
if (initValue != NULL) { if(initValue.size() == 0) {
memset(this->address, 0, this->getByteSize());
}
else {
memcpy(this->address, initValue.begin(), this->getByteSize());
}
}
template <typename T>
PoolEntry<T>::PoolEntry( T* initValue, uint8_t set_length, uint8_t set_valid ) :
length(set_length), valid(set_valid) {
this->address = new T[this->length];
if (initValue != nullptr) {
memcpy(this->address, initValue, this->getByteSize() ); memcpy(this->address, initValue, this->getByteSize() );
} else { } else {
memset(this->address, 0, this->getByteSize() ); memset(this->address, 0, this->getByteSize() );
@ -57,6 +70,7 @@ Type PoolEntry<T>::getType() {
return PodTypeConversion<T>::type; return PodTypeConversion<T>::type;
} }
template class PoolEntry<bool>;
template class PoolEntry<uint8_t>; template class PoolEntry<uint8_t>;
template class PoolEntry<uint16_t>; template class PoolEntry<uint16_t>;
template class PoolEntry<uint32_t>; template class PoolEntry<uint32_t>;

View File

@ -1,81 +1,94 @@
#ifndef POOLENTRY_H_ #ifndef POOLENTRY_H_
#define POOLENTRY_H_ #define POOLENTRY_H_
#include <framework/datapool/PoolEntryIF.h> #include <framework/datapool/PoolEntryIF.h>
#include <stddef.h> #include <stddef.h>
#include <cstring> #include <cstring>
#include <initializer_list>
/** /**
* \brief This is a small helper class that defines a single data pool entry. * @brief This is a small helper class that defines a single data pool entry.
* @details
* The helper is used to store all information together with the data as a
* single data pool entry.The content's type is defined by the template argument.
* It is prepared for use with plain old data types, but may be extended to
* complex types if necessary. It can be initialized with a certain value,
* size and validity flag. It holds a pointer to the real data and offers
* methods to access this data and to acquire additional information
* (such as validity and array/byte size). It is NOT intended to be used
* outside the DataPool class.
* *
* \details The helper is used to store all information together with the data as a single data pool entry. * @ingroup data_pool
* The content's type is defined by the template argument.
* It is prepared for use with plain old data types,
* but may be extended to complex types if necessary.
* It can be initialized with a certain value, size and validity flag.
* It holds a pointer to the real data and offers methods to access this data and to acquire
* additional information (such as validity and array/byte size).
* It is NOT intended to be used outside the DataPool class.
*
* \ingroup data_pool
* *
*/ */
template <typename T> template <typename T>
class PoolEntry : public PoolEntryIF { class PoolEntry : public PoolEntryIF {
public: public:
/** /**
* \brief In the classe's constructor, space is allocated on the heap and * @brief In the classe's constructor, space is allocated on the heap and
* potential init values are copied to that space. * potential init values are copied to that space.
* \param initValue A pointer to the single value or array that holds the init value. * @param initValue Initializer list with values to initialize with
* With the default value (NULL), the entry is initalized with all 0. * @param set_length Defines the array length of this entry.
* \param set_length Defines the array length of this entry. * @param set_valid Sets the initialization flag.
* \param set_valid Sets the initialization flag. It is invalid (0) by default. * It is invalid (0) by default.
*/ */
PoolEntry( T* initValue = NULL, uint8_t set_length = 1, uint8_t set_valid = 0 ); PoolEntry(std::initializer_list<T> initValue = {}, uint8_t set_length = 1,
uint8_t set_valid = 0);
/** /**
* \brief The allocated memory for the variable is freed in the destructor. * @brief In the classe's constructor, space is allocated on the heap and
* \details As the data pool is global, this dtor is only called on program exit. * potential init values are copied to that space.
* @param initValue A pointer to the single value or array that holds
* the init value. With the default value (nullptr), the entry is
* initalized with all 0.
* @param set_length Defines the array length of this entry.
* @param set_valid Sets the initialization flag. It is invalid (0) by default.
*/
PoolEntry(T* initValue = nullptr, uint8_t set_length = 1, uint8_t set_valid = 0);
/**
* @brief The allocated memory for the variable is freed in the destructor.
* @details As the data pool is global, this dtor is only called on program exit.
* PoolEntries shall never be copied, as a copy might delete the variable on the heap. * PoolEntries shall never be copied, as a copy might delete the variable on the heap.
*/ */
~PoolEntry(); ~PoolEntry();
/** /**
* \brief This is the address pointing to the allocated memory. * @brief This is the address pointing to the allocated memory.
*/ */
T* address; T* address;
/** /**
* \brief This attribute stores the length information. * @brief This attribute stores the length information.
*/ */
uint8_t length; uint8_t length;
/** /**
* \brief Here, the validity information for a variable is stored. * @brief Here, the validity information for a variable is stored.
* Every entry (single variable or vector) has one valid flag. * Every entry (single variable or vector) has one valid flag.
*/ */
uint8_t valid; uint8_t valid;
/** /**
* \brief getSize returns the array size of the entry. * @brief getSize returns the array size of the entry.
* \details A single parameter has size 1. * @details A single parameter has size 1.
*/ */
uint8_t getSize(); uint8_t getSize();
/** /**
* \brief This operation returns the size in bytes. * @brief This operation returns the size in bytes.
* \details The size is calculated by sizeof(type) * array_size. * @details The size is calculated by sizeof(type) * array_size.
*/ */
uint16_t getByteSize(); uint16_t getByteSize();
/** /**
* \brief This operation returns a the address pointer casted to void*. * @brief This operation returns a the address pointer casted to void*.
*/ */
void* getRawData(); void* getRawData();
/** /**
* \brief This method allows to set the valid information of the pool entry. * @brief This method allows to set the valid information of the pool entry.
*/ */
void setValid( uint8_t isValid ); void setValid( uint8_t isValid );
/** /**
* \brief This method allows to get the valid information of the pool entry. * @brief This method allows to get the valid information of the pool entry.
*/ */
uint8_t getValid(); uint8_t getValid();
/** /**
* \brief This is a debug method that prints all values and the valid information to the screen. * @brief This is a debug method that prints all values and the valid
* It prints all array entries in a row. * information to the screen. It prints all array entries in a row.
*/ */
void print(); void print();