Astarte device API for C++ 0.6.2
Astarte device SDK for C++
Loading...
Searching...
No Matches
data.hpp
Go to the documentation of this file.
1// (C) Copyright 2025, SECO Mind Srl
2//
3// SPDX-License-Identifier: Apache-2.0
4
5#ifndef ASTARTE_DEVICE_SDK_DATA_H
6#define ASTARTE_DEVICE_SDK_DATA_H
7
12
13#include <chrono>
14#include <cstdint>
15#include <optional>
16#include <string>
17#include <variant>
18#include <vector>
19#if !(__cplusplus >= 202002L)
20#include <type_traits>
21#endif // !(__cplusplus >= 202002L)
22
24
26
27#if __cplusplus >= 202002L
29template <typename T>
30concept AstarteDataAllowedType = requires {
31 requires std::is_same_v<T, int32_t> || std::is_same_v<T, int64_t> || std::is_same_v<T, double> ||
32 std::is_same_v<T, bool> || std::is_same_v<T, std::string> ||
33 std::is_same_v<T, std::vector<uint8_t>> ||
34 std::is_same_v<T, std::chrono::system_clock::time_point> ||
35 std::is_same_v<T, std::vector<int32_t>> || std::is_same_v<T, std::vector<int64_t>> ||
36 std::is_same_v<T, std::vector<double>> || std::is_same_v<T, std::vector<bool>> ||
37 std::is_same_v<T, std::vector<std::string>> ||
38 std::is_same_v<T, std::vector<std::vector<uint8_t>>> ||
39 std::is_same_v<T, std::vector<std::chrono::system_clock::time_point>>;
40};
41#else // __cplusplus >= 202002L
42template <typename T>
43struct astarte_data_is_allowed_type : std::false_type {};
44template <>
45struct astarte_data_is_allowed_type<int32_t> : std::true_type {};
46template <>
47struct astarte_data_is_allowed_type<int64_t> : std::true_type {};
48template <>
49struct astarte_data_is_allowed_type<double> : std::true_type {};
50template <>
51struct astarte_data_is_allowed_type<bool> : std::true_type {};
52template <>
53struct astarte_data_is_allowed_type<std::string> : std::true_type {};
54template <>
55struct astarte_data_is_allowed_type<std::vector<uint8_t>> : std::true_type {};
56template <>
57struct astarte_data_is_allowed_type<std::chrono::system_clock::time_point> : std::true_type {};
58template <>
59struct astarte_data_is_allowed_type<std::vector<int32_t>> : std::true_type {};
60template <>
61struct astarte_data_is_allowed_type<std::vector<int64_t>> : std::true_type {};
62template <>
63struct astarte_data_is_allowed_type<std::vector<double>> : std::true_type {};
64template <>
65struct astarte_data_is_allowed_type<std::vector<bool>> : std::true_type {};
66template <>
67struct astarte_data_is_allowed_type<std::vector<std::string>> : std::true_type {};
68template <>
69struct astarte_data_is_allowed_type<std::vector<std::vector<uint8_t>>> : std::true_type {};
70template <>
71struct astarte_data_is_allowed_type<std::vector<std::chrono::system_clock::time_point>>
72 : std::true_type {};
73#endif // __cplusplus >= 202002L
74
77 public:
78#if __cplusplus >= 202002L
83 template <AstarteDataAllowedType T>
84 explicit AstarteData(T value) : data_(value) {}
85
90 template <AstarteDataAllowedType T>
91 auto into() const -> const T& {
92 return std::get<T>(data_);
93 }
94
98 template <AstarteDataAllowedType T>
99 auto try_into() const -> std::optional<T> {
100 if (std::holds_alternative<T>(data_)) {
101 return std::get<T>(data_);
102 }
103
104 return std::nullopt;
105 }
106#else // __cplusplus >= 202002L
111 template <typename T>
112 explicit AstarteData(
113 T value, std::enable_if_t<astarte_data_is_allowed_type<T>::value, bool> /*unused*/ = true)
114 : data_(value) {}
115
120 template <typename T>
121 auto into(std::enable_if_t<astarte_data_is_allowed_type<T>::value, bool> /*unused*/ = true) const
122 -> const T& {
123 return std::get<T>(data_);
124 }
129 template <typename T>
130 auto try_into(std::enable_if_t<astarte_data_is_allowed_type<T>::value, bool> /*unused*/ =
131 true) const -> std::optional<T> {
132 if (std::holds_alternative<T>(data_)) {
133 return std::get<T>(data_);
134 }
135
136 return std::nullopt;
137 }
138#endif // __cplusplus >= 202002L
143 [[nodiscard]] auto get_type() const -> AstarteType;
149 [[nodiscard]] auto get_raw_data() const
150 -> const std::variant<int32_t, int64_t, double, bool, std::string, std::vector<uint8_t>,
151 std::chrono::system_clock::time_point, std::vector<int32_t>,
152 std::vector<int64_t>, std::vector<double>, std::vector<bool>,
153 std::vector<std::string>, std::vector<std::vector<uint8_t>>,
154 std::vector<std::chrono::system_clock::time_point>>&;
160 [[nodiscard]] auto operator==(const AstarteData& other) const -> bool;
166 [[nodiscard]] auto operator!=(const AstarteData& other) const -> bool;
167
168 private:
169 std::variant<int32_t, int64_t, double, bool, std::string, std::vector<uint8_t>,
170 std::chrono::system_clock::time_point, std::vector<int32_t>, std::vector<int64_t>,
171 std::vector<double>, std::vector<bool>, std::vector<std::string>,
172 std::vector<std::vector<uint8_t>>,
173 std::vector<std::chrono::system_clock::time_point>>
174 data_;
175};
176
177} // namespace AstarteDeviceSdk
178
179#endif // ASTARTE_DEVICE_SDK_DATA_H
auto get_raw_data() const -> const std::variant< int32_t, int64_t, double, bool, std::string, std::vector< uint8_t >, std::chrono::system_clock::time_point, std::vector< int32_t >, std::vector< int64_t >, std::vector< double >, std::vector< bool >, std::vector< std::string >, std::vector< std::vector< uint8_t > >, std::vector< std::chrono::system_clock::time_point > > &
Return the raw data contained in this class instance.
auto get_type() const -> AstarteType
Get the type of the data contained in this class instance.
auto try_into() const -> std::optional< T >
Convert the Astarte data class to the given type if it's the correct variant.
Definition data.hpp:99
auto into() const -> const T &
Convert the Astarte data class to the appropriate data type.
Definition data.hpp:91
AstarteData(T value)
Constructor for the AstarteData class.
Definition data.hpp:84
Restricts the allowed types for instances of an Astarte data class.
Definition data.hpp:30
Umbrella namespace for the Astarte device SDK.
Definition data.hpp:25
AstarteType
Possible Astarte types.
Definition type.hpp:18
Types definitions for communication with Astarte.