File: Writer.hpp

package info (click to toggle)
reflect-cpp 0.18.0%2Bds-3
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 12,524 kB
  • sloc: cpp: 44,484; python: 131; makefile: 30; sh: 3
file content (180 lines) | stat: -rw-r--r-- 6,324 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
#ifndef RFL_BSON_WRITER_HPP_
#define RFL_BSON_WRITER_HPP_

#include <bson/bson.h>

#include <cstddef>
#include <exception>
#include <map>
#include <sstream>
#include <stdexcept>
#include <string>
#include <string_view>
#include <type_traits>
#include <variant>
#include <vector>

#include "../Box.hpp"
#include "../Bytestring.hpp"
#include "../Ref.hpp"
#include "../Result.hpp"
#include "../always_false.hpp"
#include "../internal/ptr_cast.hpp"

namespace rfl {
namespace bson {

/// Please refer to https://mongoc.org/libbson/current/api.html
class Writer {
  struct BSONType {
    bson_t val_;
  };

  struct IsArray {
    bson_array_builder_t* ptr_;
  };

  struct IsObject {
    bson_t* ptr_;
  };

  struct IsRoot {};

  using ParentType = std::variant<IsArray, IsObject, IsRoot>;

 public:
  struct BSONOutputArray {
    BSONOutputArray(bson_array_builder_t* _val, ParentType _parent)
        : parent_(_parent), val_(_val) {}
    ParentType parent_;
    bson_array_builder_t* val_;
  };

  struct BSONOutputObject {
    BSONOutputObject(bson_t* _val, ParentType _parent)
        : parent_(_parent), val_(_val) {}
    ParentType parent_;
    bson_t* val_;
  };

  struct BSONOutputVar {};

  using OutputArrayType = BSONOutputArray;
  using OutputObjectType = BSONOutputObject;
  using OutputVarType = BSONOutputVar;

  Writer(bson_t* _doc);

  ~Writer();

  OutputArrayType array_as_root(const size_t _size) const noexcept;

  OutputObjectType object_as_root(const size_t _size) const noexcept;

  OutputVarType null_as_root() const noexcept;

  template <class T>
  OutputVarType value_as_root(const T& _var) const noexcept {
    static_assert(rfl::always_false_v<T>,
                  "BSON only allows arrays or objects as its root.");
    return OutputVarType{};
  }

  OutputArrayType add_array_to_array(const size_t _size,
                                     OutputArrayType* _parent) const noexcept;

  OutputArrayType add_array_to_object(const std::string_view& _name,
                                      const size_t _size,
                                      OutputObjectType* _parent) const noexcept;

  OutputObjectType add_object_to_array(const size_t _size,
                                       OutputArrayType* _parent) const noexcept;

  OutputObjectType add_object_to_object(
      const std::string_view& _name, const size_t _size,
      OutputObjectType* _parent) const noexcept;

  template <class T>
  OutputVarType add_value_to_array(const T& _var,
                                   OutputArrayType* _parent) const noexcept {
    if constexpr (std::is_same<std::remove_cvref_t<T>, std::string>()) {
      bson_array_builder_append_utf8(_parent->val_, _var.c_str(),
                                     static_cast<int>(_var.size()));
    } else if constexpr (std::is_same<std::remove_cvref_t<T>,
                                      rfl::Bytestring>()) {
      bson_array_builder_append_binary(
          _parent->val_, BSON_SUBTYPE_BINARY,
          internal::ptr_cast<const uint8_t*>(_var.c_str()),
          static_cast<uint32_t>(_var.size()));
    } else if constexpr (std::is_same<std::remove_cvref_t<T>, bool>()) {
      bson_array_builder_append_bool(_parent->val_, _var);
    } else if constexpr (std::is_floating_point<std::remove_cvref_t<T>>()) {
      bson_array_builder_append_double(_parent->val_,
                                       static_cast<double>(_var));
    } else if constexpr (std::is_integral<std::remove_cvref_t<T>>()) {
      bson_array_builder_append_int64(_parent->val_,
                                      static_cast<std::int64_t>(_var));
    } else if constexpr (std::is_same<std::remove_cvref_t<T>, bson_oid_t>()) {
      bson_array_builder_append_oid(_parent->val_, &_var);
    } else {
      static_assert(rfl::always_false_v<T>, "Unsupported type.");
    }
    return OutputVarType{};
  }

  template <class T>
  OutputVarType add_value_to_object(const std::string_view& _name,
                                    const T& _var,
                                    OutputObjectType* _parent) const noexcept {
    if constexpr (std::is_same<std::remove_cvref_t<T>, std::string>()) {
      bson_append_utf8(_parent->val_, _name.data(),
                       static_cast<int>(_name.size()), _var.c_str(),
                       static_cast<int>(_var.size()));
    } else if constexpr (std::is_same<std::remove_cvref_t<T>,
                                      rfl::Bytestring>()) {
      bson_append_binary(_parent->val_, _name.data(),
                         static_cast<int>(_name.size()), BSON_SUBTYPE_BINARY,
                         internal::ptr_cast<const uint8_t*>(_var.c_str()),
                         static_cast<uint32_t>(_var.size()));
    } else if constexpr (std::is_same<std::remove_cvref_t<T>, bool>()) {
      bson_append_bool(_parent->val_, _name.data(),
                       static_cast<int>(_name.size()), _var);
    } else if constexpr (std::is_floating_point<std::remove_cvref_t<T>>()) {
      bson_append_double(_parent->val_, _name.data(),
                         static_cast<int>(_name.size()),
                         static_cast<double>(_var));
    } else if constexpr (std::is_integral<std::remove_cvref_t<T>>()) {
      bson_append_int64(_parent->val_, _name.data(),
                        static_cast<int>(_name.size()),
                        static_cast<std::int64_t>(_var));
    } else if constexpr (std::is_same<std::remove_cvref_t<T>, bson_oid_t>()) {
      bson_append_oid(_parent->val_, _name.data(),
                      static_cast<int>(_name.size()), &_var);
    } else {
      static_assert(rfl::always_false_v<T>, "Unsupported type.");
    }
    return OutputVarType{};
  }

  OutputVarType add_null_to_array(OutputArrayType* _parent) const noexcept;

  OutputVarType add_null_to_object(const std::string_view& _name,
                                   OutputObjectType* _parent) const noexcept;

  void end_array(OutputArrayType* _arr) const noexcept;

  void end_object(OutputObjectType* _obj) const noexcept;

 private:
  /// Pointer to the main document. In BSON, documents are what are usually
  /// called objects.
  bson_t* const doc_;

  /// Contain all of the subdocuments.
  const rfl::Ref<std::vector<rfl::Box<BSONType>>> subdocs_;
};

}  // namespace bson
}  // namespace rfl

#endif