Enrollment 28.2.0
Loading...
Searching...
No Matches
Writer.h
Go to the documentation of this file.
1
9
10#pragma once
11
12#include <cstdint>
13#include <memory>
14#include <optional>
15#include <type_traits>
16#include <vector>
17
18namespace inno
19{
20 class Writer;
21 class Image;
22
26 */
27 class Serializable
28 {
29 public:
34 virtual void WriteTo(Writer& writer) const = 0;
35
36 Serializable(const Serializable&) = default;
37 Serializable(Serializable&&) = default;
38 Serializable& operator=(const Serializable&) = default;
39 Serializable& operator=(Serializable&&) = default;
40 Serializable() = default;
41 virtual ~Serializable() = default;
42 };
43
47 */
48 class BytesWriterStrategy
49 {
50 public:
57 virtual void WriteBytes(Writer& writer, const char* name, const std::vector<uint8_t>& value) = 0;
58
59 BytesWriterStrategy(const BytesWriterStrategy&) = default;
60 BytesWriterStrategy(BytesWriterStrategy&&) = default;
61 BytesWriterStrategy& operator=(const BytesWriterStrategy&) = default;
62 BytesWriterStrategy& operator=(BytesWriterStrategy&&) = default;
63 BytesWriterStrategy() = default;
64 virtual ~BytesWriterStrategy() = default;
65 };
66
67#ifndef SWIG
68 template<typename T>
69 concept IsIterable = requires(T a) {
70 begin(a); // Requires 'begin' function
71 end(a); // Requires 'end' function
72 };
73 template<typename T>
74 concept IsNotIterable = !IsIterable<T>;
75#endif
76
163 */
164 class Writer
165 {
166 public:
172 virtual void Write(const char* name, const Serializable& s) = 0;
177 virtual void WriteRoot(Serializable& s) = 0;
178
184 virtual void Write(const char* name, const char* value) = 0;
190 virtual void Write(const char* name, int value) = 0;
196 virtual void Write(const char* name, unsigned int value) = 0;
202 virtual void Write(const char* name, double value) = 0;
208 virtual void Write(const char* name, int64_t value) = 0;
214 virtual void Write(const char* name, uint64_t value) = 0;
222 virtual void Write(const char* name, const std::shared_ptr<Image>& value) = 0;
227 virtual void WriteNull(const char* name) = 0;
233 */
234 void WriteArray(const char* name, const std::vector<uint8_t>& value)
235 {
236 bytesStrategy->WriteBytes(*this, name, value);
237 }
245 */
247 {
248 bytesStrategy = &s;
249 }
259 template<typename T>
260 void WriteArray(const char* name, const T& array);
261
266 */
267 void Write(const char* name, const std::shared_ptr<const Serializable>& s)
268 {
269 if (s != nullptr)
270 {
271 Write(name, *s);
272 }
273 else
274 {
275 WriteNull(name);
276 }
277 }
278
284 template<typename T>
285 void Write(const char* name, const std::optional<T>& s)
286 {
287 if (s.has_value())
288 {
289 Write(name, s.value());
290 }
291 else
292 {
293 WriteNull(name);
294 }
295 }
296
297 Writer(const Writer&) = default;
298 Writer(Writer&&) = default;
299 Writer& operator=(const Writer&) = default;
300 Writer& operator=(Writer&&) = default;
301 Writer()
302 : bytesStrategy(&defaultBytesStrategy)
303 {
304 }
305 virtual ~Writer() = default;
306
307 protected:
308 virtual void StartArray(const char* name) = 0;
309 virtual void EndArray() = 0;
310
311 private:
312#ifndef SWIG
313 template<typename T>
314 requires IsIterable<T>
315 void WriteInWriter(T& value)
316 {
317 WriteArray("", value);
318 }
319 template<typename T>
320 requires IsNotIterable<T>
321 void WriteInWriter(T& value)
322 {
323 Write("", value);
324 }
325#endif
326
327 class DefaultBytesStrategy : public BytesWriterStrategy
328 {
329 public:
330 void WriteBytes(Writer& writer, const char* name, const std::vector<uint8_t>& value) final
331 {
332 writer.WriteArray<>(name, value);
333 }
334
335 DefaultBytesStrategy(const DefaultBytesStrategy&) = default;
336 DefaultBytesStrategy(DefaultBytesStrategy&&) = default;
337 DefaultBytesStrategy& operator=(const DefaultBytesStrategy&) = default;
338 DefaultBytesStrategy& operator=(DefaultBytesStrategy&&) = default;
339 DefaultBytesStrategy() = default;
340 virtual ~DefaultBytesStrategy() = default;
341 };
342
343 DefaultBytesStrategy defaultBytesStrategy;
344 BytesWriterStrategy* bytesStrategy;
345 };
346
347 // MUST be here to be possible to override it with non template function
348 template<typename T>
349 void Writer::WriteArray(const char* name, const T& array)
350 {
351 StartArray(name);
352 for (auto& e : array)
353 {
354 WriteInWriter(e);
355 }
356 EndArray();
357 }
358} // namespace inno
It is used by Writer class to modify bytes serialization algorithm.
Definition Writer.h:48
virtual void WriteBytes(Writer &writer, const char *name, const std::vector< uint8_t > &value)=0
It writes bytes in given writer.
Image representation.
Definition Image.h:34
Interface that MUST be implemented by all classes those want to be serialized.
Definition Writer.h:27
virtual void WriteTo(Writer &writer) const =0
Write itself into writer.
Provides interface for serialization of all classes.
Definition Writer.h:164
virtual void Write(const char *name, const char *value)=0
Serialize string.
virtual void Write(const char *name, int64_t value)=0
Serialize int64_t integer.
void SetBytesStrategy(BytesWriterStrategy &s)
Set bytes serialization strategy .
Definition Writer.h:245
virtual void Write(const char *name, int value)=0
Serialize int.
void Write(const char *name, const std::shared_ptr< const Serializable > &s)
Serialize const serializable given as shared_ptr.
Definition Writer.h:266
virtual void Write(const char *name, const Serializable &s)=0
Serialize serializable.
virtual void Write(const char *name, const std::shared_ptr< Image > &value)=0
Serialize Image.
void WriteArray(const char *name, const std::vector< uint8_t > &value)
Serialize bytes from vector.
Definition Writer.h:233
virtual void WriteNull(const char *name)=0
Serialize null object.
virtual void WriteRoot(Serializable &s)=0
Serialize serializable as root object.
virtual void Write(const char *name, uint64_t value)=0
Serialize uint64_t integer.
void Write(const char *name, const std::optional< T > &s)
Serialize serializable given as shared_ptr.
Definition Writer.h:284
virtual void Write(const char *name, double value)=0
Serialize double.
virtual void Write(const char *name, unsigned int value)=0
Serialize unsigned int.