PdCom  5.0
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  boolean_T = 0,
37  uint8_T,
38  int8_T,
39  uint16_T,
40  int16_T,
41  uint32_T,
42  int32_T,
43  uint64_T,
44  int64_T,
45  double_T,
46  single_T,
47  DataTypeEnd // keep this as the very last one!
48  };
49 
50  DataType type;
51  const char *ctype;
52  size_t element_size;
53  constexpr TypeInfo(
54  DataType type,
55  const char *ctype,
56  size_t element_size) noexcept :
57  type(type), ctype(ctype), element_size(element_size)
58  {}
59 };
60 
62 class SizeInfo
63 {
64  using UnderlyingType = std::vector<unsigned int>;
65  UnderlyingType size_info;
66 
67  SizeInfo(UnderlyingType size_info) : size_info(std::move(size_info)) {}
68 
69  public:
70  SizeInfo() : size_info(1, 1U) {}
71  bool isScalar() const
72  {
73  return size_info.size() == 1 and size_info[0] == 1;
74  }
75  bool isVector() const { return size_info.size() == 1 and size_info[0] > 1; }
76  std::size_t dimension() const noexcept { return size_info.size(); }
77  unsigned int operator[](std::size_t index) const
78  {
79  return size_info[index];
80  }
81  typename UnderlyingType::const_iterator begin() const
82  {
83  return size_info.begin();
84  }
85 
86  typename UnderlyingType::const_iterator end() const
87  {
88  return size_info.end();
89  }
90  std::size_t totalElements() const
91  {
92  std::size_t ans = 1;
93  for (auto i : size_info)
94  ans *= i;
95  return ans;
96  }
97  static SizeInfo RowVector(unsigned int rows) { return {{1, rows}}; }
98  static SizeInfo Matrix(unsigned int rows, unsigned int cols)
99  {
100  UnderlyingType u {2, 0};
101  u[0] = rows;
102  u[1] = cols;
103  return {u};
104  }
105 };
106 } // namespace PdCom
107 
108 
109 #endif // PDCOM5_SIZETYPEINFO_H
Size of a Variable.
Definition: SizeTypeInfo.h:62
Type of a Variable.
Definition: SizeTypeInfo.h:33
library version string as "major.minor.patch"
Definition: details.h:37