Astarte device API for C++ 0.7.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 <variant>
18#include <vector>
19
21
23
25template <typename T>
26concept AstarteDataAllowedType = requires {
27 requires std::is_same_v<T, int32_t> || std::is_same_v<T, int64_t> || std::is_same_v<T, double> ||
28 std::is_same_v<T, bool> || std::is_same_v<T, std::string> ||
29 std::is_same_v<T, std::vector<uint8_t>> ||
30 std::is_same_v<T, std::chrono::system_clock::time_point> ||
31 std::is_same_v<T, std::vector<int32_t>> || std::is_same_v<T, std::vector<int64_t>> ||
32 std::is_same_v<T, std::vector<double>> || std::is_same_v<T, std::vector<bool>> ||
33 std::is_same_v<T, std::vector<std::string>> ||
34 std::is_same_v<T, std::vector<std::vector<uint8_t>>> ||
35 std::is_same_v<T, std::vector<std::chrono::system_clock::time_point>>;
36};
37
40 public:
45 template <AstarteDataAllowedType T>
46 explicit AstarteData(T value) : data_(value) {}
47
52 template <AstarteDataAllowedType T>
53 auto into() const -> const T& {
54 return std::get<T>(data_);
55 }
56
60 template <AstarteDataAllowedType T>
61 auto try_into() const -> std::optional<T> {
62 if (std::holds_alternative<T>(data_)) {
63 return std::get<T>(data_);
64 }
65
66 return std::nullopt;
67 }
68
72 [[nodiscard]] auto get_type() const -> AstarteType;
78 [[nodiscard]] auto get_raw_data() const
79 -> const std::variant<int32_t, int64_t, double, bool, std::string, std::vector<uint8_t>,
80 std::chrono::system_clock::time_point, std::vector<int32_t>,
81 std::vector<int64_t>, std::vector<double>, std::vector<bool>,
82 std::vector<std::string>, std::vector<std::vector<uint8_t>>,
83 std::vector<std::chrono::system_clock::time_point>>&;
89 [[nodiscard]] auto operator==(const AstarteData& other) const -> bool;
95 [[nodiscard]] auto operator!=(const AstarteData& other) const -> bool;
96
97 private:
98 std::variant<int32_t, int64_t, double, bool, std::string, std::vector<uint8_t>,
99 std::chrono::system_clock::time_point, std::vector<int32_t>, std::vector<int64_t>,
100 std::vector<double>, std::vector<bool>, std::vector<std::string>,
101 std::vector<std::vector<uint8_t>>,
102 std::vector<std::chrono::system_clock::time_point>>
103 data_;
104};
105
106} // namespace AstarteDeviceSdk
107
108#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:61
auto into() const -> const T &
Convert the Astarte data class to the appropriate data type.
Definition data.hpp:53
AstarteData(T value)
Constructor for the AstarteData class.
Definition data.hpp:46
Restricts the allowed types for instances of an Astarte data class.
Definition data.hpp:26
Umbrella namespace for the Astarte device SDK.
Definition data.hpp:22
AstarteType
Possible Astarte types.
Definition type.hpp:18
Types definitions for communication with Astarte.