Astarte device API for C++ 0.8.1
Astarte device SDK for C++
Loading...
Searching...
No Matches
object.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_OBJECT_H
6#define ASTARTE_DEVICE_SDK_OBJECT_H
7
15
16#include <initializer_list>
17#include <ostream>
18#include <string>
19#include <unordered_map>
20
23
24namespace astarte::device {
25
34 public:
36 using MapType = std::unordered_map<std::string, Data>;
38 using iterator = MapType::iterator;
40 using const_iterator = MapType::const_iterator;
42 using size_type = MapType::size_type;
44 using value_type = MapType::value_type;
45
48
54 DatastreamObject(std::initializer_list<value_type> init);
55
64 auto at(const std::string& key) -> Data&;
65
74 auto at(const std::string& key) const -> const Data&;
75
83 auto begin() -> iterator;
84
92 auto begin() const -> const_iterator;
93
101 auto end() -> iterator;
102
110 auto end() const -> const_iterator;
111
119 auto size() const -> size_type;
120
128 auto empty() const -> bool;
129
138 void insert(const std::string& key, const Data& data);
139
148 auto erase(const std::string& key) -> size_type;
149
155 void clear();
156
165 auto find(const std::string& key) -> iterator;
166
175 auto find(const std::string& key) const -> const_iterator;
176
182 auto get_raw_data() const -> const MapType&;
183
190 [[nodiscard]] auto operator==(const DatastreamObject& other) const -> bool;
191
198 [[nodiscard]] auto operator!=(const DatastreamObject& other) const -> bool;
199
200 private:
201 MapType data_;
202};
203
204} // namespace astarte::device
205
207template <>
215 template <typename ParseContext>
216 constexpr auto parse(ParseContext& ctx) const {
217 return ctx.begin();
218 }
219
227 template <typename FormatContext>
228 auto format(const astarte::device::DatastreamObject& data, FormatContext& ctx) const {
229 auto out = ctx.out();
230 out = astarte_fmt::format_to(out, "{{");
231
232 bool first = true;
233 for (const auto& pair : data.get_raw_data()) {
234 if (!first) {
235 out = astarte_fmt::format_to(out, ", ");
236 }
237 out = astarte_fmt::format_to(out, R"("{}": {})", pair.first, pair.second);
238 first = false;
239 }
240
241 out = astarte_fmt::format_to(out, "}}");
242 return out;
243 }
244};
245
253inline auto operator<<(std::ostream& out, const astarte::device::DatastreamObject& data)
254 -> std::ostream& {
255 out << astarte_fmt::format("{}", data);
256 return out;
257}
258
259#endif // ASTARTE_DEVICE_SDK_OBJECT_H
Represents a single Astarte data value.
Definition data.hpp:61
Astarte object class, representing the Astarte object datastream data.
Definition object.hpp:33
MapType::iterator iterator
Helper type for the iterator over the map of paths and Astarte datas.
Definition object.hpp:38
auto begin() -> iterator
Returns an iterator to the beginning of the map.
MapType::size_type size_type
Helper type for size type of the map of paths and Astarte datas.
Definition object.hpp:42
std::unordered_map< std::string, Data > MapType
Helper type for the map of paths and Astarte datas.
Definition object.hpp:36
auto end() -> iterator
Returns an iterator to the end of the map.
auto erase(const std::string &key) -> size_type
Erases elements from the map.
void clear()
Clears the contents of the map.
MapType::const_iterator const_iterator
Helper type for the const iterator over the map of paths and Astarte datas.
Definition object.hpp:40
DatastreamObject(std::initializer_list< value_type > init)
Constructs a DatastreamObject with initial content.
void insert(const std::string &key, const Data &data)
Inserts elements into the map.
auto find(const std::string &key) -> iterator
Finds an element with a specific key.
auto empty() const -> bool
Checks if the map is empty.
auto at(const std::string &key) -> Data &
Accesses the specified element with bounds checking.
DatastreamObject()
Constructs an empty DatastreamObject.
auto begin() const -> const_iterator
Returns an iterator to the beginning of the map.
auto get_raw_data() const -> const MapType &
Returns the raw data contained in this class instance.
auto size() const -> size_type
Returns the number of elements in the map.
auto at(const std::string &key) const -> const Data &
Accesses the specified element with bounds checking.
MapType::value_type value_type
Helper type for value type of the map of paths and Astarte datas.
Definition object.hpp:44
Astarte data class and its related methods.
auto operator<<(std::ostream &out, const astarte::device::Data &data) -> std::ostream &
Stream insertion operator for Data.
Definition data.hpp:243
Data formatting utilities for Astarte types.
Umbrella namespace for the Astarte device library.
Definition data.hpp:29
Namespace alias for the formatting library (std or fmt).
Definition formatter.hpp:45
Global namespace for all Astarte related functionality.
Definition data.hpp:29
auto format(const astarte::device::DatastreamObject &data, FormatContext &ctx) const
Formats the DatastreamObject object as a key-value map.
Definition object.hpp:228
constexpr auto parse(ParseContext &ctx) const
Parses the format string.
Definition object.hpp:216
Base formatter struct declaration for Doxygen.
Definition formatter.hpp:48