General Purpose library for Freestanding C++ and POSIX systems
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

151 lines
2.5 KiB

4 years ago
4 years ago
4 years ago
4 years ago
  1. #pragma once
  2. #include <gp/pair.hpp>
  3. #include <gp/optional.hpp>
  4. #include <gp/variant.hpp>
  5. #include <gp/vector.hpp>
  6. namespace gp {
  7. enum class cbor_type {
  8. uint = 0,
  9. nint = 1,
  10. bstr = 2,
  11. tstr = 3,
  12. list = 4,
  13. hmap = 5,
  14. tags = 6,
  15. oths = 7
  16. };
  17. enum class cbor_oths {
  18. value_false = 20,
  19. value_true = 21,
  20. value_null = 22,
  21. value_undefined = 23,
  22. byte = 24,
  23. word = 25,
  24. dword = 26,
  25. qword = 27,
  26. terminator = 31
  27. };
  28. enum class cbor_tags {
  29. datetime = 0,
  30. unix_time = 1,
  31. ubignum = 2,
  32. nbignum = 3,
  33. decimal = 4,
  34. bigfloat = 5,
  35. cose_encrypt0 = 16,
  36. cose_mac0 = 17,
  37. cose_sign1 = 18,
  38. expected_base64url = 21,
  39. expected_base64 = 22,
  40. expected_base16 = 23,
  41. encoded_cbor = 24,
  42. url = 32,
  43. base64url = 33,
  44. base64 = 34,
  45. regexp = 35,
  46. mime = 36,
  47. cose_encrypt = 96,
  48. cose_mac = 97,
  49. cose_sign = 98,
  50. signature = 55799
  51. };
  52. using cbor_number = gp::fixed_variant<
  53. int8_t,
  54. uint8_t,
  55. int16_t,
  56. uint16_t,
  57. int32_t,
  58. uint32_t,
  59. int64_t,
  60. uint64_t
  61. >;
  62. using cbor_floating_point = gp::fixed_variant<
  63. float,
  64. double
  65. >;
  66. struct undefined_t final {};
  67. template<typename T>
  68. using cbor_composite = gp::fixed_variant<
  69. cbor_number,
  70. gp::buffer<std::byte>,
  71. bool,
  72. gp::vector<T>,
  73. gp::vector<gp::pair<T, T>>,
  74. gp::nullopt_t,
  75. undefined_t,
  76. cbor_floating_point
  77. >;
  78. class cbor_value {
  79. cbor_composite<cbor_value> contents;
  80. gp::reference_wrapper<allocator> alloc;
  81. public:
  82. cbor_value(allocator& alloc_v)
  83. : contents(cbor_composite<cbor_value>(undefined_t{}))
  84. , alloc(alloc_v)
  85. {}
  86. cbor_value(cbor_number number, allocator& alloc_v)
  87. : contents(number)
  88. , alloc(alloc_v)
  89. {}
  90. cbor_value(const cbor_value& oth)
  91. : contents(oth.contents)
  92. , alloc(oth.alloc)
  93. {}
  94. cbor_value(cbor_value&& oth)
  95. : contents(gp::move(oth.contents))
  96. , alloc(gp::move(oth.alloc))
  97. {}
  98. cbor_value& operator=(cbor_value& value) {
  99. contents = value.contents;
  100. alloc = value.alloc;
  101. return *this;
  102. }
  103. cbor_value& operator=(cbor_value&& value) {
  104. gp::swap(contents, value.contents);
  105. gp::swap(alloc, value.alloc);
  106. return *this;
  107. }
  108. cbor_value& operator=(cbor_composite<cbor_value>& value) {
  109. contents = value;
  110. return *this;
  111. }
  112. template<typename T>
  113. cbor_value& operator=(T& value) {
  114. contents = value;
  115. return *this;
  116. }
  117. template<typename T>
  118. cbor_value& operator=(T&& value) {
  119. contents = gp::move(value);
  120. return *this;
  121. }
  122. auto new_array() {
  123. return gp::vector<cbor_value>{alloc};
  124. }
  125. auto new_object() {
  126. return gp::vector<gp::pair<cbor_value, cbor_value>>{alloc};
  127. }
  128. };
  129. }