CeresEngine 0.2.0
A game development framework
Loading...
Searching...
No Matches
CBOR.hpp
Go to the documentation of this file.
1//
2// CeresEngine - A game development framework
3//
4// Created by Rogiel Sulzbach.
5// Copyright (c) 2018-2022 Rogiel Sulzbach. All rights reserved.
6//
7
8#pragma once
9
12
22
24#include <cstddef>
25#include <utility>
26
27namespace CeresEngine {
28
37 class CBOR {
38 public:
39 class Encoder;
40 class Decoder;
41
42 public:
57 struct Array {
60
62 [[nodiscard]] bool isIndeterminate() const noexcept { return size == ~0u; }
63 };
64
81 struct Map {
84
86 [[nodiscard]] bool isIndeterminate() const noexcept { return size == ~0u; }
87 };
88
114 struct Tag {
117 };
118
146 struct Binary {
150
153
155 explicit Binary(const MemoryView<const Byte>& data) : stream(MemoryDataStream((Byte*)data.data(), data.size())) {}
156
159 };
160
166 struct Undefined {};
167
174 struct Break {};
175
188 struct Unknown {
196 };
197
199 enum class ValueType : UInt8 {
201 UInt64,
202
204 Int64,
205
207 Float,
208
210 Double,
211
213 Boolean,
214
216 String,
217
219 Binary,
220
222 Null,
223
225 Undefined,
226
228 Array,
229
231 Map,
232
234 Tag,
235
237 Break,
238
240 Unknown,
241 };
242
284 class Value : public Variant<UInt64, Int64, float, double, bool, String, Binary, std::nullptr_t, Undefined, Array, Map, Tag, Break, Unknown> {
286
287 public:
288 using super::super;
290
291 public:
294
297
298 public: // Type Checking
300 [[nodiscard]] bool isUInt64() const noexcept { return is<UInt64>(); }
301
303 [[nodiscard]] bool isInt64() const noexcept { return is<Int64>(); }
304
306 [[nodiscard]] bool isFloat() const noexcept { return is<float>(); }
307
309 [[nodiscard]] bool isDouble() const noexcept { return is<double>(); }
310
312 [[nodiscard]] bool isBoolean() const noexcept { return is<bool>(); }
313
315 [[nodiscard]] bool isString() const noexcept { return is<String>(); }
316
318 [[nodiscard]] bool isBinary() const noexcept { return is<Binary>(); }
319
322
325
327 [[nodiscard]] bool isArray() const noexcept { return is<Array>(); }
328
330 [[nodiscard]] bool isMap() const noexcept { return is<Map>(); }
331
333 [[nodiscard]] bool isTag() const noexcept { return is<Tag>(); }
334
336 [[nodiscard]] bool isBreak() const noexcept { return is<Break>(); }
337
340
341 public: // Get type
347
351 return as<Int64>();
352 }
353
357 return as<float>();
358 }
359
363 return as<double>();
364 }
365
369 return as<bool>();
370 }
371
375 return as<String>();
376 }
377
381 return as<Binary>();
382 }
383
385 [[nodiscard]] Binary&& asBinary() && noexcept {
387 return std::move(as<Binary>());
388 }
389
392
396 return as<Array>();
397 }
398
402 return as<Map>();
403 }
404
408 return as<Tag>();
409 }
410
414 return as<Unknown>();
415 }
416
418 template<typename T> requires std::is_enum_v<T>
419 [[nodiscard]] T asEnum() const {
420 if(isUInt64()) {
421 return T(asUInt64());
422 }
423
424 if(isInt64()) {
425 return T(asInt64());
426 }
427
429 }
430 };
431
432 public: // Reader & Writer
442 private:
448
449 public:
451 explicit Decoder(InputStream inputStream) : mStream(std::move(inputStream), 4096) {}
452
454 explicit Decoder(IInputStream& inputStream) : mStream(&inputStream, 4096) {}
455
456 public:
460
465 template<typename Callback> void map(const Value& value, Callback&& callback) {
466 const Map& map = value.as<Map>();
467 if(map.isIndeterminate()) {
468 for(const UInt64 index : range(map.size)) {
470 if(elementKey.isBreak()) {
471 break;
472 }
474
475 callback(*this, value.as<Map>(), index, std::move(elementKey), std::move(elementValue));
476 }
477 } else {
478 for(const UInt64 index : range(map.size)) {
479 callback(*this, value.as<Map>(), index, read(), read());
480 }
481 }
482 }
483
492
497 template<typename Callback> void array(const Value& value, Callback&& callback) {
498 const Array& array = value.as<Array>();
499 if(array.isIndeterminate()) {
500 for(const UInt64 index : range(std::numeric_limits<UInt64>::max())) {
501 Value element = read();
502 if(element.isBreak()) {
503 break;
504 }
505
506 callback(*this, array, index, std::move(element));
507 }
508 } else {
509 for(const UInt64 index : range(array.size)) {
510 callback(*this, array, index, read());
511 }
512 }
513 }
514
523
527 template<typename Callback> void tag(const Value& value, Callback&& callback) {
528 CE_ASSERT(value.is<Tag>());
529 callback(*this, value.as<Tag>(), read());
530 }
531 };
532
542 private:
548
549 public:
552
555
556 public:
558 void write(UInt64 value);
559
561 void write(Int64 value);
562
564 void write(float value);
565
567 void write(double value);
568
570 void write(bool value);
571
573 void write(StringView value);
574
576 void write(const String& value) { return write(StringView(value)); }
577
579 void write(const char* value) { return write(StringView(value)); }
580
582 void write(Binary value);
583
585 void write(std::nullptr_t value);
586
588 void write(Undefined value);
589
591 void write(Break value);
592
594 void break_() { write(Break()); }
595
597 void write(Array value);
598
604 void array(const UInt64 size) { write(Array{.size = size}); }
605
609 template<typename Func> void array(const UInt64 size, Func&& func) {
610 array(size);
611 func(*this);
612 }
613
620 void array() { write(Array{.size = ~0u}); }
621
628 template<typename Func> void array(Func&& func) {
629 array();
630 func(*this);
631 break_();
632 }
633
635 void write(Map value);
636
642 void map(const UInt64 size) { write(Map{.size = size}); }
643
647 template<typename Func> void map(const UInt64 size, Func&& func) {
648 map(size);
649 func(*this);
650 }
651
658 void map() { write(Map{.size = ~0u}); }
659
666 template<typename Func> void map(Func&& func) {
667 map();
668 func(*this);
669 break_();
670 }
671
673 void write(Tag value);
674
679 void tag(const UInt64 id) { write(Tag{.id = id}); }
680
684 template<typename Func> void tag(const UInt64 id, Func&& func) {
685 tag(id);
686 func(*this);
687 }
688
693 void binary(const void* data, size_t length);
694
696 void binary(const MemoryView<const Byte>& data) { return binary(data.data(), data.size()); }
697
710 void binary(const FunctionView<void(OutputStream&) const>& func);
711
726 void binary(UInt64 length, const FunctionView<void(OutputStream&) const>& func);
727
729 void write(Value&& value);
730
732 [[nodiscard]] bool flush() { return mStream.flush(); }
733
734 public:
736 template<typename T> requires std::is_enum_v<T>
737 void write(T value) {
738 using UnderlyingType = std::underlying_type_t<T>;
739 constexpr bool isSigned = std::is_signed_v<UnderlyingType>;
740 return isSigned ? write((Int64)value) : write((UInt64)value);
741 }
742 };
743 };
744
745} // namespace CeresEngine
#define CE_ASSERT_UNDEFINED()
Definition Macros.hpp:326
#define CE_ASSERT(...)
Definition Macros.hpp:323
A filtered InputStream that caches read and write operations to it's underlying InputStream.
Definition Stream.Buffered.hpp:16
A filtered OutputStream that caches read and write operations to it's underlying OutputStream.
Definition Stream.Buffered.hpp:53
bool flush() final
In the stream is buffered, invalidates any buffered write data from to stream.
The Decoder class is a CBOR (Concise Binary Object Representation) decoder that provides an abstracti...
Definition CBOR.hpp:441
Generator< Pair< Value, Value > > map(const Map &value)
Decodes the contents of a map from the CBOR value stream.
void array(const Value &value, Callback &&callback)
Decodes the contents of an array from the CBOR value stream.
Definition CBOR.hpp:497
void map(const Value &value, Callback &&callback)
Decodes the contents of a map from the CBOR value stream.
Definition CBOR.hpp:465
Value read()
Decodes the next object from the CBOR stream.
void tag(const Value &value, Callback &&callback)
Decodes the contents of a tag from the CBOR value stream.
Definition CBOR.hpp:527
BufferedInputStream mStream
The stream to read from.
Definition CBOR.hpp:447
Decoder(IInputStream &inputStream)
Creates a new CBOR decoder from an IInputStream.
Definition CBOR.hpp:454
Decoder(InputStream inputStream)
Creates a new CBOR decoder from an InputStream.
Definition CBOR.hpp:451
Generator< Value > array(const Array &value)
Decodes the contents of an array from the CBOR value stream.
The Encoder class is a CBOR (Concise Binary Object Representation) encoder that provides an abstracti...
Definition CBOR.hpp:541
void write(Map value)
Encodes a map header into the CBOR stream.
void map(const UInt64 size, Func &&func)
Encodes a map header into the CBOR stream.
Definition CBOR.hpp:647
void write(std::nullptr_t value)
Encodes a nullptr into the CBOR stream.
Encoder(IOutputStream &outputStream)
Creates a new CBOR encoder from an IOutputStream.
Definition CBOR.hpp:554
void write(Value &&value)
Encodes a value into the CBOR stream.
void map()
Encodes an map header into the CBOR stream.
Definition CBOR.hpp:658
void write(Binary value)
Encodes a Binary into the CBOR stream.
void map(const UInt64 size)
Encodes an map header into the CBOR stream.
Definition CBOR.hpp:642
void binary(const FunctionView< void(OutputStream &) const > &func)
Encodes binary data into the CBOR stream.
void map(Func &&func)
Encodes a map header into the CBOR stream.
Definition CBOR.hpp:666
void write(Array value)
Encodes an array header into the CBOR stream.
void array(const UInt64 size)
Encodes an array header into the CBOR stream.
Definition CBOR.hpp:604
bool flush()
Definition CBOR.hpp:732
void tag(const UInt64 id)
Encodes an tag header into the CBOR stream.
Definition CBOR.hpp:679
void write(Undefined value)
Encodes an undefined into the CBOR stream.
void write(bool value)
Encodes a bool into the CBOR stream.
BufferedOutputStream mStream
The stream to write to.
Definition CBOR.hpp:547
void write(Int64 value)
Encodes a signed integer into the CBOR stream.
void write(StringView value)
Encodes a StringView into the CBOR stream.
void write(double value)
Encodes a double into the CBOR stream.
void array(Func &&func)
Encodes an array header into the CBOR stream.
Definition CBOR.hpp:628
void tag(const UInt64 id, Func &&func)
Encodes a tag header into the CBOR stream.
Definition CBOR.hpp:684
void binary(UInt64 length, const FunctionView< void(OutputStream &) const > &func)
Encodes binary data into the CBOR stream.
void write(UInt64 value)
Encodes an unsigned integer into the CBOR stream.
void write(const char *value)
Encodes a const char* into the CBOR stream.
Definition CBOR.hpp:579
Encoder(OutputStream outputStream)
Creates a new CBOR encoder from an OutputStream.
Definition CBOR.hpp:551
void write(Tag value)
Encodes a tag header into the CBOR stream.
void break_()
Encodes an Break into the CBOR stream.
Definition CBOR.hpp:594
void write(T value)
Encodes an enum value into the CBOR stream.
Definition CBOR.hpp:737
void write(float value)
Encodes a float into the CBOR stream.
void binary(const MemoryView< const Byte > &data)
Encodes binary data into the CBOR stream.
Definition CBOR.hpp:696
void binary(const void *data, size_t length)
Encodes binary data into the CBOR stream.
void write(Break value)
Encodes an Break into the CBOR stream.
void write(const String &value)
Encodes a String into the CBOR stream.
Definition CBOR.hpp:576
void array(const UInt64 size, Func &&func)
Encodes an array header into the CBOR stream.
Definition CBOR.hpp:609
void array()
Encodes an array header into the CBOR stream.
Definition CBOR.hpp:620
The Value class represents a data holder in the CBOR encoder or decoder.
Definition CBOR.hpp:284
bool isBoolean() const noexcept
Checks if the type is bool.
Definition CBOR.hpp:312
bool isBinary() const noexcept
Checks if the type is Binary.
Definition CBOR.hpp:318
bool isString() const noexcept
Checks if the type is String.
Definition CBOR.hpp:315
bool isMap() const noexcept
Checks if the type is Map.
Definition CBOR.hpp:330
const Unknown & asUnknown() const noexcept
Gets the value as type Unknown.
Definition CBOR.hpp:412
Binary && asBinary() &&noexcept
Gets the value as type Binary.
Definition CBOR.hpp:385
const String & asString() const noexcept
Gets the value as type String.
Definition CBOR.hpp:373
bool isTag() const noexcept
Checks if the type is Tag.
Definition CBOR.hpp:333
double asDouble() const noexcept
Gets the value as type double.
Definition CBOR.hpp:361
ValueType getType() const noexcept
Gets the type of the stored value.
Value()
Definition CBOR.hpp:289
const Tag & asTag() const noexcept
Gets the value as type Tag.
Definition CBOR.hpp:406
const Array & asArray() const noexcept
Gets the value as type Array.
Definition CBOR.hpp:394
bool isInt64() const noexcept
Checks if the type is Int64.
Definition CBOR.hpp:303
const Map & asMap() const noexcept
Gets the value as type Map.
Definition CBOR.hpp:400
bool isUInt64() const noexcept
Checks if the type is UInt64.
Definition CBOR.hpp:300
bool isBreak() const noexcept
Checks if the type is Break.
Definition CBOR.hpp:336
T asEnum() const
Gets the value as an enum type.
Definition CBOR.hpp:419
bool isDouble() const noexcept
Checks if the type is double.
Definition CBOR.hpp:309
String toString() const
Converts the type into a string format.
bool isUndefined() const noexcept
Checks if the type is undefined.
Definition CBOR.hpp:324
bool isUnknown() const noexcept
Checks if the type is Unknown.
Definition CBOR.hpp:339
const Binary & asBinary() const &noexcept
Gets the value as type Binary.
Definition CBOR.hpp:379
StringView asStringView() const noexcept
Gets the value as type asStringView.
Definition CBOR.hpp:391
UInt64 asUInt64() const noexcept
Gets the value as type UInt64.
Definition CBOR.hpp:343
bool isFloat() const noexcept
Checks if the type is float.
Definition CBOR.hpp:306
bool isArray() const noexcept
Checks if the type is Array.
Definition CBOR.hpp:327
bool isNull() const noexcept
Checks if the type is nullptr.
Definition CBOR.hpp:321
bool asBoolean() const noexcept
Gets the value as type bool.
Definition CBOR.hpp:367
Int64 asInt64() const noexcept
Gets the value as type Int64.
Definition CBOR.hpp:349
float asFloat() const noexcept
Gets the value as type float.
Definition CBOR.hpp:355
Concise Binary Object Representation (CBOR)
Definition CBOR.hpp:37
ValueType
An enumeration of all possible types.
Definition CBOR.hpp:199
@ Float
The value is of Float type.
@ String
The value is of String type.
@ Boolean
The value is of Boolean type.
@ UInt64
The value is of UInt64 type.
@ Null
The value is of Null type.
@ Double
The value is of Double type.
@ Int64
The value is of Int64 type.
A generator represents a coroutine type that produces a sequence of values of type T,...
Definition Generator.hpp:50
A stream that provides read-only stream functionality.
Definition Stream.hpp:126
A stream that provides write-only stream functionality.
Definition Stream.hpp:233
A stream that provides read-only stream functionality.
Definition Stream.hpp:210
A input stream that filters another input stream and limit reads to up-to limit bytes.
Definition Stream.Limited.hpp:16
A data stream that reads or writes data into a memory buffer.
Definition Stream.hpp:693
A memory view is a class which attaches to an chunk of memory and provides a view to it (optionally c...
Definition MemoryView.hpp:62
A stream that provides write-only stream functionality.
Definition Stream.hpp:307
Definition Variant.hpp:15
Definition Application.hpp:19
Byte
Definition DataTypes.hpp:40
std::uint64_t UInt64
Definition DataTypes.hpp:26
auto range()
Returns an iterator that increases it's value from 0 to end by 1 for each step.
Definition Iterator.hpp:350
BasicStringView< char > StringView
Narrow string view used for handling narrow encoded text in UTF-8.
Definition String.hpp:190
auto move(Vector3 position)
Moves a entity to the given position.
Definition Helpers.hpp:22
FunctionBase< false, true, fu2::capacity_default, true, false, Signatures... > FunctionView
A non owning copyable function wrapper for arbitrary callable types.
Definition Function.hpp:64
std::uint8_t UInt8
Definition DataTypes.hpp:17
std::int64_t Int64
Definition DataTypes.hpp:24
constexpr size_t hash(const T &v)
Generates a hash for the provided type.
Definition Hash.hpp:25
Definition Span.hpp:668
Represents a CBOR array.
Definition CBOR.hpp:57
UInt64 size
The number of items in the array.
Definition CBOR.hpp:59
bool isIndeterminate() const noexcept
Determines if the array is indeterminate.
Definition CBOR.hpp:62
Represents a CBOR byte string (or binary).
Definition CBOR.hpp:146
InputStream stream
The stream that contains the binary data either being written or read.
Definition CBOR.hpp:149
Binary(InputStream stream)
Creates a new binary object from a input stream.
Definition CBOR.hpp:152
Binary(InputStream stream, const UInt64 limit)
Creates a new limited read stream.
Definition CBOR.hpp:158
Binary(const MemoryView< const Byte > &data)
Creates a new binary from a memory view.
Definition CBOR.hpp:155
Represents a CBOR break.
Definition CBOR.hpp:174
Represents a CBOR map.
Definition CBOR.hpp:81
bool isIndeterminate() const noexcept
Determines if the array is indeterminate.
Definition CBOR.hpp:86
UInt64 size
The number of key-pair items in the map.
Definition CBOR.hpp:83
Represents a CBOR tag.
Definition CBOR.hpp:114
UInt64 id
The tag unique identifier.
Definition CBOR.hpp:116
Represents a CBOR undefined type.
Definition CBOR.hpp:166
Represents an unknown CBOR opcode.
Definition CBOR.hpp:188
UInt64 minor
Represents the minor type of the unknown CBOR opcode.
Definition CBOR.hpp:195