PdCom  5.3
Process data communication client
SizeTypeInfo.h
1 /*****************************************************************************
2  *
3  * Copyright (C) 2021 Richard Hacker (lerichi at gmx dot net),
4  * Florian Pose (fp at igh dot de),
5  * Bjarne von Horn (vh at igh dot de).
6  *
7  * This file is part of the PdCom library.
8  *
9  * The PdCom library is free software: you can redistribute it and/or modify
10  * it under the terms of the GNU Lesser General Public License as published by
11  * the Free Software Foundation, either version 3 of the License, or (at your
12  * option) any later version.
13  *
14  * The PdCom library is distributed in the hope that it will be useful, but
15  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
16  * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
17  * License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public License
20  * along with the PdCom library. If not, see <http://www.gnu.org/licenses/>.
21  *
22  *****************************************************************************/
23 
24 #ifndef PDCOM5_SIZETYPEINFO_H
25 #define PDCOM5_SIZETYPEINFO_H
26 
27 #include <cstddef>
28 #include <vector>
29 
30 namespace PdCom {
31 
33 struct TypeInfo
34 {
35  enum DataType {
36  DataTypeBegin = 0,
37  boolean_T = DataTypeBegin,
38  uint8_T,
39  int8_T,
40  uint16_T,
41  int16_T,
42  uint32_T,
43  int32_T,
44  uint64_T,
45  int64_T,
46  double_T,
47  single_T,
48  char_T,
49  DataTypeEnd // keep this as the very last one!
50  };
51 
52  DataType type;
54  const char *ctype;
56  size_t element_size;
57  constexpr TypeInfo(
58  DataType type,
59  const char *ctype,
60  size_t element_size) noexcept :
61  type(type), ctype(ctype), element_size(element_size)
62  {}
63 };
64 
66 class SizeInfo : public std::vector<unsigned int>
67 {
68  public:
69  using std::vector<unsigned int>::vector;
70 
71  bool isScalar() const { return size() == 1 && (*this)[0] == 1; }
72  bool isVector() const { return size() == 1 && (*this)[0] > 1; }
73  bool is2DMatrix() const { return size() == 2; }
74  std::size_t dimension() const noexcept
75  {
76  return size() == 1 && (*this)[0] == 1 ? 0 : size();
77  }
78 
79  std::size_t totalElements() const
80  {
81  if (empty())
82  return 0;
83  std::size_t ans = 1;
84  for (auto i : *this)
85  ans *= i;
86  return ans;
87  }
88  static SizeInfo Scalar() { return {1U}; }
89  static SizeInfo RowVector(unsigned int rows) { return {rows}; }
90  static SizeInfo Matrix(unsigned int rows, unsigned int cols)
91  {
92  return {{rows, cols}};
93  }
94  static SizeInfo
95  Matrix(unsigned int layers, unsigned int rows, unsigned int cols)
96  {
97  return {{layers, rows, cols}};
98  }
99 
100  unsigned int layers() const
101  {
102  if (empty())
103  return 0;
104  return size() >= 3 ? operator[](size() - 3) : 1;
105  }
106 
107  unsigned int rows() const
108  {
109  if (empty())
110  return 0;
111  return size() >= 2 ? operator[](size() - 2) : 1;
112  }
113 
114  unsigned int columns() const { return empty() ? 0 : back(); }
115 };
116 } // namespace PdCom
117 
118 
119 #endif // PDCOM5_SIZETYPEINFO_H
const char * ctype
name of the correspondig c type (double, char, ...)
Definition: SizeTypeInfo.h:54
Size of a Variable.
Definition: SizeTypeInfo.h:66
Type of a Variable.
Definition: SizeTypeInfo.h:33
size_t element_size
Size of one element in bytes.
Definition: SizeTypeInfo.h:56
Definition: ClientStatistics.h:31