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 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276
|
#ifndef RFL_CAPNPROTO_WRITER_HPP_
#define RFL_CAPNPROTO_WRITER_HPP_
#include <capnp/dynamic.h>
#include <kj/array.h>
#include <bit>
#include <cstdint>
#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 "../Vectorstring.hpp"
#include "../always_false.hpp"
#include "../internal/is_literal.hpp"
#include "../internal/ptr_cast.hpp"
namespace rfl::capnproto {
class Writer {
public:
struct CapnProtoOutputArray {
capnp::DynamicList::Builder val_;
size_t ix_ = 0;
};
struct CapnProtoOutputMap {
capnp::DynamicStruct::Builder val_;
size_t ix_ = 0;
};
struct CapnProtoOutputObject {
capnp::DynamicStruct::Builder val_;
};
struct CapnProtoOutputUnion {
capnp::DynamicStruct::Builder val_;
};
struct CapnProtoOutputVar {
capnp::DynamicValue::Builder val_;
};
using OutputArrayType = CapnProtoOutputArray;
using OutputMapType = CapnProtoOutputMap;
using OutputObjectType = CapnProtoOutputObject;
using OutputUnionType = CapnProtoOutputUnion;
using OutputVarType = CapnProtoOutputVar;
Writer(capnp::DynamicStruct::Builder* _root);
~Writer();
template <class T>
OutputArrayType array_as_root(const T _size) const noexcept {
static_assert(always_false_v<T>,
"In Cap'n Proto, root values must always be structs.");
throw std::runtime_error("Unsupported.");
}
template <class T>
OutputMapType map_as_root(const T _size) const noexcept {
static_assert(always_false_v<T>,
"In Cap'n Proto, root values must always be structs.");
throw std::runtime_error("Unsupported.");
}
Writer::OutputObjectType object_as_root(const size_t _size) const noexcept;
template <class T = int>
OutputVarType null_as_root() const noexcept {
static_assert(always_false_v<T>,
"In Cap'n Proto, root values must always be structs.");
throw std::runtime_error("Unsupported.");
}
template <class T = int>
OutputUnionType union_as_root() const noexcept {
static_assert(always_false_v<T>,
"In Cap'n Proto, root values must always be structs.");
throw std::runtime_error("Unsupported.");
}
template <class T>
OutputVarType value_as_root(const T& _var) const noexcept {
static_assert(always_false_v<T>,
"In Cap'n Proto, root values must always be structs.");
throw std::runtime_error("Unsupported.");
}
OutputArrayType add_array_to_array(const size_t _size,
OutputArrayType* _parent) const noexcept;
OutputArrayType add_array_to_map(const std::string_view& _name,
const size_t _size,
OutputMapType* _parent) const noexcept;
OutputArrayType add_array_to_object(const std::string_view& _name,
const size_t _size,
OutputObjectType* _parent) const noexcept;
OutputArrayType add_array_to_union(const size_t _index, const size_t _size,
OutputUnionType* _parent) const noexcept;
OutputMapType add_map_to_array(const size_t _size,
OutputArrayType* _parent) const noexcept;
OutputMapType add_map_to_map(const std::string_view& _name,
const size_t _size,
OutputMapType* _parent) const noexcept;
OutputMapType add_map_to_object(const std::string_view& _name,
const size_t _size,
OutputObjectType* _parent) const noexcept;
OutputMapType add_map_to_union(const size_t _index, const size_t _size,
OutputUnionType* _parent) const noexcept;
OutputObjectType add_object_to_array(const size_t _size,
OutputArrayType* _parent) const noexcept;
OutputObjectType add_object_to_map(const std::string_view& _name,
const size_t _size,
OutputMapType* _parent) const noexcept;
OutputObjectType add_object_to_object(
const std::string_view& _name, const size_t _size,
OutputObjectType* _parent) const noexcept;
OutputObjectType add_object_to_union(const size_t _index, const size_t _size,
OutputUnionType* _parent) const noexcept;
OutputUnionType add_union_to_array(OutputArrayType* _parent) const noexcept;
OutputUnionType add_union_to_map(const std::string_view& _name,
OutputMapType* _parent) const noexcept;
OutputUnionType add_union_to_object(const std::string_view& _name,
OutputObjectType* _parent) const noexcept;
OutputUnionType add_union_to_union(const size_t _index,
OutputUnionType* _parent) const noexcept;
OutputVarType add_null_to_array(OutputArrayType* _parent) const noexcept;
OutputVarType add_null_to_map(const std::string_view& _name,
OutputMapType* _parent) const noexcept;
OutputVarType add_null_to_object(const std::string_view& _name,
OutputObjectType* _parent) const noexcept;
OutputVarType add_null_to_union(const size_t _index,
OutputUnionType* _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>()) {
_parent->val_.set(_parent->ix_++, _var.c_str());
} else if constexpr (std::is_same<std::remove_cvref_t<T>,
rfl::Bytestring>() ||
std::is_same<std::remove_cvref_t<T>,
rfl::Vectorstring>()) {
const auto array_ptr = kj::ArrayPtr<const kj::byte>(
internal::ptr_cast<const unsigned char*>(_var.data()), _var.size());
_parent->val_.set(_parent->ix_++, capnp::Data::Reader(array_ptr));
} else if constexpr (std::is_floating_point<std::remove_cvref_t<T>>() ||
std::is_same<std::remove_cvref_t<T>, bool>()) {
_parent->val_.set(_parent->ix_++, _var);
} else if constexpr (std::is_integral<std::remove_cvref_t<T>>()) {
_parent->val_.set(_parent->ix_++, static_cast<std::int64_t>(_var));
} else if constexpr (internal::is_literal_v<T>) {
return add_value_to_array(_var.value(), _parent);
} else {
static_assert(rfl::always_false_v<T>, "Unsupported type.");
}
return OutputVarType{};
}
template <class T>
OutputVarType add_value_to_map(const std::string_view& _name, const T& _var,
OutputMapType* _parent) const noexcept {
auto entries =
OutputArrayType{_parent->val_.get("entries").as<capnp::DynamicList>()};
auto new_entry = add_object_to_array(2, &entries);
add_value_to_object("key", std::string(_name), &new_entry);
return add_value_to_object("value", _var, &new_entry);
}
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>()) {
_parent->val_.set(_name.data(), _var.c_str());
} else if constexpr (std::is_same<std::remove_cvref_t<T>,
rfl::Bytestring>()) {
const auto array_ptr = kj::ArrayPtr<const kj::byte>(
internal::ptr_cast<const unsigned char*>(_var.data()), _var.size());
_parent->val_.set(_name.data(), capnp::Data::Reader(array_ptr));
} else if constexpr (std::is_floating_point<std::remove_cvref_t<T>>() ||
std::is_same<std::remove_cvref_t<T>, bool>()) {
_parent->val_.set(_name.data(), _var);
} else if constexpr (std::is_integral<std::remove_cvref_t<T>>()) {
_parent->val_.set(_name.data(), static_cast<std::int64_t>(_var));
} else if constexpr (internal::is_literal_v<T>) {
return add_value_to_object(_name, _var.value(), _parent);
} else {
static_assert(rfl::always_false_v<T>, "Unsupported type.");
}
return OutputVarType{};
}
template <class T>
OutputVarType add_value_to_union(const size_t _index, const T& _var,
OutputUnionType* _parent) const noexcept {
const auto field = _parent->val_.getSchema().getFields()[_index];
if constexpr (std::is_same<std::remove_cvref_t<T>, std::string>()) {
_parent->val_.set(field, _var.c_str());
} else if constexpr (std::is_same<std::remove_cvref_t<T>,
rfl::Bytestring>()) {
const auto array_ptr = kj::ArrayPtr<const kj::byte>(
internal::ptr_cast<const unsigned char*>(_var.data()), _var.size());
_parent->val_.set(field, capnp::Data::Reader(array_ptr));
} else if constexpr (std::is_floating_point<std::remove_cvref_t<T>>() ||
std::is_same<std::remove_cvref_t<T>, bool>()) {
_parent->val_.set(field, _var);
} else if constexpr (std::is_integral<std::remove_cvref_t<T>>()) {
_parent->val_.set(field, static_cast<std::int64_t>(_var));
} else if constexpr (internal::is_literal_v<T>) {
return add_value_to_union(_index, _var.value(), _parent);
} else {
static_assert(rfl::always_false_v<T>, "Unsupported type.");
}
return OutputVarType{};
}
void end_array(OutputArrayType* _arr) const noexcept {}
void end_map(OutputMapType* _obj) const noexcept {}
void end_object(OutputObjectType* _obj) const noexcept {}
private:
capnp::DynamicStruct::Builder* root_;
};
} // namespace rfl::capnproto
#endif
|