libpromeki 1.0.0-alpha
PROfessional MEdia toolKIt
 
Loading...
Searching...
No Matches
mdnstxtrecord.h
Go to the documentation of this file.
1
8#pragma once
9
10
11#include <promeki/config.h>
12#if PROMEKI_ENABLE_MDNS
13#include <cstdint>
14#include <promeki/namespace.h>
15#include <promeki/datatype.h>
16#include <promeki/string.h>
17#include <promeki/stringlist.h>
18#include <promeki/list.h>
19#include <promeki/buffer.h>
20#include <promeki/result.h>
21#include <promeki/error.h>
22#include <promeki/function.h>
23
24PROMEKI_NAMESPACE_BEGIN
25
26class DataStream;
27class TextStream;
28
81class MdnsTxtRecord {
82 public:
83 PROMEKI_DATATYPE(MdnsTxtRecord, DataTypeMdnsTxtRecord, 1)
84
85
86 Error writeToStream(DataStream &s) const;
88 template <uint32_t V> static Result<MdnsTxtRecord> readFromStream(DataStream &s);
89
97 enum class Presence : uint8_t {
98 Absent = 0,
99 KeyOnly = 1,
100 Empty = 2,
101 Present = 3,
102 };
103
105 static constexpr int MaxEntryBytes = 255;
106
108 MdnsTxtRecord() = default;
109
118 void set(const String &key, const String &value);
119
128 void setKey(const String &key);
129
138 void setEmpty(const String &key);
139
145 void remove(const String &key);
146
148 void clear() { _entries.clear(); }
149
151 Presence presence(const String &key) const;
152
154 bool contains(const String &key) const { return presence(key) != Presence::Absent; }
155
164 String value(const String &key, const String &defaultValue = String()) const;
165
167 bool isEmpty() const { return _entries.isEmpty(); }
168
170 int count() const { return static_cast<int>(_entries.size()); }
171
173 StringList keys() const;
174
182 void forEach(Function<void(const String &key, Presence p, const String &value)> func) const;
183
201 Buffer encode() const;
202
214 static Result<MdnsTxtRecord> decode(const Buffer &b);
215
217 static Result<MdnsTxtRecord> decode(const uint8_t *bytes, size_t len);
218
220 bool operator==(const MdnsTxtRecord &other) const;
221 bool operator!=(const MdnsTxtRecord &other) const { return !(*this == other); }
222
223 private:
224 struct Entry {
225 using List = ::promeki::List<Entry>;
226 String key;
227 Presence presence = Presence::Absent;
228 String value;
229 };
230
231 // Returns the index of the entry whose key matches @p key
232 // case-insensitively, or -1 if no entry exists.
233 int findEntry(const String &key) const;
234
235 // Returns the on-the-wire byte string for a single
236 // entry, truncated at @ref MaxEntryBytes. Defined as
237 // a private static method so it can reach the private
238 // @ref Entry layout without an anonymous-namespace
239 // friend.
240 static String entryToWire(const Entry &e);
241
242 Entry::List _entries;
243};
244
246TextStream &operator<<(TextStream &stream, const MdnsTxtRecord &txt);
247
248PROMEKI_NAMESPACE_END
249
250#endif // PROMEKI_ENABLE_MDNS