esphome-api-client 0.5.0
Modern C++17 client for the ESPHome native API (plaintext + Noise)
Loading...
Searching...
No Matches
proto_message.hpp
Go to the documentation of this file.
1#pragma once
2
11
13
14#include <cstddef>
15#include <cstdint>
16#include <cstring>
17#include <memory>
18#include <string>
19
20namespace esphome::api {
21
24public:
25 ProtoMessage() = default;
26 ProtoMessage(const ProtoMessage&) = default;
30 virtual ~ProtoMessage() = default;
31
33 virtual void encode(ProtoWriter& writer) const = 0;
34
37 virtual bool decode(ProtoReader& reader) = 0;
38
40 [[nodiscard]] virtual std::size_t calculate_size() const = 0;
41
44 [[nodiscard]] virtual std::uint32_t message_id() const = 0;
45
47 [[nodiscard]] virtual const char* message_name() const = 0;
48
50 [[nodiscard]] virtual std::unique_ptr<ProtoMessage> clone() const = 0;
51
52 // --- protobuf-compatible wrappers (non-virtual) ------------------------
53
55 [[nodiscard]] std::size_t ByteSizeLong() const {
56 return calculate_size();
57 }
58
61 bool SerializeToArray(void* data, const int size) const {
62 if (size < 0)
63 return false;
64 std::string buf;
65 buf.reserve(static_cast<std::size_t>(size));
66 ProtoWriter writer(buf);
67 encode(writer);
68 if (buf.size() != static_cast<std::size_t>(size))
69 return false;
70 if (!buf.empty())
71 std::memcpy(data, buf.data(), buf.size());
72 return true;
73 }
74
76 [[nodiscard]] std::string SerializeAsString() const {
77 std::string buf;
78 buf.reserve(calculate_size());
79 ProtoWriter writer(buf);
80 encode(writer);
81 return buf;
82 }
83
85 bool ParseFromArray(const void* data, const int size) {
86 if (size < 0)
87 return false;
88 ProtoReader reader(data, static_cast<std::size_t>(size));
89 return decode(reader);
90 }
91};
92
97public:
98 [[nodiscard]] virtual std::uint32_t key() const = 0;
99 [[nodiscard]] virtual const std::string& name() const = 0;
100 [[nodiscard]] virtual const std::string& object_id() const = 0;
101};
102
107public:
108 [[nodiscard]] virtual std::uint32_t key() const = 0;
109};
110
111} // namespace esphome::api
Intermediate base for ListEntities*Response messages (proto (base_class) = "InfoResponseProtoMessage"...
Definition proto_message.hpp:96
virtual const std::string & name() const =0
virtual const std::string & object_id() const =0
virtual std::uint32_t key() const =0
Base class every generated message derives from.
Definition proto_message.hpp:23
virtual bool decode(ProtoReader &reader)=0
Decode wire bytes into this message.
std::string SerializeAsString() const
Serialize to a freshly allocated string.
Definition proto_message.hpp:76
virtual std::unique_ptr< ProtoMessage > clone() const =0
A deep copy of this message as a new owning pointer.
bool SerializeToArray(void *data, const int size) const
Serialize into a caller-provided buffer of exactly size bytes.
Definition proto_message.hpp:61
ProtoMessage(const ProtoMessage &)=default
bool ParseFromArray(const void *data, const int size)
Parse wire bytes into this message. Returns false on malformed input.
Definition proto_message.hpp:85
ProtoMessage & operator=(const ProtoMessage &)=default
virtual std::uint32_t message_id() const =0
The protocol message id (the proto (id) option), or 0 if the message carries none.
virtual void encode(ProtoWriter &writer) const =0
Serialize this message's fields to writer (ascending field number).
ProtoMessage & operator=(ProtoMessage &&)=default
std::size_t ByteSizeLong() const
Alias for calculate_size() — matches protobuf's API.
Definition proto_message.hpp:55
ProtoMessage(ProtoMessage &&)=default
virtual ~ProtoMessage()=default
virtual std::size_t calculate_size() const =0
Exact number of bytes encode() will write.
virtual const char * message_name() const =0
The proto message name (e.g. "ListEntitiesLightResponse").
Definition wire.hpp:120
Definition wire.hpp:69
Intermediate base for *StateResponse messages (proto (base_class) = "StateResponseProtoMessage").
Definition proto_message.hpp:106
virtual std::uint32_t key() const =0
Definition bytes.hpp:10
Self-contained proto3 wire-format primitives (varint / zigzag / fixed32 / fixed64 / length-delimited)...