From 23c76ff16593171ad4af3101284f8840acb8fbda Mon Sep 17 00:00:00 2001 From: "Dr. Patrick Urbanke" Date: Sun, 22 Dec 2024 18:28:47 +0100 Subject: [PATCH] Made sure the schema compiles --- include/rfl/capnproto/Schema.hpp | 15 +++++++------- include/rfl/capnproto/SchemaImpl.hpp | 30 ++++++++++------------------ src/reflectcpp_capnproto.cpp | 3 +++ src/rfl/capnproto/SchemaImpl.cpp | 22 ++++++++++++++++++++ 4 files changed, 43 insertions(+), 27 deletions(-) create mode 100644 src/rfl/capnproto/SchemaImpl.cpp diff --git a/include/rfl/capnproto/Schema.hpp b/include/rfl/capnproto/Schema.hpp index 147ffedb..f254f6d4 100644 --- a/include/rfl/capnproto/Schema.hpp +++ b/include/rfl/capnproto/Schema.hpp @@ -1,6 +1,8 @@ #ifndef RFL_CAPNPROTO_SCHEMA_HPP_ #define RFL_CAPNPROTO_SCHEMA_HPP_ +#include + #include #include "../Ref.hpp" @@ -13,8 +15,7 @@ class Schema { public: using Type = std::remove_cvref_t; - Schema(const std::string& _json_str) - : impl_(Ref::make(_json_str)) {} + Schema(const std::string& _str) : impl_(Ref::make(_str)) {} static Result> from_json(const std::string& _json_str) noexcept { try { @@ -25,14 +26,12 @@ class Schema { } /// The JSON string used to create this schema. - const std::string& json_str() const { return impl_->json_str(); } - - /// The JSON string used to create this schema. - const std::string& str() const { return impl_->json_str(); } + const std::string& str() const { return impl_->str(); } - /// The interface used to create new values. - capnproto_value_iface_t* iface() const { return impl_->iface(); }; + /// The interface used to generate new values. + const capnp::StructSchema& value() const { return impl_->value(); }; + private: private: /// We are using the "pimpl"-pattern Ref impl_; diff --git a/include/rfl/capnproto/SchemaImpl.hpp b/include/rfl/capnproto/SchemaImpl.hpp index 6730ccc9..8c3512fe 100644 --- a/include/rfl/capnproto/SchemaImpl.hpp +++ b/include/rfl/capnproto/SchemaImpl.hpp @@ -1,7 +1,7 @@ #ifndef RFL_CAPNPROTO_SCHEMAIMPL_HPP_ #define RFL_CAPNPROTO_SCHEMAIMPL_HPP_ -#include +#include #include @@ -12,33 +12,25 @@ namespace rfl::capnproto { class SchemaImpl { public: - SchemaImpl(const std::string& _json_str); + SchemaImpl(const std::string& _str); - ~SchemaImpl(); - - SchemaImpl(const SchemaImpl& _other) = delete; - - SchemaImpl(SchemaImpl&& _other) noexcept; - - SchemaImpl& operator=(const SchemaImpl& _other) = delete; - - SchemaImpl& operator=(SchemaImpl&& _other) noexcept; + ~SchemaImpl() = default; /// The JSON string used to create this schema. - const std::string& json_str() const { return json_str_; } + const std::string& str() const { return str_; } /// The interface used to create new values. - capnproto_value_iface_t* iface() const { return iface_; }; + const capnp::StructSchema& value() const { return *schema_; }; private: - /// The JSON string used to create the schema. - std::string json_str_; + static capnp::StructSchema make_schema(const std::string& _str); - /// The actual schema - Box schema_; + private: + /// The string used to create the schema. + std::string str_; - /// The interface used to create new, generic classes. - capnproto_value_iface_t* iface_; + /// The actual schema + Box schema_; }; } // namespace rfl::capnproto diff --git a/src/reflectcpp_capnproto.cpp b/src/reflectcpp_capnproto.cpp index 877ea0ca..dafeb8c7 100644 --- a/src/reflectcpp_capnproto.cpp +++ b/src/reflectcpp_capnproto.cpp @@ -28,9 +28,12 @@ SOFTWARE. // don't need to add multiple source files into their build. // Also, this speeds up compile time, compared to multiple separate .cpp files // compilation. + /* #include "rfl/avro/Reader.cpp" #include "rfl/avro/SchemaImpl.cpp" #include "rfl/avro/Type.cpp" #include "rfl/avro/Writer.cpp" #include "rfl/avro/to_schema.cpp"*/ +#include "rfl/capnproto/SchemaImpl.cpp" + diff --git a/src/rfl/capnproto/SchemaImpl.cpp b/src/rfl/capnproto/SchemaImpl.cpp new file mode 100644 index 00000000..2960da69 --- /dev/null +++ b/src/rfl/capnproto/SchemaImpl.cpp @@ -0,0 +1,22 @@ +#include "rfl/capnproto/SchemaImpl.hpp" + +#include +#include + +namespace rfl::capnproto { + +SchemaImpl::SchemaImpl(const std::string& _str) + : str_(_str), schema_(Box::make(make_schema(_str))) {} + +capnp::StructSchema make_schema(const std::string& _str) { + auto dir = kj::newInMemoryDirectory(kj::nullClock()); + auto path = kj::Path::parse("foo/bar.capnp"); + dir->openFile(path, kj::WriteMode::CREATE | kj::WriteMode::CREATE_PARENT) + ->writeAll(_str); + capnp::SchemaParser parser; + auto parsed_schema = + parser.parseFromDirectory(*dir, std::move(path), nullptr); + return parsed_schema.asStruct(); +} + +} // namespace rfl::capnproto