Skip to content

Commit

Permalink
Made sure the schema compiles
Browse files Browse the repository at this point in the history
  • Loading branch information
liuzicheng1987 committed Dec 22, 2024
1 parent 5433262 commit 23c76ff
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 27 deletions.
15 changes: 7 additions & 8 deletions include/rfl/capnproto/Schema.hpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#ifndef RFL_CAPNPROTO_SCHEMA_HPP_
#define RFL_CAPNPROTO_SCHEMA_HPP_

#include <capnp/schema.h>

#include <type_traits>

#include "../Ref.hpp"
Expand All @@ -13,8 +15,7 @@ class Schema {
public:
using Type = std::remove_cvref_t<T>;

Schema(const std::string& _json_str)
: impl_(Ref<SchemaImpl>::make(_json_str)) {}
Schema(const std::string& _str) : impl_(Ref<SchemaImpl>::make(_str)) {}

static Result<Schema<T>> from_json(const std::string& _json_str) noexcept {
try {
Expand All @@ -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<SchemaImpl> impl_;
Expand Down
30 changes: 11 additions & 19 deletions include/rfl/capnproto/SchemaImpl.hpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#ifndef RFL_CAPNPROTO_SCHEMAIMPL_HPP_
#define RFL_CAPNPROTO_SCHEMAIMPL_HPP_

#include <capnproto.h>
#include <capnp/schema.h>

#include <string>

Expand All @@ -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<capnproto_schema_t> 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<capnp::StructSchema> schema_;
};

} // namespace rfl::capnproto
Expand Down
3 changes: 3 additions & 0 deletions src/reflectcpp_capnproto.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"

22 changes: 22 additions & 0 deletions src/rfl/capnproto/SchemaImpl.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#include "rfl/capnproto/SchemaImpl.hpp"

#include <capnp/schema-parser.h>
#include <capnp/schema.h>

namespace rfl::capnproto {

SchemaImpl::SchemaImpl(const std::string& _str)
: str_(_str), schema_(Box<capnp::StructSchema>::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

0 comments on commit 23c76ff

Please sign in to comment.