aqnwb 0.4.0
Loading...
Searching...
No Matches
Types.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <cstddef>
4#include <cstdint>
5#include <limits>
6#include <string>
7#include <string_view>
8#include <type_traits>
9#include <unordered_map>
10#include <utility>
11#include <variant>
12#include <vector>
13
14namespace AQNWB
15{
16
17// Forward declaration of Channel
18class Channel;
19
23namespace Types
24{
29{
32};
33
41{
42 return (lhs == Success && rhs == Success) ? Success : Failure;
43}
44
52{
53 return (lhs == Success || rhs == Success) ? Success : Failure;
54}
55
66
73{
74 switch (type) {
75 case Group:
76 return "Group";
77 case Dataset:
78 return "Dataset";
79 case Attribute:
80 return "Attribute";
81 case Undefined:
82 return "Undefined";
83 default:
84 return "Unknown";
85 }
86}
87
95template<StorageObjectType T>
97 : std::integral_constant<bool, (T == Dataset || T == Attribute)>
98{
99};
100
104using SizeType = size_t;
105
109inline constexpr SizeType SizeTypeNotSet =
110 (std::numeric_limits<SizeType>::max)();
111
115using SizeArray = std::vector<SizeType>;
116
120using ChannelVector = std::vector<Channel>;
121
125using ScalarDataVariant = std::variant<std::monostate,
126 uint8_t,
127 uint16_t,
128 uint32_t,
129 uint64_t,
130 int8_t,
131 int16_t,
132 int32_t,
133 int64_t,
134 float,
135 double,
136 std::string>;
137
141using VectorDataVariant = std::variant<std::monostate,
142 std::vector<uint8_t>,
143 std::vector<uint16_t>,
144 std::vector<uint32_t>,
145 std::vector<uint64_t>,
146 std::vector<int8_t>,
147 std::vector<int16_t>,
148 std::vector<int32_t>,
149 std::vector<int64_t>,
150 std::vector<float>,
151 std::vector<double>,
152 std::vector<std::string>>;
153
158{
159 std::string name;
160 std::string version;
161
168 std::vector<std::pair<std::string_view, std::string_view>>
170};
171
190{
191 std::variant<ScalarDataVariant, VectorDataVariant> value;
192
193 CellValue() = default;
194
203 template<typename T,
204 typename =
205 std::enable_if_t<std::is_constructible_v<ScalarDataVariant, T&&>
206 && !std::is_same_v<std::decay_t<T>, CellValue>>>
207 // cppcheck-suppress noExplicitConstructor
208 CellValue(T&& val)
209 : value(ScalarDataVariant(std::forward<T>(val)))
210 {
211 }
212
219 template<typename T,
220 typename = std::enable_if_t<
221 std::is_constructible_v<VectorDataVariant, T&&>>,
222 typename = void>
223 // cppcheck-suppress noExplicitConstructor
224 CellValue(T&& val)
225 : value(VectorDataVariant(std::forward<T>(val)))
226 {
227 }
228
236 // cppcheck-suppress noExplicitConstructor
237 CellValue(const char* val)
238 : value(ScalarDataVariant(std::string(val)))
239 {
240 }
241
248 template<typename T>
249 bool holds_alternative() const
250 {
251 bool result = false;
252 if constexpr (std::is_constructible_v<ScalarDataVariant, T>) {
253 if (std::holds_alternative<ScalarDataVariant>(value)) {
254 result = result
255 || std::holds_alternative<T>(std::get<ScalarDataVariant>(value));
256 }
257 }
258 if constexpr (std::is_constructible_v<VectorDataVariant, T>) {
259 if (std::holds_alternative<VectorDataVariant>(value)) {
260 result = result
261 || std::holds_alternative<T>(std::get<VectorDataVariant>(value));
262 }
263 }
264 return result;
265 }
266
275 template<typename T>
276 const T& get() const
277 {
278 if constexpr (std::is_constructible_v<VectorDataVariant, T>) {
279 return std::get<T>(std::get<VectorDataVariant>(value));
280 } else {
281 return std::get<T>(std::get<ScalarDataVariant>(value));
282 }
283 }
284
301 template<typename T,
302 typename = std::enable_if_t<
303 std::is_constructible_v<ScalarDataVariant, T>
304 || std::is_constructible_v<VectorDataVariant, T>>>
305 operator T() const
306 {
307 return get<T>();
308 }
309
314 std::string toString() const
315 {
316 return std::visit(
317 [](auto&& arg) -> std::string
318 {
319 using T = std::decay_t<decltype(arg)>;
320 if constexpr (std::is_same_v<T, ScalarDataVariant>) {
321 return std::visit(
322 [](auto&& scalarArg) -> std::string
323 {
324 using ScalarT = std::decay_t<decltype(scalarArg)>;
325 if constexpr (std::is_same_v<ScalarT, std::monostate>) {
326 return "null";
327 } else if constexpr (std::is_same_v<ScalarT, std::string>) {
328 return scalarArg;
329 } else {
330 return std::to_string(scalarArg);
331 }
332 },
333 arg);
334 } else if constexpr (std::is_same_v<T, VectorDataVariant>) {
335 return std::visit(
336 [](auto&& vectorArg) -> std::string
337 {
338 using VectorT = std::decay_t<decltype(vectorArg)>;
339 if constexpr (std::is_same_v<VectorT, std::monostate>) {
340 return "[]";
341 } else {
342 std::string res = "[";
343 for (size_t i = 0; i < vectorArg.size(); ++i) {
344 if constexpr (std::is_same_v<VectorT,
345 std::vector<std::string>>) {
346 res += vectorArg[i];
347 } else {
348 res += std::to_string(vectorArg[i]);
349 }
350 if (i < vectorArg.size() - 1) {
351 res += ", ";
352 }
353 }
354 res += "]";
355 return res;
356 }
357 },
358 arg);
359 } else {
360 return "Unknown";
361 }
362 },
363 value);
364 }
365};
366
372using RowData = std::unordered_map<std::string, CellValue>;
373
374} // namespace Types
375} // namespace AQNWB
Class for storing acquisition system channel information.
Definition Channel.hpp:16
Provides definitions for various types used in the project.
Definition Types.hpp:24
std::vector< Channel > ChannelVector
Alias for a vector of channels.
Definition Types.hpp:120
std::string storageObjectTypeToString(StorageObjectType type)
Convert StorageObjectType enum value to string.
Definition Types.hpp:72
constexpr SizeType SizeTypeNotSet
Value to use to indicate that a SizeType index is not set.
Definition Types.hpp:109
std::variant< std::monostate, std::vector< uint8_t >, std::vector< uint16_t >, std::vector< uint32_t >, std::vector< uint64_t >, std::vector< int8_t >, std::vector< int16_t >, std::vector< int32_t >, std::vector< int64_t >, std::vector< float >, std::vector< double >, std::vector< std::string > > VectorDataVariant
Variant data type for representing any 1D vector of scalar values.
Definition Types.hpp:141
std::unordered_map< std::string, CellValue > RowData
Represents a row of data in a DynamicTable.
Definition Types.hpp:372
Status operator&&(Status lhs, Status rhs)
Overloaded && operator for Status enum.
Definition Types.hpp:40
StorageObjectType
Types of object used in the NWB schema.
Definition Types.hpp:60
@ Attribute
Definition Types.hpp:63
@ Group
Definition Types.hpp:61
@ Dataset
Definition Types.hpp:62
@ Undefined
Definition Types.hpp:64
Status operator||(Status lhs, Status rhs)
Overloaded || operator for Status enum.
Definition Types.hpp:51
Status
Represents the status of an operation.
Definition Types.hpp:29
@ Success
Definition Types.hpp:30
@ Failure
Definition Types.hpp:31
std::variant< std::monostate, uint8_t, uint16_t, uint32_t, uint64_t, int8_t, int16_t, int32_t, int64_t, float, double, std::string > ScalarDataVariant
Variant data type for representing a single scalar value.
Definition Types.hpp:125
std::vector< SizeType > SizeArray
Alias for an array of size types used in the project.
Definition Types.hpp:115
size_t SizeType
Alias for the size type used in the project.
Definition Types.hpp:104
The main namespace for AqNWB.
Definition Channel.hpp:11
std::variant< ScalarDataVariant, VectorDataVariant > value
Definition Types.hpp:191
CellValue(const char *val)
Implicit constructor for string literals.
Definition Types.hpp:237
std::string toString() const
Convert the cell value to a string representation.
Definition Types.hpp:314
const T & get() const
Helper method to extract the underlying value.
Definition Types.hpp:276
CellValue(T &&val)
Implicit constructor for vector values.
Definition Types.hpp:224
CellValue(T &&val)
Implicit constructor for scalar values.
Definition Types.hpp:208
bool holds_alternative() const
Checks if the CellValue holds a specific type.
Definition Types.hpp:249
Helper struct to check if a value is a data field, i.e., Dataset or Attribute.
Definition Types.hpp:98
Struct to hold namespace information.
Definition Types.hpp:158
std::string name
The name of the namespace.
Definition Types.hpp:159
std::string version
The version of the namespace.
Definition Types.hpp:160
std::vector< std::pair< std::string_view, std::string_view > > specVariables
The specVariables of the namespace.
Definition Types.hpp:169