Flyby SDK v1.0.2
Loading...
Searching...
No Matches
message.h
Go to the documentation of this file.
1
7
8#pragma once
9
10#include <memory>
11#include <string>
12
13namespace flyby {
21 public:
22 virtual ~SerialObject() = default;
23
29 [[nodiscard]] virtual std::string serialize() const = 0;
30 };
31
38 class MessageObject : public SerialObject, public std::enable_shared_from_this<MessageObject> {
39 public:
40 ~MessageObject() override = default;
41
47 static std::shared_ptr<MessageObject> create();
48
56 static std::shared_ptr<MessageObject> from(const std::string& obj);
57
66 virtual std::shared_ptr<MessageObject> set_field(const std::string& key, const std::string& value) = 0;
67
76 virtual std::shared_ptr<MessageObject> set_field(const std::string& key, long value) = 0;
77
83 virtual std::shared_ptr<MessageObject> set_field(const std::string& key, double value) = 0;
84
90 virtual std::shared_ptr<MessageObject> set_field(
91 const std::string& key,
92 std::shared_ptr<MessageObject> value
93 ) = 0;
94
100 virtual bool is_string(const std::string& key) = 0;
101
107 virtual bool is_long(const std::string& key) = 0;
108
114 virtual bool is_double(const std::string& key) = 0;
115
121 virtual bool is_object(const std::string& key) = 0;
122
130 virtual std::string get_string(const std::string& key) const = 0;
131
139 virtual long get_long(const std::string& key) const = 0;
140
148 virtual double get_double(const std::string& key) const = 0;
149
157 virtual std::shared_ptr<MessageObject> get_object(const std::string& key) const = 0;
158 };
159
167 class Message : public SerialObject {
168 public:
173 enum MessageType : std::uint8_t {
174 TEXT = 0,
175 OBJECT,
176 };
177
178 Message() = default;
179
186 explicit Message(const std::string& msg, const std::string& source = std::string());
187
194 explicit Message(const std::shared_ptr<MessageObject>& msg, const std::string& source = std::string());
195
201 void set_version(const std::string& version);
202
208 void set_source(const std::string& source);
209
216
222 void set_payload(const std::string& payload);
223
229 [[nodiscard]] std::string get_version() const;
230
236 [[nodiscard]] std::string get_source() const;
237
243 [[nodiscard]] MessageType get_type() const;
244
250 [[nodiscard]] std::string get_payload() const;
251
257 [[nodiscard]] bool is_object() const;
258
264 [[nodiscard]] std::string serialize() const override;
265
266 private:
267 std::string m_version;
268 std::string m_source;
269 MessageType m_type;
270 std::string m_payload;
271 };
272} // namespace flyby
A key-value object that is serializable.
Definition message.h:38
virtual std::shared_ptr< MessageObject > set_field(const std::string &key, std::shared_ptr< MessageObject > value)=0
Set an object field.
virtual bool is_long(const std::string &key)=0
Check if a field is a long.
virtual std::shared_ptr< MessageObject > set_field(const std::string &key, const std::string &value)=0
Set a string field.
virtual bool is_double(const std::string &key)=0
Check if a field is a double.
virtual long get_long(const std::string &key) const =0
Get the long value stored with key.
static std::shared_ptr< MessageObject > from(const std::string &obj)
Creates a new message object from a serialized object string.
static std::shared_ptr< MessageObject > create()
Creates a new message object.
virtual double get_double(const std::string &key) const =0
Get the double value stored with key.
virtual bool is_string(const std::string &key)=0
Check if a field is a string.
virtual std::string get_string(const std::string &key) const =0
Get the string value stored with key.
virtual std::shared_ptr< MessageObject > set_field(const std::string &key, double value)=0
Set a double field.
virtual std::shared_ptr< MessageObject > set_field(const std::string &key, long value)=0
Set a long field.
virtual bool is_object(const std::string &key)=0
Check if a field is an object.
virtual std::shared_ptr< MessageObject > get_object(const std::string &key) const =0
Get the object value stored with key.
A message that is sent across the message bus.
Definition message.h:167
std::string get_version() const
Get the version of the message.
MessageType
The types that messages can hold.
Definition message.h:173
void set_source(const std::string &source)
Set the source of the message.
std::string get_payload() const
Get the payload of the message.
void set_payload(const std::string &payload)
Set the payload of the message.
Message(const std::shared_ptr< MessageObject > &msg, const std::string &source=std::string())
Message constructor for object messages.
MessageType get_type() const
Get the type of the message.
void set_type(MessageType type)
Set the type of the message.
std::string get_source() const
Get the source of the message.
std::string serialize() const override
Serialize the message.
void set_version(const std::string &version)
Set the version of the message protocol.
bool is_object() const
Check if the message is an object type.
Message(const std::string &msg, const std::string &source=std::string())
Message constructor for text messages.
An object that is serializable.
Definition message.h:20
virtual std::string serialize() const =0
Serialize the object.