capn_proto 0.0.1.alpha.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (57) hide show
  1. data/.gitignore +5 -0
  2. data/Gemfile +2 -0
  3. data/LICENSE +19 -0
  4. data/README.md +42 -0
  5. data/Rakefile +25 -0
  6. data/capn_proto.gemspec +33 -0
  7. data/examples/addressbook.bin +0 -0
  8. data/examples/addressbook.capnp +31 -0
  9. data/examples/create_test_data.py +39 -0
  10. data/examples/example.rb +38 -0
  11. data/ext/capn_proto/.ycm_extra_conf.py +65 -0
  12. data/ext/capn_proto/.ycm_extra_conf.pyc +0 -0
  13. data/ext/capn_proto/class_builder.cc +80 -0
  14. data/ext/capn_proto/class_builder.h +72 -0
  15. data/ext/capn_proto/dynamic_list_reader.cc +52 -0
  16. data/ext/capn_proto/dynamic_list_reader.h +23 -0
  17. data/ext/capn_proto/dynamic_struct_reader.cc +59 -0
  18. data/ext/capn_proto/dynamic_struct_reader.h +23 -0
  19. data/ext/capn_proto/dynamic_value_reader.cc +50 -0
  20. data/ext/capn_proto/dynamic_value_reader.h +13 -0
  21. data/ext/capn_proto/exception.cc +24 -0
  22. data/ext/capn_proto/exception.h +18 -0
  23. data/ext/capn_proto/extconf.rb +29 -0
  24. data/ext/capn_proto/field_list.cc +51 -0
  25. data/ext/capn_proto/field_list.h +23 -0
  26. data/ext/capn_proto/init.cc +34 -0
  27. data/ext/capn_proto/list_nested_node_reader.cc +50 -0
  28. data/ext/capn_proto/list_nested_node_reader.h +24 -0
  29. data/ext/capn_proto/message_reader.cc +30 -0
  30. data/ext/capn_proto/message_reader.h +17 -0
  31. data/ext/capn_proto/nested_node_reader.cc +42 -0
  32. data/ext/capn_proto/nested_node_reader.h +21 -0
  33. data/ext/capn_proto/parsed_schema.cc +58 -0
  34. data/ext/capn_proto/parsed_schema.h +23 -0
  35. data/ext/capn_proto/rb.cc +0 -0
  36. data/ext/capn_proto/rb.h +0 -0
  37. data/ext/capn_proto/ruby_capn_proto.cc +4 -0
  38. data/ext/capn_proto/ruby_capn_proto.h +38 -0
  39. data/ext/capn_proto/schema_node_reader.cc +51 -0
  40. data/ext/capn_proto/schema_node_reader.h +22 -0
  41. data/ext/capn_proto/schema_parser.cc +53 -0
  42. data/ext/capn_proto/schema_parser.h +20 -0
  43. data/ext/capn_proto/stream_fd_message_reader.cc +53 -0
  44. data/ext/capn_proto/stream_fd_message_reader.h +21 -0
  45. data/ext/capn_proto/struct_schema.cc +57 -0
  46. data/ext/capn_proto/struct_schema.h +23 -0
  47. data/ext/capn_proto/util.cc +25 -0
  48. data/ext/capn_proto/util.h +16 -0
  49. data/lib/capn_proto/version.rb +3 -0
  50. data/lib/capn_proto.rb +88 -0
  51. data/media/captain_proto.png +0 -0
  52. data/media/captain_proto_small.png +0 -0
  53. data/spec/addressbook.bin +0 -0
  54. data/spec/addressbook.capnp +31 -0
  55. data/spec/capn_proto_spec.rb +7 -0
  56. data/spec/create_test_data.py +38 -0
  57. metadata +188 -0
@@ -0,0 +1,13 @@
1
+ #ifndef DYNAMIC_VALUE_READER_H
2
+ #define DYNAMIC_VALUE_READER_H
3
+
4
+ #include "ruby_capn_proto.h"
5
+
6
+ namespace ruby_capn_proto {
7
+ class DynamicValueReader {
8
+ public:
9
+ static VALUE to_ruby(capnp::DynamicValue::Reader value, VALUE parent);
10
+ };
11
+ }
12
+
13
+ #endif /* DYNAMIC_VALUE_READER_H */
@@ -0,0 +1,24 @@
1
+ #include "ruby_capn_proto.h"
2
+ #include "exception.h"
3
+ #include "class_builder.h"
4
+ #include "util.h"
5
+
6
+ // VALUE rb_eException;
7
+ // VALUE rb_eStandardError;
8
+ // VALUE rb_eRuntimeError;
9
+
10
+ namespace ruby_capn_proto {
11
+ using WrappedType = kj::Exception;
12
+ VALUE Exception::Class;
13
+
14
+ void Exception::Init() {
15
+ ClassBuilder("Exception", rb_eException).
16
+ store(&Class);
17
+ }
18
+
19
+ VALUE Exception::create(WrappedType exception) {
20
+ VALUE msg = Util::toRubyString(exception.getDescription());
21
+ VALUE rb_exception = rb_funcall(Class, rb_intern("new"), 1, msg);
22
+ return rb_exception;
23
+ }
24
+ }
@@ -0,0 +1,18 @@
1
+ #ifndef EXCEPTION_H
2
+ #define EXCEPTION_H
3
+
4
+ #include "ruby_capn_proto.h"
5
+
6
+ namespace ruby_capn_proto {
7
+ class Exception {
8
+ public:
9
+ using WrappedType = kj::Exception;
10
+ static void Init();
11
+ static VALUE create(WrappedType schema);
12
+
13
+ static VALUE Class;
14
+ };
15
+ }
16
+
17
+
18
+ #endif /* EXCEPTION_H */
@@ -0,0 +1,29 @@
1
+ require 'mkmf'
2
+
3
+ if enable_config('debug')
4
+ CONFIG['CFLAGS'] += " -O0 -ggdb3"
5
+ else
6
+ $CPPFLAGS += " -DNDEBUG"
7
+ end
8
+
9
+ CONFIG['CXX'] = ENV['CXX'] if ENV['CXX']
10
+ CONFIG['CXXFLAGS'] += " #{ENV['CXXFLAGS']}" if ENV['CXXFLAGS']
11
+
12
+ $LDFLAGS += " -lcapnpc"
13
+ $LDFLAGS += " -lcapnp"
14
+ $LDFLAGS += " -lkj"
15
+
16
+ create_makefile('ruby_capn_proto/init')
17
+
18
+ # HACK ATTACK
19
+ # def inreplace(path, search, replace)
20
+ # contents = File.read(path)
21
+ # contents.gsub!(search, replace)
22
+ # File.open(path, "w") do |f|
23
+ # f.write(contents)
24
+ # end
25
+ # end
26
+
27
+ # inreplace("Makefile", "V = 0", "V = 1")
28
+ # inreplace("Makefile", "-O3", "-O0")
29
+ # inreplace("Makefile", "ECHO = $(ECHO1:0=@echo)", "ECHO = @echo")
@@ -0,0 +1,51 @@
1
+ #include "ruby_capn_proto.h"
2
+ #include "field_list.h"
3
+ #include "class_builder.h"
4
+ #include "util.h"
5
+
6
+ namespace ruby_capn_proto {
7
+ using WrappedType = capnp::StructSchema::FieldList;
8
+ VALUE FieldList::Class;
9
+
10
+ void FieldList::Init() {
11
+ ClassBuilder("FieldList", rb_cObject).
12
+ defineAlloc(&alloc).
13
+ defineMethod("size", &size).
14
+ defineMethod("[]", &get).
15
+ store(&Class);
16
+ }
17
+
18
+ void FieldList::free(WrappedType* p) {
19
+ p->~FieldList();
20
+ ruby_xfree(p);
21
+ }
22
+
23
+ VALUE FieldList::alloc(VALUE klass) {
24
+ return Data_Wrap_Struct(klass, NULL, free, ruby_xmalloc(sizeof(WrappedType)));
25
+ }
26
+
27
+ WrappedType* FieldList::unwrap(VALUE self) {
28
+ WrappedType* p;
29
+ Data_Get_Struct(self, WrappedType, p);
30
+ return p;
31
+ }
32
+
33
+ VALUE FieldList::create(WrappedType schema) {
34
+ VALUE rb_obj = alloc(Class);
35
+ WrappedType* wrapped_schema = unwrap(rb_obj);
36
+ *wrapped_schema = kj::mv(schema);
37
+
38
+ return rb_obj;
39
+ }
40
+
41
+ VALUE FieldList::get(VALUE self, VALUE index) {
42
+ auto idx = FIX2INT(index);
43
+ auto list = *unwrap(self);
44
+ return Qnil;
45
+ // return Field::create(list[idx]);
46
+ }
47
+
48
+ VALUE FieldList::size(VALUE self) {
49
+ return INT2FIX(unwrap(self)->size());
50
+ }
51
+ }
@@ -0,0 +1,23 @@
1
+ #ifndef FIELD_LIST_H
2
+ #define FIELD_LIST_H
3
+
4
+ #include "ruby_capn_proto.h"
5
+
6
+ namespace ruby_capn_proto {
7
+ class FieldList {
8
+ public:
9
+ using WrappedType = capnp::StructSchema::FieldList;
10
+ static void Init();
11
+ static VALUE alloc(VALUE klass);
12
+ static VALUE create(WrappedType schema);
13
+ static void free(WrappedType* p);
14
+ static WrappedType* unwrap(VALUE self);
15
+ static VALUE get(VALUE self, VALUE name);
16
+ static VALUE size(VALUE self);
17
+
18
+ static VALUE Class;
19
+ };
20
+ }
21
+
22
+
23
+ #endif /* FIELD_LIST_H */
@@ -0,0 +1,34 @@
1
+ #include "ruby_capn_proto.h"
2
+ #include "parsed_schema.h"
3
+ #include "schema_parser.h"
4
+ #include "schema_node_reader.h"
5
+ #include "struct_schema.h"
6
+ #include "nested_node_reader.h"
7
+ #include "list_nested_node_reader.h"
8
+ #include "dynamic_struct_reader.h"
9
+ #include "dynamic_list_reader.h"
10
+ #include "stream_fd_message_reader.h"
11
+ #include "message_reader.h"
12
+ #include "field_list.h"
13
+
14
+ extern "C" {
15
+ void Init_init();
16
+ }
17
+
18
+ using namespace ruby_capn_proto;
19
+
20
+ extern "C" {
21
+ void Init_init() {
22
+ SchemaParser::Init();
23
+ SchemaNodeReader::Init();
24
+ ParsedSchema::Init();
25
+ StructSchema::Init();
26
+ MessageReader::Init();
27
+ ListNestedNodeReader::Init();
28
+ NestedNodeReader::Init();
29
+ DynamicStructReader::Init();
30
+ DynamicListReader::Init();
31
+ StreamFdMessageReader::Init();
32
+ FieldList::Init();
33
+ }
34
+ }
@@ -0,0 +1,50 @@
1
+ #include "ruby_capn_proto.h"
2
+ #include "list_nested_node_reader.h"
3
+ #include "nested_node_reader.h"
4
+ #include "class_builder.h"
5
+
6
+ namespace ruby_capn_proto {
7
+ using NodeList = ::capnp::List< ::capnp::schema::Node::NestedNode>::Reader;
8
+
9
+ VALUE ListNestedNodeReader::Class;
10
+
11
+ void ListNestedNodeReader::Init() {
12
+ ClassBuilder("ListNestedNodeReader", rb_cObject).
13
+ defineAlloc(&alloc).
14
+ defineMethod("size", &size).
15
+ defineMethod("[]", &get).
16
+ store(&Class);
17
+ }
18
+
19
+ void ListNestedNodeReader::free(NodeList* p) {
20
+ p->~NodeList();
21
+ ruby_xfree(p);
22
+ }
23
+
24
+ VALUE ListNestedNodeReader::alloc(VALUE klass) {
25
+ return Data_Wrap_Struct(klass, NULL, free, ruby_xmalloc(sizeof(NodeList)));
26
+ }
27
+
28
+ NodeList* ListNestedNodeReader::unwrap(VALUE self) {
29
+ NodeList* p;
30
+ Data_Get_Struct(self, NodeList, p);
31
+ return p;
32
+ }
33
+
34
+ VALUE ListNestedNodeReader::create(NodeList schema) {
35
+ VALUE rb_obj = alloc(Class);
36
+ NodeList* wrapped_schema = unwrap(rb_obj);
37
+ *wrapped_schema = kj::mv(schema);
38
+ return rb_obj;
39
+ }
40
+
41
+ VALUE ListNestedNodeReader::size(VALUE self) {
42
+ return INT2FIX(unwrap(self)->size());
43
+ }
44
+
45
+ VALUE ListNestedNodeReader::get(VALUE self, VALUE index) {
46
+ auto idx = FIX2INT(index);
47
+ auto list = *unwrap(self);
48
+ return NestedNodeReader::create(list[idx]);
49
+ }
50
+ }
@@ -0,0 +1,24 @@
1
+ #ifndef LIST_NESTED_NODE_READER_H
2
+ #define LIST_NESTED_NODE_READER_H
3
+
4
+ #include "ruby_capn_proto.h"
5
+
6
+ namespace ruby_capn_proto {
7
+ class ListNestedNodeReader {
8
+ public:
9
+ using NodeList = ::capnp::List< ::capnp::schema::Node::NestedNode>::Reader;
10
+
11
+ static void Init();
12
+ static VALUE initialize(VALUE klass);
13
+ static VALUE alloc(VALUE klass);
14
+ static void free(NodeList* p);
15
+ static NodeList* unwrap(VALUE self);
16
+ static VALUE create(NodeList reader);
17
+ static VALUE size(VALUE self);
18
+ static VALUE get(VALUE self, VALUE index);
19
+
20
+ static VALUE Class;
21
+ };
22
+ }
23
+
24
+ #endif /* LIST_NESTED_NODE_READER_H */
@@ -0,0 +1,30 @@
1
+ #include "ruby_capn_proto.h"
2
+ #include "message_reader.h"
3
+ #include "class_builder.h"
4
+ #include "struct_schema.h"
5
+ #include "dynamic_struct_reader.h"
6
+ #include "util.h"
7
+
8
+ namespace ruby_capn_proto {
9
+ using WrappedType = capnp::MessageReader;
10
+ VALUE MessageReader::Class;
11
+
12
+ void MessageReader::Init() {
13
+ ClassBuilder("MessageReader", rb_cObject).
14
+ store(&Class);
15
+ }
16
+
17
+ // XXX
18
+ // I wanted to use inheritance here for #get_root,
19
+ // but this segfaults unless I reinterpret_cast to the right
20
+ // subclass of capnp::MessageReader. Whish I know more about
21
+ // the C++ object model :(...
22
+ // Worst case scenario, I could probably use tempalates...
23
+ // If only I had experience with them.
24
+
25
+ WrappedType* MessageReader::unwrap(VALUE self) {
26
+ WrappedType* p;
27
+ Data_Get_Struct(self, WrappedType, p);
28
+ return p;
29
+ }
30
+ }
@@ -0,0 +1,17 @@
1
+ #ifndef MESSAGE_READER_H
2
+ #define MESSAGE_READER_H
3
+
4
+ #include "ruby_capn_proto.h"
5
+
6
+ namespace ruby_capn_proto {
7
+ class MessageReader {
8
+ public:
9
+ using WrappedType = capnp::MessageReader;
10
+ static void Init();
11
+ static WrappedType* unwrap(VALUE self);
12
+
13
+ static VALUE Class;
14
+ };
15
+ }
16
+
17
+ #endif /* MESSAGE_READER_H */
@@ -0,0 +1,42 @@
1
+ #include "ruby_capn_proto.h"
2
+ #include "nested_node_reader.h"
3
+ #include "class_builder.h"
4
+ #include "util.h"
5
+
6
+ namespace ruby_capn_proto {
7
+ using WrappedType = capnp::schema::Node::NestedNode::Reader;
8
+ VALUE NestedNodeReader::Class;
9
+
10
+ void NestedNodeReader::Init() {
11
+ ClassBuilder("NestedNodeReader", rb_cObject).
12
+ defineAlloc(&alloc).
13
+ defineMethod("name", &name).
14
+ store(&Class);
15
+ }
16
+
17
+ VALUE NestedNodeReader::alloc(VALUE klass) {
18
+ return Data_Wrap_Struct(klass, NULL, free, ruby_xmalloc(sizeof(WrappedType)));
19
+ }
20
+
21
+ VALUE NestedNodeReader::create(WrappedType reader) {
22
+ VALUE rb_obj = alloc(Class);
23
+ WrappedType* wrapped = unwrap(rb_obj);
24
+ *wrapped = kj::mv(reader);
25
+ return rb_obj;
26
+ }
27
+
28
+ void NestedNodeReader::free(WrappedType* p) {
29
+ p->~Reader();
30
+ ruby_xfree(p);
31
+ }
32
+
33
+ WrappedType* NestedNodeReader::unwrap(VALUE self) {
34
+ WrappedType* p;
35
+ Data_Get_Struct(self, WrappedType, p);
36
+ return p;
37
+ }
38
+
39
+ VALUE NestedNodeReader::name(VALUE self) {
40
+ return Util::toRubyString(unwrap(self)->getName());
41
+ }
42
+ }
@@ -0,0 +1,21 @@
1
+ #ifndef NESTED_NODE_READER_H
2
+ #define NESTED_NODE_READER_H
3
+
4
+ #include "ruby_capn_proto.h"
5
+
6
+ namespace ruby_capn_proto {
7
+ class NestedNodeReader {
8
+ public:
9
+ using WrappedType = capnp::schema::Node::NestedNode::Reader;
10
+ static void Init();
11
+ static VALUE alloc(VALUE klass);
12
+ static VALUE create(WrappedType reader);
13
+ static void free(WrappedType* p);
14
+ static WrappedType* unwrap(VALUE self);
15
+ static VALUE name(VALUE self);
16
+
17
+ static VALUE Class;
18
+ };
19
+ }
20
+
21
+ #endif /* NESTED_NODE_READER_H */
@@ -0,0 +1,58 @@
1
+ #include "ruby_capn_proto.h"
2
+ #include "parsed_schema.h"
3
+ #include "schema_node_reader.h"
4
+ #include "struct_schema.h"
5
+ #include "class_builder.h"
6
+ #include "util.h"
7
+
8
+ namespace ruby_capn_proto {
9
+ using WrappedType = capnp::ParsedSchema;
10
+ VALUE ParsedSchema::Class;
11
+
12
+ void ParsedSchema::Init() {
13
+ ClassBuilder("ParsedSchema", rb_cObject).
14
+ defineAlloc(&alloc).
15
+ defineMethod("get_proto", &get_proto).
16
+ defineMethod("get_nested", &get_nested).
17
+ defineMethod("as_struct", &as_struct).
18
+ store(&Class);
19
+ }
20
+
21
+ void ParsedSchema::free(WrappedType* p) {
22
+ p->~ParsedSchema();
23
+ ruby_xfree(p);
24
+ }
25
+
26
+ VALUE ParsedSchema::alloc(VALUE klass) {
27
+ return Data_Wrap_Struct(klass, NULL, free, ruby_xmalloc(sizeof(WrappedType)));
28
+ }
29
+
30
+ WrappedType* ParsedSchema::unwrap(VALUE self) {
31
+ WrappedType* p;
32
+ Data_Get_Struct(self, WrappedType, p);
33
+ return p;
34
+ }
35
+
36
+ VALUE ParsedSchema::create(VALUE parent, WrappedType schema) {
37
+ VALUE rb_obj = alloc(Class);
38
+ WrappedType* wrapped_schema = unwrap(rb_obj);
39
+ *wrapped_schema = kj::mv(schema);
40
+
41
+ rb_iv_set(rb_obj, "parent", parent);
42
+
43
+ return rb_obj;
44
+ }
45
+
46
+ VALUE ParsedSchema::get_proto(VALUE self) {
47
+ return SchemaNodeReader::create(unwrap(self)->getProto());
48
+ }
49
+
50
+ VALUE ParsedSchema::get_nested(VALUE self, VALUE rb_name) {
51
+ auto name = Util::toString(rb_name);
52
+ return create(self, unwrap(self)->getNested(name));
53
+ }
54
+
55
+ VALUE ParsedSchema::as_struct(VALUE self) {
56
+ return StructSchema::create(unwrap(self)->asStruct());
57
+ }
58
+ }
@@ -0,0 +1,23 @@
1
+ #ifndef PARSED_SCHEMA_H
2
+ #define PARSED_SCHEMA_H
3
+
4
+ #include "ruby_capn_proto.h"
5
+
6
+ namespace ruby_capn_proto {
7
+ class ParsedSchema {
8
+ public:
9
+ using WrappedType = capnp::ParsedSchema;
10
+ static void Init();
11
+ static VALUE alloc(VALUE klass);
12
+ static VALUE create(VALUE parent, WrappedType schema);
13
+ static void free(WrappedType* p);
14
+ static WrappedType* unwrap(VALUE self);
15
+ static VALUE get_proto(VALUE self);
16
+ static VALUE get_nested(VALUE self, VALUE name);
17
+ static VALUE as_struct(VALUE name);
18
+
19
+ static VALUE Class;
20
+ };
21
+ }
22
+
23
+ #endif /* PARSED_SCHEMA_H */
File without changes
File without changes
@@ -0,0 +1,4 @@
1
+ #include "ruby_capn_proto.h"
2
+
3
+ namespace ruby_capn_proto {
4
+ }
@@ -0,0 +1,38 @@
1
+ #ifndef RUBY_CAPN_PROTO_H
2
+ #define RUBY_CAPN_PROTO_H
3
+
4
+ #include <iostream>
5
+ #include <vector>
6
+
7
+ #include <kj/common.h>
8
+ #include <kj/array.h>
9
+
10
+ #include <capnp/serialize.h>
11
+ #include <capnp/serialize-packed.h>
12
+ #include <capnp/message.h>
13
+ #include <capnp/schema-parser.h>
14
+ #include <capnp/schema.h>
15
+ #include <capnp/message.h>
16
+ #include <capnp/dynamic.h>
17
+
18
+ #include <ruby.h>
19
+ #ifdef __
20
+ #undef __
21
+ #endif
22
+ #ifdef HAVE_RUBY_ENCODING_H
23
+ #include "ruby/encoding.h"
24
+ #endif
25
+
26
+ #if defined(RUBY_METHOD_FUNC)
27
+ # undef RUBY_METHOD_FUNC
28
+ # if RUBY_VERSION_CODE <= 166
29
+ extern "C" typedef VALUE (*RUBY_METHOD_FUNC)();
30
+ # else
31
+ extern "C" typedef VALUE (*RUBY_METHOD_FUNC)(ANYARGS);
32
+ # endif
33
+ #endif
34
+
35
+ namespace ruby_capn_proto {
36
+ }
37
+
38
+ #endif /* RUBY_CAPN_PROTO_H */
@@ -0,0 +1,51 @@
1
+ #include "ruby_capn_proto.h"
2
+ #include "schema_node_reader.h"
3
+ #include "list_nested_node_reader.h"
4
+ #include "class_builder.h"
5
+
6
+ namespace ruby_capn_proto {
7
+ using WrappedType = capnp::schema::Node::Reader;
8
+ VALUE SchemaNodeReader::Class;
9
+
10
+ void SchemaNodeReader::Init() {
11
+ ClassBuilder("SchemaNodeReader", rb_cObject).
12
+ defineAlloc(&alloc).
13
+ defineMethod("nested_nodes", &get_nested_nodes).
14
+ defineMethod("struct?", &is_struct).
15
+ store(&Class);
16
+ }
17
+
18
+ VALUE SchemaNodeReader::alloc(VALUE klass) {
19
+ return Data_Wrap_Struct(klass, NULL, free, ruby_xmalloc(sizeof(WrappedType)));
20
+ }
21
+
22
+ VALUE SchemaNodeReader::create(WrappedType reader) {
23
+ VALUE rb_obj = alloc(Class);
24
+ WrappedType* wrapped = unwrap(rb_obj);
25
+ *wrapped = kj::mv(reader);
26
+ return rb_obj;
27
+ }
28
+
29
+ void SchemaNodeReader::free(WrappedType* p) {
30
+ p->~Reader();
31
+ ruby_xfree(p);
32
+ }
33
+
34
+ WrappedType* SchemaNodeReader::unwrap(VALUE self) {
35
+ WrappedType* p;
36
+ Data_Get_Struct(self, WrappedType, p);
37
+ return p;
38
+ }
39
+
40
+ VALUE SchemaNodeReader::get_nested_nodes(VALUE self) {
41
+ return ListNestedNodeReader::create(unwrap(self)->getNestedNodes());
42
+ }
43
+
44
+ VALUE SchemaNodeReader::is_struct(VALUE self) {
45
+ return unwrap(self)->isStruct() ? Qtrue : Qfalse;
46
+ }
47
+
48
+ VALUE SchemaNodeReader::is_enum(VALUE self) {
49
+ return unwrap(self)->isEnum() ? Qtrue : Qfalse;
50
+ }
51
+ }
@@ -0,0 +1,22 @@
1
+ #ifndef SCHEMA_NODE_READER_H
2
+ #define SCHEMA_NODE_READER_H
3
+
4
+ #include "ruby_capn_proto.h"
5
+
6
+ namespace ruby_capn_proto {
7
+ class SchemaNodeReader {
8
+ public:
9
+ static void Init();
10
+ static VALUE alloc(VALUE klass);
11
+ static VALUE create(capnp::schema::Node::Reader reader);
12
+ static void free(capnp::schema::Node::Reader* p);
13
+ static capnp::schema::Node::Reader* unwrap(VALUE self);
14
+ static VALUE get_nested_nodes(VALUE self);
15
+ static VALUE is_struct(VALUE self);
16
+ static VALUE is_enum(VALUE self);
17
+
18
+ static VALUE Class;
19
+ };
20
+ }
21
+
22
+ #endif /* SCHEMA_NODE_READER_H */
@@ -0,0 +1,53 @@
1
+ #include "ruby_capn_proto.h"
2
+ #include "schema_parser.h"
3
+ #include "parsed_schema.h"
4
+ #include "class_builder.h"
5
+ #include "util.h"
6
+
7
+ namespace ruby_capn_proto {
8
+ VALUE SchemaParser::Class;
9
+ using WrappedType = capnp::SchemaParser;
10
+
11
+ void SchemaParser::Init() {
12
+ ClassBuilder("SchemaParser", rb_cObject).
13
+ defineAlloc(&alloc).
14
+ defineMethod("initialize", &initialize).
15
+ defineMethod("parse_disk_file", &parse_disk_file).
16
+ store(&Class);
17
+ }
18
+
19
+ void SchemaParser::free(SchemaParser* p) {
20
+ p->~SchemaParser();
21
+ ruby_xfree(p);
22
+ }
23
+
24
+ VALUE SchemaParser::alloc(VALUE klass) {
25
+ return Data_Wrap_Struct(klass, NULL, free, ruby_xmalloc(sizeof(WrappedType)));
26
+ }
27
+
28
+ WrappedType* SchemaParser::unwrap(VALUE self) {
29
+ WrappedType* p;
30
+ Data_Get_Struct(self, WrappedType, p);
31
+ return p;
32
+ }
33
+
34
+ VALUE SchemaParser::initialize(VALUE self) {
35
+ WrappedType* p = unwrap(self);
36
+ new (p) WrappedType();
37
+
38
+ return Qnil;
39
+ }
40
+
41
+ VALUE SchemaParser::parse_disk_file(VALUE self, VALUE rb_display_name, VALUE rb_disk_path, VALUE rb_import_path) {
42
+ auto imports = Util::toStringArray(rb_import_path);
43
+ auto importsPtrs = KJ_MAP(s, imports) -> kj::StringPtr { return s; };
44
+
45
+ auto display_name = Util::toString(rb_display_name);
46
+ auto schema = unwrap(self)->parseDiskFile(
47
+ display_name,
48
+ StringValueCStr(rb_disk_path),
49
+ importsPtrs
50
+ );
51
+ return ParsedSchema::create(self, schema);
52
+ }
53
+ }
@@ -0,0 +1,20 @@
1
+ #ifndef SCHEMA_PARSER_H
2
+ #define SCHEMA_PARSER_H
3
+
4
+ #include "ruby_capn_proto.h"
5
+
6
+ namespace ruby_capn_proto {
7
+ class SchemaParser {
8
+ public:
9
+ static void Init();
10
+ static VALUE parse_disk_file(VALUE self, VALUE display_name, VALUE disk_path, VALUE import_path);
11
+ static VALUE initialize(VALUE klass);
12
+ static VALUE alloc(VALUE klass);
13
+ static void free(SchemaParser* p);
14
+ static capnp::SchemaParser* unwrap(VALUE self);
15
+
16
+ static VALUE Class;
17
+ };
18
+ }
19
+
20
+ #endif /* SCHEMA_PARSER_H */