[2] | 1 | // %flair:license{
|
---|
[13] | 2 | // This file is part of the Flair framework distributed under the
|
---|
| 3 | // CECILL-C License, Version 1.0.
|
---|
[2] | 4 | // %flair:license}
|
---|
| 5 | /*!
|
---|
| 6 | * \file io_data.h
|
---|
| 7 | * \brief Abstract class for data types.
|
---|
| 8 | * \author Guillaume Sanahuja, Copyright Heudiasyc UMR UTC/CNRS 7253
|
---|
| 9 | * \date 2012/03/21
|
---|
| 10 | * \version 4.0
|
---|
| 11 | */
|
---|
| 12 |
|
---|
| 13 | #ifndef IO_DATA_H
|
---|
| 14 | #define IO_DATA_H
|
---|
| 15 |
|
---|
| 16 | #include <Mutex.h>
|
---|
| 17 |
|
---|
| 18 | class IODevice_impl;
|
---|
| 19 | class io_data_impl;
|
---|
| 20 |
|
---|
[13] | 21 | namespace flair {
|
---|
| 22 | namespace core {
|
---|
[2] | 23 |
|
---|
[13] | 24 | class Object;
|
---|
[2] | 25 |
|
---|
[13] | 26 | class DataType {
|
---|
| 27 | public:
|
---|
| 28 | virtual std::string GetDescription() const = 0;
|
---|
| 29 | // size in bytes
|
---|
| 30 | virtual size_t GetSize() const = 0;
|
---|
| 31 | };
|
---|
[2] | 32 |
|
---|
[13] | 33 | class DummyType : public DataType {
|
---|
| 34 | public:
|
---|
| 35 | size_t GetSize() const { return 0; }
|
---|
| 36 | std::string GetDescription() const { return "dummy"; };
|
---|
| 37 | };
|
---|
| 38 | extern DummyType dummyType;
|
---|
[2] | 39 |
|
---|
[13] | 40 | class ScalarType : public DataType {
|
---|
| 41 | public:
|
---|
| 42 | ScalarType(size_t _size) : size(_size) {}
|
---|
| 43 | size_t GetSize() const { return size; }
|
---|
| 44 | virtual std::string GetDescription() const { return "scalar"; };
|
---|
[2] | 45 |
|
---|
[13] | 46 | private:
|
---|
| 47 | size_t size;
|
---|
| 48 | };
|
---|
[2] | 49 |
|
---|
[13] | 50 | class SignedIntegerType : public ScalarType {
|
---|
| 51 | public:
|
---|
| 52 | SignedIntegerType(size_t sizeInBits) : ScalarType(sizeInBits / 8) {}
|
---|
| 53 | std::string GetDescription() const {
|
---|
| 54 | return "int" + std::to_string(GetSize() * 8) + "_t";
|
---|
| 55 | };
|
---|
| 56 | };
|
---|
| 57 | extern SignedIntegerType Int8Type;
|
---|
| 58 | extern SignedIntegerType Int16Type;
|
---|
[2] | 59 |
|
---|
[32] | 60 | class UnsignedIntegerType : public ScalarType {
|
---|
| 61 | public:
|
---|
| 62 | UnsignedIntegerType(size_t sizeInBits) : ScalarType(sizeInBits / 8) {}
|
---|
| 63 | std::string GetDescription() const {
|
---|
| 64 | return "uint" + std::to_string(GetSize() * 8) + "_t";
|
---|
| 65 | };
|
---|
| 66 | };
|
---|
| 67 | extern UnsignedIntegerType UInt8Type;
|
---|
| 68 | extern UnsignedIntegerType UInt16Type;
|
---|
| 69 |
|
---|
[13] | 70 | class FloatType : public ScalarType {
|
---|
| 71 | public:
|
---|
| 72 | FloatType() : ScalarType(4) {}
|
---|
| 73 | std::string GetDescription() const { return "float"; };
|
---|
| 74 | };
|
---|
| 75 | extern FloatType floatType;
|
---|
[2] | 76 |
|
---|
[32] | 77 | class DoubleType : public ScalarType {
|
---|
| 78 | public:
|
---|
| 79 | DoubleType() : ScalarType(8) {}
|
---|
| 80 | std::string GetDescription() const { return "double"; };
|
---|
| 81 | };
|
---|
| 82 | extern DoubleType doubleType;
|
---|
| 83 |
|
---|
[13] | 84 | /*! \class io_data
|
---|
| 85 | *
|
---|
| 86 | * \brief Abstract class for data types.
|
---|
| 87 | *
|
---|
| 88 | * Use this class to define a custom data type. Data types ares used for logging
|
---|
| 89 | *and graphs. \n
|
---|
| 90 | * The reimplemented class must call SetSize() in its constructor. \n
|
---|
| 91 | * io_data can be constructed with n samples (see io_data::io_data).
|
---|
| 92 | * In this case, old samples can be accessed throug io_data::Prev.
|
---|
| 93 | */
|
---|
| 94 | class io_data : public Mutex {
|
---|
| 95 | friend class IODevice;
|
---|
| 96 | friend class ::IODevice_impl;
|
---|
| 97 | friend class ::io_data_impl;
|
---|
[2] | 98 |
|
---|
[13] | 99 | public:
|
---|
| 100 | /*!
|
---|
| 101 | * \brief Constructor
|
---|
| 102 | *
|
---|
| 103 | * Construct an io_data. \n
|
---|
| 104 | *
|
---|
| 105 | * \param parent parent
|
---|
| 106 | * \param name name
|
---|
| 107 | * \param n number of samples
|
---|
| 108 | */
|
---|
| 109 | io_data(const Object *parent, std::string name, int n);
|
---|
[2] | 110 |
|
---|
[13] | 111 | /*!
|
---|
| 112 | * \brief Destructor
|
---|
| 113 | *
|
---|
| 114 | */
|
---|
| 115 | virtual ~io_data();
|
---|
[2] | 116 |
|
---|
[13] | 117 | /*!
|
---|
| 118 | * \brief Set data time
|
---|
| 119 | *
|
---|
| 120 | * \param time time
|
---|
| 121 | */
|
---|
| 122 | void SetDataTime(Time time);
|
---|
[2] | 123 |
|
---|
[13] | 124 | /*!
|
---|
| 125 | * \brief Data time
|
---|
| 126 | *
|
---|
| 127 | * \return data time
|
---|
| 128 | */
|
---|
| 129 | Time DataTime(void) const;
|
---|
[2] | 130 |
|
---|
[13] | 131 | /*!
|
---|
| 132 | * \brief Previous data
|
---|
| 133 | *
|
---|
| 134 | * Access previous data. io_data must have been constructed with
|
---|
| 135 | * n>1, io_data::SetPtrToCircle must have been set and
|
---|
| 136 | * io_data::prev must have been allocated.
|
---|
| 137 | *
|
---|
| 138 | * \param n previous data number
|
---|
| 139 | *
|
---|
| 140 | * \return previous data
|
---|
| 141 | */
|
---|
| 142 | const io_data *Prev(int n) const;
|
---|
[2] | 143 |
|
---|
[13] | 144 | virtual DataType const &GetDataType() const = 0;
|
---|
[2] | 145 |
|
---|
[13] | 146 | protected:
|
---|
| 147 | /*!
|
---|
| 148 | * \brief Specify the description of the reimplemented class data's
|
---|
| 149 | *
|
---|
| 150 | * This method must be called in the constructor of the reimplemented class,
|
---|
| 151 | *once by element. \n
|
---|
| 152 | * Each element description must be called in the same order as CopyDatas put
|
---|
| 153 | *the datas in the buffer. \n
|
---|
| 154 | * The description will be used for the log descriptor file.
|
---|
| 155 | *
|
---|
| 156 | * \param description description of the element
|
---|
| 157 | * \param datatype type of the element
|
---|
| 158 | */
|
---|
| 159 | void AppendLogDescription(std::string description, DataType const &datatype);
|
---|
[2] | 160 |
|
---|
[13] | 161 | /*!
|
---|
| 162 | * \brief Set the datas to circle
|
---|
| 163 | *
|
---|
| 164 | * \param ptr pointer to the data to circle
|
---|
| 165 | */
|
---|
| 166 | void SetPtrToCircle(void **ptr);
|
---|
[2] | 167 |
|
---|
[13] | 168 | /*!
|
---|
| 169 | * \brief Pointer to previous data
|
---|
| 170 | *
|
---|
| 171 | * Reimplemented class must allocate this pointer if n>1. \n
|
---|
| 172 | * Pointer must be allocated with the same kind of reimplemented class.
|
---|
| 173 | */
|
---|
| 174 | io_data *prev;
|
---|
[2] | 175 |
|
---|
[13] | 176 | private:
|
---|
| 177 | /*!
|
---|
| 178 | * \brief Copy datas
|
---|
| 179 | *
|
---|
| 180 | * This method is automatically called by IODevice::ProcessUpdate to log
|
---|
| 181 | *io_data datas. \n
|
---|
| 182 | * This method must be reimplemented, in order to copy the datas to the logs.
|
---|
| 183 | * Copied datas must be of size io_data::Size.
|
---|
| 184 | *
|
---|
| 185 | * \param dst destination buffer
|
---|
| 186 | */
|
---|
| 187 | virtual void CopyDatas(char *dst) const = 0;
|
---|
[2] | 188 |
|
---|
[13] | 189 | io_data_impl *pimpl_;
|
---|
| 190 | };
|
---|
[2] | 191 |
|
---|
| 192 | } // end namespace core
|
---|
| 193 | } // end namespace flair
|
---|
| 194 |
|
---|
| 195 | #endif // IO_DATA_H
|
---|