Astarte device API for C++ 0.8.1
Astarte device SDK for C++
Loading...
Searching...
No Matches
type.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_TYPE_H
6#define ASTARTE_DEVICE_SDK_TYPE_H
7
16
17#include <cstdint>
18#include <ostream>
19#include <string>
20#include <string_view>
21
24
25namespace astarte::device {
26
63
73// NOLINTNEXTLINE(readability-function-size)
74inline auto astarte_type_from_str(const std::string& type) -> astarte_tl::expected<Type, Error> {
75 if (type == "binaryblob") {
76 return Type::kBinaryBlob;
77 }
78 if (type == "boolean") {
79 return Type::kBoolean;
80 }
81 if (type == "datetime") {
82 return Type::kDatetime;
83 }
84 if (type == "double") {
85 return Type::kDouble;
86 }
87 if (type == "integer") {
88 return Type::kInteger;
89 }
90 if (type == "longinteger") {
91 return Type::kLongInteger;
92 }
93 if (type == "string") {
94 return Type::kString;
95 }
96 if (type == "binaryblobarray") {
98 }
99 if (type == "booleanarray") {
100 return Type::kBooleanArray;
101 }
102 if (type == "datetimearray") {
104 }
105 if (type == "doublearray") {
106 return Type::kDoubleArray;
107 }
108 if (type == "integerarray") {
109 return Type::kIntegerArray;
110 }
111 if (type == "longintegerarray") {
113 }
114 if (type == "stringarray") {
115 return Type::kStringArray;
116 }
117 return astarte_tl::unexpected(InvalidAstarteTypeError("data type not valid: " + type));
118}
119
120} // namespace astarte::device
121
123template <>
124struct astarte_fmt::formatter<astarte::device::Type> {
130 template <typename ParseContext>
131 constexpr auto parse(ParseContext& ctx) const {
132 return ctx.begin();
133 }
134
141 template <typename FormatContext>
142 auto format(const astarte::device::Type& typ, FormatContext& ctx) const {
143 std::string_view name = "Unknown Type";
144
145 switch (typ) {
147 name = "BinaryBlob";
148 break;
150 name = "Boolean";
151 break;
153 name = "Datetime";
154 break;
156 name = "Double";
157 break;
159 name = "Integer";
160 break;
162 name = "LongInteger";
163 break;
165 name = "String";
166 break;
168 name = "BinaryBlobArray";
169 break;
171 name = "BooleanArray";
172 break;
174 name = "DatetimeArray";
175 break;
177 name = "DoubleArray";
178 break;
180 name = "IntegerArray";
181 break;
183 name = "LongIntegerArray";
184 break;
186 name = "StringArray";
187 break;
188 }
189
190 return astarte_fmt::format_to(ctx.out(), "{}", name);
191 }
192};
193
201inline auto operator<<(std::ostream& out, const astarte::device::Type typ) -> std::ostream& {
202 out << astarte_fmt::format("{}", typ);
203 return out;
204}
205
206#endif // ASTARTE_DEVICE_SDK_TYPE_H
Error indicating that the provided Astarte type is incorrect.
Definition errors.hpp:425
Error types and handling for the Astarte device library.
Data formatting utilities for Astarte types.
Umbrella namespace for the Astarte device library.
Definition data.hpp:29
auto astarte_type_from_str(const std::string &type) -> astarte_tl::expected< Type, Error >
Converts a string representation of a type to a Type enum.
Definition type.hpp:74
Type
Enumeration of supported Astarte data types.
Definition type.hpp:33
@ kIntegerArray
Integer array Astarte type.
Definition type.hpp:57
@ kBoolean
Boolean Astarte type.
Definition type.hpp:37
@ kBooleanArray
Boolean array Astarte type.
Definition type.hpp:51
@ kLongIntegerArray
Long integer array Astarte type.
Definition type.hpp:59
@ kString
String Astarte type.
Definition type.hpp:47
@ kStringArray
String array Astarte type.
Definition type.hpp:61
@ kDatetime
Date-time Astarte type.
Definition type.hpp:39
@ kDoubleArray
Double array Astarte type.
Definition type.hpp:55
@ kInteger
Integer Astarte type.
Definition type.hpp:43
@ kLongInteger
Long integer Astarte type.
Definition type.hpp:45
@ kDouble
Double Astarte type.
Definition type.hpp:41
@ kBinaryBlob
Binary blob Astarte type.
Definition type.hpp:35
@ kDatetimeArray
Datetime array Astarte type.
Definition type.hpp:53
@ kBinaryBlobArray
Binary blob array Astarte type.
Definition type.hpp:49
Global namespace for all Astarte related functionality.
Definition data.hpp:29
auto format(const astarte::device::Type &typ, FormatContext &ctx) const
Formats the Type enum to its string representation.
Definition type.hpp:142
constexpr auto parse(ParseContext &ctx) const
Parses the format string. Default implementation.
Definition type.hpp:131
Base formatter struct declaration for Doxygen.
Definition formatter.hpp:48
auto operator<<(std::ostream &out, const astarte::device::Type typ) -> std::ostream &
Stream insertion operator for Type.
Definition type.hpp:201