Astarte device API for C++ 0.8.1
Astarte device SDK for C++
Loading...
Searching...
No Matches
msg.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_MSG_H
6#define ASTARTE_DEVICE_SDK_MSG_H
7
15
16#include <optional>
17#include <ostream>
18#include <string>
19#include <string_view>
20#include <utility>
21#include <variant>
22
27
28namespace astarte::device {
29
37class Message {
38 public:
47 template <typename T>
48 Message(std::string_view interface, std::string_view path, T data)
49 : interface_(interface), path_(path), data_(std::move(data)) {}
50
56 [[nodiscard]] auto get_interface() const -> const std::string&;
57
63 [[nodiscard]] auto get_path() const -> const std::string&;
64
70 [[nodiscard]] auto is_datastream() const -> bool;
71
77 [[nodiscard]] auto is_individual() const -> bool;
78
85 template <typename T>
86 [[nodiscard]] auto into() const -> const T& {
87 return std::get<T>(data_);
88 }
89
96 template <typename T>
97 [[nodiscard]] auto try_into() const -> std::optional<T> {
98 if (std::holds_alternative<T>(data_)) {
99 return std::get<T>(data_);
100 }
101
102 return std::nullopt;
103 }
104
110 [[nodiscard]] auto get_raw_data() const
112
119 [[nodiscard]] auto operator==(const Message& other) const -> bool;
120
127 [[nodiscard]] auto operator!=(const Message& other) const -> bool;
128
129 private:
130 std::string interface_;
131 std::string path_;
133};
134
135} // namespace astarte::device
136
138template <>
146 template <typename ParseContext>
147 constexpr auto parse(ParseContext& ctx) const {
148 return ctx.begin();
149 }
150
158 template <typename FormatContext>
159 auto format(const astarte::device::Message& msg, FormatContext& ctx) const {
160 auto out = ctx.out();
161
162 out = astarte_fmt::format_to(out, "{{interface: {}, path: {}", msg.get_interface(),
163 msg.get_path());
164
165 // check if the payload is an unset property, which is the only "empty" case
166 bool is_unset_prop = false;
167 const auto* prop = std::get_if<astarte::device::PropertyIndividual>(&msg.get_raw_data());
168 if (prop && !prop->get_value().has_value()) {
169 is_unset_prop = true;
170 }
171
172 if (!is_unset_prop) {
173 out = astarte_fmt::format_to(out, ", value: ");
174 std::visit([&out](const auto& arg) { out = astarte_fmt::format_to(out, "{}", arg); },
175 msg.get_raw_data());
176 }
177
178 return astarte_fmt::format_to(out, "}}");
179 }
180};
181
189inline auto operator<<(std::ostream& out, const astarte::device::Message& msg) -> std::ostream& {
190 out << astarte_fmt::format("{}", msg);
191 return out;
192}
193
194#endif // ASTARTE_DEVICE_SDK_MSG_H
Represents the Astarte individual datastream data.
Definition individual.hpp:29
Astarte object class, representing the Astarte object datastream data.
Definition object.hpp:33
Astarte message class, represents a full message for/from Astarte.
Definition msg.hpp:37
auto is_individual() const -> bool
Checks if this message contains individual data.
auto get_interface() const -> const std::string &
Gets the interface of the message.
auto into() const -> const T &
Get the content of the message.
Definition msg.hpp:86
auto try_into() const -> std::optional< T >
Return the content of the message if it's of the correct type.
Definition msg.hpp:97
auto get_raw_data() const -> const std::variant< DatastreamIndividual, DatastreamObject, PropertyIndividual > &
Returns the raw data contained in this class instance.
auto is_datastream() const -> bool
Checks if this message contains a datastream.
auto get_path() const -> const std::string &
Gets the path of the message.
Message(std::string_view interface, std::string_view path, T data)
Constructor for the Message class.
Definition msg.hpp:48
Representing the Astarte individual property data.
Definition property.hpp:22
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.
Astarte individual datastream class and its related methods.
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
Astarte object class and its related methods.
Astarte individual property class and its related methods.
constexpr auto parse(ParseContext &ctx) const
Parses the format string.
Definition msg.hpp:147
auto format(const astarte::device::Message &msg, FormatContext &ctx) const
Formats the Message object.
Definition msg.hpp:159
Base formatter struct declaration for Doxygen.
Definition formatter.hpp:48