Astarte device API for C++ 0.8.1
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 <string_view>
18#include <variant>
19#include <vector>
20
22
24
26template <typename T>
27concept AstarteDataAllowedType = requires {
28 requires std::is_same_v<T, int32_t> || std::is_same_v<T, int64_t> || std::is_same_v<T, double> ||
29 std::is_same_v<T, bool> || std::is_same_v<T, std::string> ||
30 std::is_same_v<T, std::string_view> || std::is_same_v<T, std::vector<uint8_t>> ||
31 std::is_same_v<T, std::chrono::system_clock::time_point> ||
32 std::is_same_v<T, std::vector<int32_t>> || std::is_same_v<T, std::vector<int64_t>> ||
33 std::is_same_v<T, std::vector<double>> || std::is_same_v<T, std::vector<bool>> ||
34 std::is_same_v<T, std::vector<std::string>> ||
35 std::is_same_v<T, std::vector<std::vector<uint8_t>>> ||
36 std::is_same_v<T, std::vector<std::chrono::system_clock::time_point>>;
37};
38
41 public:
50 template <AstarteDataAllowedType T>
51 explicit AstarteData(T value) {
52 if constexpr (std::is_same_v<T, std::string_view>) {
53 data_ = std::string(value);
54 } else {
55 data_ = value;
56 }
57 }
58
63 template <AstarteDataAllowedType T>
64 [[nodiscard]] auto into() const
65 -> std::conditional_t<std::is_same_v<T, std::string_view>, std::string_view, const T&> {
66 if constexpr (std::is_same_v<T, std::string_view>) {
67 return std::string_view(std::get<std::string>(data_));
68 } else {
69 return std::get<T>(data_);
70 }
71 }
72
76 template <AstarteDataAllowedType T>
77 [[nodiscard]] auto try_into() const -> std::optional<T> {
78 if constexpr (std::is_same_v<T, std::string_view>) {
79 if (std::holds_alternative<std::string>(data_)) {
80 return std::string_view(std::get<std::string>(data_));
81 }
82 } else {
83 if (std::holds_alternative<T>(data_)) {
84 return std::get<T>(data_);
85 }
86 }
87
88 return std::nullopt;
89 }
90
94 [[nodiscard]] auto get_type() const -> AstarteType;
100 [[nodiscard]] auto get_raw_data() const
101 -> const std::variant<int32_t, int64_t, double, bool, std::string, std::vector<uint8_t>,
102 std::chrono::system_clock::time_point, std::vector<int32_t>,
103 std::vector<int64_t>, std::vector<double>, std::vector<bool>,
104 std::vector<std::string>, std::vector<std::vector<uint8_t>>,
105 std::vector<std::chrono::system_clock::time_point>>&;
111 [[nodiscard]] auto operator==(const AstarteData& other) const -> bool;
117 [[nodiscard]] auto operator!=(const AstarteData& other) const -> bool;
118
119 private:
120 std::variant<int32_t, int64_t, double, bool, std::string, std::vector<uint8_t>,
121 std::chrono::system_clock::time_point, std::vector<int32_t>, std::vector<int64_t>,
122 std::vector<double>, std::vector<bool>, std::vector<std::string>,
123 std::vector<std::vector<uint8_t>>,
124 std::vector<std::chrono::system_clock::time_point>>
125 data_;
126};
127
128} // namespace AstarteDeviceSdk
129
130#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:77
auto into() const -> std::conditional_t< std::is_same_v< T, std::string_view >, std::string_view, const T & >
Convert the Astarte data class to the appropriate data type.
Definition data.hpp:64
AstarteData(T value)
Constructor for the AstarteData class.
Definition data.hpp:51
Restricts the allowed types for instances of an Astarte data class.
Definition data.hpp:27
Umbrella namespace for the Astarte device SDK.
Definition data.hpp:23
AstarteType
Possible Astarte types.
Definition type.hpp:18
Types definitions for communication with Astarte.