Skip to content

Commit

Permalink
Began the passing through of the schema type
Browse files Browse the repository at this point in the history
  • Loading branch information
liuzicheng1987 committed Jan 25, 2025
1 parent 860f596 commit 397ba60
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 6 deletions.
8 changes: 6 additions & 2 deletions include/rfl/flatbuf/Writer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ class Writer {
throw std::runtime_error("Unsupported.");
}

Writer::OutputObjectType object_as_root(const size_t _size) const noexcept;
Writer::OutputObjectType object_as_root(const size_t _size) const;

template <class T = int>
OutputVarType null_as_root() const noexcept {
Expand Down Expand Up @@ -113,7 +113,7 @@ class Writer {

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

OutputArrayType add_array_to_union(const size_t _index, const size_t _size,
OutputUnionType* _parent) const noexcept;
Expand Down Expand Up @@ -244,6 +244,10 @@ class Writer {

void end_object(OutputObjectType* _obj) const noexcept {}

private:
/// Starts a new typed vector.
void start_vector(const schema::Type& _type, const size_t _size) const;

private:
Ref<flatbuffers::FlatBufferBuilder> fbb_;

Expand Down
19 changes: 19 additions & 0 deletions include/rfl/flatbuf/schema/Type.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include <map>
#include <memory>
#include <optional>
#include <stdexcept>
#include <string>

#include "../../Literal.hpp"
Expand Down Expand Up @@ -80,6 +81,24 @@ struct Type {
UInt32, UInt64, Float32, Float64, String, Enum, Optional,
Vector, Map, Reference, Table, Union>;

/// Converts to T or throws.
template <class T>
const T& convert_to() const {
return value.visit([](const auto& _v) -> const T& {
using V = std::remove_cvref_t<decltype(_v)>;
if constexpr (std::is_same<T, V>()) {
return _v;
} else if constexpr (std::is_same<V, Reference>()) {
if (!_v.type_ptr) {
throw std::runtime_error("Type pointer of reference not set.");
}
return _v.type_ptr->template convert_to<T>();
} else {
throw std::runtime_error("Type pointer of reference not set.");
}
});
}

const auto& reflection() const { return value; }

Type with_name(const std::string& _name) const;
Expand Down
74 changes: 70 additions & 4 deletions src/rfl/flatbuf/Writer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#include <stdexcept>

#include "rfl/always_false.hpp"
#include "rfl/parsing/schemaful/IsSchemafulWriter.hpp"

namespace rfl::flatbuf {
Expand All @@ -15,9 +16,14 @@ Writer::Writer(const Ref<flatbuffers::FlatBufferBuilder>& _fbb,

Writer::~Writer() = default;

Writer::OutputObjectType Writer::object_as_root(
const size_t _size) const noexcept {
return OutputObjectType{/* TODO */};
Writer::OutputObjectType Writer::object_as_root(const size_t _size) const {
if (!schema_->root_type_.type_ptr) {
throw std::runtime_error("Root type not set.");
}
return OutputObjectType{
.schema_ =
schema_->root_type_.type_ptr->convert_to<schema::Type::Table>(),
.offset_ = fbb_->StartTable()};
}

Writer::OutputArrayType Writer::add_array_to_array(
Expand All @@ -29,7 +35,12 @@ Writer::OutputArrayType Writer::add_array_to_map(

Writer::OutputArrayType Writer::add_array_to_object(
const std::string_view& _name, const size_t _size,
OutputObjectType* _parent) const noexcept {}
OutputObjectType* _parent) const {
const auto schema = _parent->schema_.fields.at(_parent->ix_)
.second.convert_to<schema::Type::Vector>();
start_vector(*schema.type, _size);
return OutputArrayType{.schema_ = schema};
}

Writer::OutputArrayType Writer::add_array_to_union(
const size_t _index, const size_t _size,
Expand Down Expand Up @@ -89,4 +100,59 @@ Writer::OutputVarType Writer::add_null_to_object(
Writer::OutputVarType Writer::add_null_to_union(
const size_t _index, OutputUnionType* _parent) const noexcept {}

void Writer::start_vector(const schema::Type& _type, const size_t _size) const {
_type.reflection().visit([&]<class T>(const T& _t) {
using U = std::remove_cvref_t<T>;
if constexpr (std::is_same<U, schema::Type::Bool>()) {
fbb_->StartVector<bool>(_size);
} else if constexpr (std::is_same<U, schema::Type::Byte>()) {
throw std::runtime_error("TODO"); // TODO
} else if constexpr (std::is_same<U, schema::Type::UByte>()) {
throw std::runtime_error("TODO"); // TODO
} else if constexpr (std::is_same<U, schema::Type::Int8>()) {
fbb_->StartVector<int8_t>(_size);
} else if constexpr (std::is_same<U, schema::Type::Int16>()) {
fbb_->StartVector<int16_t>(_size);
} else if constexpr (std::is_same<U, schema::Type::Int32>()) {
fbb_->StartVector<int32_t>(_size);
} else if constexpr (std::is_same<U, schema::Type::Int64>()) {
fbb_->StartVector<int64_t>(_size);
} else if constexpr (std::is_same<U, schema::Type::UInt8>()) {
fbb_->StartVector<uint8_t>(_size);
} else if constexpr (std::is_same<U, schema::Type::UInt16>()) {
fbb_->StartVector<uint16_t>(_size);
} else if constexpr (std::is_same<U, schema::Type::UInt32>()) {
fbb_->StartVector<uint32_t>(_size);
} else if constexpr (std::is_same<U, schema::Type::UInt64>()) {
fbb_->StartVector<uint64_t>(_size);
} else if constexpr (std::is_same<U, schema::Type::Float32>()) {
fbb_->StartVector<float>(_size);
} else if constexpr (std::is_same<U, schema::Type::Float64>()) {
fbb_->StartVector<double>(_size);
} else if constexpr (std::is_same<U, schema::Type::String>()) {
fbb_->StartVector<flatbuffers::Offset<flatbuffers::String>>(_size);
} else if constexpr (std::is_same<U, schema::Type::Enum>()) {
throw std::runtime_error("TODO"); // TODO
} else if constexpr (std::is_same<U, schema::Type::Map>()) {
throw std::runtime_error("TODO"); // TODO
} else if constexpr (std::is_same<U, schema::Type::Optional>()) {
throw std::runtime_error("TODO"); // TODO
} else if constexpr (std::is_same<U, schema::Type::Vector>()) {
throw std::runtime_error("TODO"); // TODO
} else if constexpr (std::is_same<U, schema::Type::Reference>()) {
if (!_t.type_ptr) {
throw std::runtime_error("type_ptr not set for '" + _t.type_name +
"'.");
}
start_vector(*_t.type_ptr, _size);
} else if constexpr (std::is_same<U, schema::Type::Table>()) {
throw std::runtime_error("TODO"); // TODO
} else if constexpr (std::is_same<U, schema::Type::Union>()) {
throw std::runtime_error("TODO"); // TODO
} else {
static_assert(always_false_v<T>, "Unsupported type");
}
});
}

} // namespace rfl::flatbuf

0 comments on commit 397ba60

Please sign in to comment.