capn_proto-rpc 0.1.1.alpha.rpc
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +10 -0
- data/.travis.yml +19 -0
- data/Gemfile +2 -0
- data/LICENSE +19 -0
- data/README.md +249 -0
- data/Rakefile +44 -0
- data/capn_proto.gemspec +37 -0
- data/examples/addressbook.bin +0 -0
- data/examples/addressbook.capnp +31 -0
- data/examples/create_test_data.py +39 -0
- data/examples/example.rb +38 -0
- data/ext/capn_proto/.ycm_extra_conf.py +65 -0
- data/ext/capn_proto/EzRpc_client.cc +52 -0
- data/ext/capn_proto/EzRpc_client.h +21 -0
- data/ext/capn_proto/EzRpc_server.cc +79 -0
- data/ext/capn_proto/EzRpc_server.h +32 -0
- data/ext/capn_proto/call_context.cc +67 -0
- data/ext/capn_proto/call_context.h +25 -0
- data/ext/capn_proto/capability_client.cc +47 -0
- data/ext/capn_proto/capability_client.h +21 -0
- data/ext/capn_proto/class_builder.cc +80 -0
- data/ext/capn_proto/class_builder.h +72 -0
- data/ext/capn_proto/cxx_compiler.rb +126 -0
- data/ext/capn_proto/dynamic_capability_client.cc +73 -0
- data/ext/capn_proto/dynamic_capability_client.h +22 -0
- data/ext/capn_proto/dynamic_list_builder.cc +123 -0
- data/ext/capn_proto/dynamic_list_builder.h +27 -0
- data/ext/capn_proto/dynamic_list_reader.cc +63 -0
- data/ext/capn_proto/dynamic_list_reader.h +25 -0
- data/ext/capn_proto/dynamic_object_builder.cc +57 -0
- data/ext/capn_proto/dynamic_object_builder.h +22 -0
- data/ext/capn_proto/dynamic_object_reader.cc +56 -0
- data/ext/capn_proto/dynamic_object_reader.h +22 -0
- data/ext/capn_proto/dynamic_struct_builder.cc +180 -0
- data/ext/capn_proto/dynamic_struct_builder.h +34 -0
- data/ext/capn_proto/dynamic_struct_reader.cc +69 -0
- data/ext/capn_proto/dynamic_struct_reader.h +25 -0
- data/ext/capn_proto/dynamic_value_builder.cc +53 -0
- data/ext/capn_proto/dynamic_value_builder.h +13 -0
- data/ext/capn_proto/dynamic_value_reader.cc +55 -0
- data/ext/capn_proto/dynamic_value_reader.h +13 -0
- data/ext/capn_proto/exception.cc +34 -0
- data/ext/capn_proto/exception.h +20 -0
- data/ext/capn_proto/extconf.rb +32 -0
- data/ext/capn_proto/field_list.cc +51 -0
- data/ext/capn_proto/field_list.h +23 -0
- data/ext/capn_proto/flat_array_message_reader.cc +61 -0
- data/ext/capn_proto/flat_array_message_reader.h +21 -0
- data/ext/capn_proto/init.cc +71 -0
- data/ext/capn_proto/interface_method.cc +38 -0
- data/ext/capn_proto/interface_method.h +21 -0
- data/ext/capn_proto/interface_schema.cc +51 -0
- data/ext/capn_proto/interface_schema.h +26 -0
- data/ext/capn_proto/list_nested_node_reader.cc +53 -0
- data/ext/capn_proto/list_nested_node_reader.h +24 -0
- data/ext/capn_proto/malloc_message_builder.cc +51 -0
- data/ext/capn_proto/malloc_message_builder.h +21 -0
- data/ext/capn_proto/message_builder.cc +22 -0
- data/ext/capn_proto/message_builder.h +17 -0
- data/ext/capn_proto/message_reader.cc +30 -0
- data/ext/capn_proto/message_reader.h +17 -0
- data/ext/capn_proto/nested_node_reader.cc +42 -0
- data/ext/capn_proto/nested_node_reader.h +21 -0
- data/ext/capn_proto/parsed_schema.cc +65 -0
- data/ext/capn_proto/parsed_schema.h +24 -0
- data/ext/capn_proto/rb.cc +0 -0
- data/ext/capn_proto/rb.h +0 -0
- data/ext/capn_proto/remote_promise.cc +116 -0
- data/ext/capn_proto/remote_promise.h +30 -0
- data/ext/capn_proto/ruby_capability_server.cc +23 -0
- data/ext/capn_proto/ruby_capability_server.h +21 -0
- data/ext/capn_proto/ruby_capn_proto.cc +4 -0
- data/ext/capn_proto/ruby_capn_proto.h +42 -0
- data/ext/capn_proto/schema_node_reader.cc +59 -0
- data/ext/capn_proto/schema_node_reader.h +23 -0
- data/ext/capn_proto/schema_parser.cc +61 -0
- data/ext/capn_proto/schema_parser.h +20 -0
- data/ext/capn_proto/stream_fd_message_reader.cc +59 -0
- data/ext/capn_proto/stream_fd_message_reader.h +21 -0
- data/ext/capn_proto/struct_schema.cc +59 -0
- data/ext/capn_proto/struct_schema.h +23 -0
- data/ext/capn_proto/util.cc +30 -0
- data/ext/capn_proto/util.h +17 -0
- data/lib/capn_proto.rb +231 -0
- data/lib/capn_proto/version.rb +3 -0
- data/media/captain_proto.png +0 -0
- data/media/captain_proto_small.png +0 -0
- data/spec/addressbook.bin +0 -0
- data/spec/addressbook.capnp +31 -0
- data/spec/capn_proto_spec.rb +80 -0
- data/spec/create_test_data.py +38 -0
- data/spec/spec_helper.rb +8 -0
- data/tests/hidraCordatus.capnp +16 -0
- data/tests/hidraCordatusEmployer.rb +33 -0
- data/tests/hidraCordatusMaster.rb +51 -0
- metadata +216 -0
@@ -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,65 @@
|
|
1
|
+
#include "ruby_capn_proto.h"
|
2
|
+
#include "parsed_schema.h"
|
3
|
+
#include "schema_node_reader.h"
|
4
|
+
#include "struct_schema.h"
|
5
|
+
#include "interface_schema.h"
|
6
|
+
#include "class_builder.h"
|
7
|
+
#include "util.h"
|
8
|
+
|
9
|
+
namespace ruby_capn_proto {
|
10
|
+
using WrappedType = capnp::ParsedSchema;
|
11
|
+
VALUE ParsedSchema::Class;
|
12
|
+
|
13
|
+
void ParsedSchema::Init() {
|
14
|
+
ClassBuilder("ParsedSchema", rb_cObject).
|
15
|
+
defineAlloc(&alloc).
|
16
|
+
defineMethod("get_proto", &get_proto).
|
17
|
+
defineMethod("get_nested", &get_nested).
|
18
|
+
defineMethod("as_struct", &as_struct).
|
19
|
+
defineMethod("as_interface", &as_interface).
|
20
|
+
store(&Class);
|
21
|
+
}
|
22
|
+
|
23
|
+
void ParsedSchema::free(WrappedType* p) {
|
24
|
+
p->~ParsedSchema();
|
25
|
+
ruby_xfree(p);
|
26
|
+
}
|
27
|
+
|
28
|
+
VALUE ParsedSchema::alloc(VALUE klass) {
|
29
|
+
return Data_Wrap_Struct(klass, NULL, free, ruby_xmalloc(sizeof(WrappedType)));
|
30
|
+
}
|
31
|
+
|
32
|
+
WrappedType* ParsedSchema::unwrap(VALUE self) {
|
33
|
+
WrappedType* p;
|
34
|
+
Data_Get_Struct(self, WrappedType, p);
|
35
|
+
return p;
|
36
|
+
}
|
37
|
+
|
38
|
+
VALUE ParsedSchema::create(WrappedType schema, VALUE parent) {
|
39
|
+
VALUE rb_obj = alloc(Class);
|
40
|
+
WrappedType* wrapped_schema = unwrap(rb_obj);
|
41
|
+
*wrapped_schema = kj::mv(schema);
|
42
|
+
|
43
|
+
rb_iv_set(rb_obj, "parent", parent);
|
44
|
+
|
45
|
+
return rb_obj;
|
46
|
+
}
|
47
|
+
|
48
|
+
VALUE ParsedSchema::get_proto(VALUE self) {
|
49
|
+
return SchemaNodeReader::create(unwrap(self)->getProto(), self);
|
50
|
+
}
|
51
|
+
|
52
|
+
VALUE ParsedSchema::get_nested(VALUE self, VALUE rb_name) {
|
53
|
+
auto name = Util::toString(rb_name);
|
54
|
+
return create(unwrap(self)->getNested(name), self);
|
55
|
+
}
|
56
|
+
|
57
|
+
VALUE ParsedSchema::as_struct(VALUE self) {
|
58
|
+
return StructSchema::create(unwrap(self)->asStruct(), self);
|
59
|
+
}
|
60
|
+
|
61
|
+
VALUE ParsedSchema::as_interface(VALUE self) {
|
62
|
+
return InterfaceSchema::create(unwrap(self)->asInterface(), self);
|
63
|
+
}
|
64
|
+
|
65
|
+
}
|
@@ -0,0 +1,24 @@
|
|
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(WrappedType schema, VALUE parent);
|
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 self);
|
18
|
+
static VALUE as_interface(VALUE self);
|
19
|
+
|
20
|
+
static VALUE Class;
|
21
|
+
};
|
22
|
+
}
|
23
|
+
|
24
|
+
#endif /* PARSED_SCHEMA_H */
|
File without changes
|
data/ext/capn_proto/rb.h
ADDED
File without changes
|
@@ -0,0 +1,116 @@
|
|
1
|
+
#include "ruby_capn_proto.h"
|
2
|
+
#include "remote_promise.h"
|
3
|
+
#include "dynamic_value_builder.h"
|
4
|
+
#include "dynamic_struct_builder.h"
|
5
|
+
#include "dynamic_struct_reader.h"
|
6
|
+
#include "EzRpc_client.h"
|
7
|
+
#include "interface_method.h"
|
8
|
+
#include "exception.h"
|
9
|
+
#include "class_builder.h"
|
10
|
+
#include <ruby/thread.h>
|
11
|
+
#include "util.h"
|
12
|
+
|
13
|
+
namespace ruby_capn_proto {
|
14
|
+
using WrappedType = capnp::RemotePromise<capnp::DynamicStruct>;
|
15
|
+
VALUE RemotePromise::Class;
|
16
|
+
|
17
|
+
void RemotePromise::Init() {
|
18
|
+
|
19
|
+
ClassBuilder("RemotePromise", rb_cObject).
|
20
|
+
defineAlloc(&alloc).
|
21
|
+
defineMethod("request_and_send" , &request_and_send).
|
22
|
+
defineMethod("wait" , &wait).
|
23
|
+
store(&Class);
|
24
|
+
}
|
25
|
+
|
26
|
+
void RemotePromise::free(WrappedType* p) {
|
27
|
+
p->~RemotePromise();
|
28
|
+
ruby_xfree(p);
|
29
|
+
}
|
30
|
+
|
31
|
+
VALUE RemotePromise::alloc(VALUE klass) {
|
32
|
+
return Data_Wrap_Struct(klass, NULL, free, ruby_xmalloc(sizeof(WrappedType)));
|
33
|
+
}
|
34
|
+
|
35
|
+
WrappedType* RemotePromise::unwrap(VALUE self) {
|
36
|
+
WrappedType* p;
|
37
|
+
Data_Get_Struct(self, WrappedType, p);
|
38
|
+
return p;
|
39
|
+
}
|
40
|
+
|
41
|
+
VALUE RemotePromise::create(WrappedType& remote_promise) {
|
42
|
+
|
43
|
+
VALUE rb_obj = alloc(Class);
|
44
|
+
WrappedType* rb_promise = unwrap(rb_obj);
|
45
|
+
new (rb_promise) WrappedType(kj::mv(remote_promise));
|
46
|
+
|
47
|
+
return rb_obj;
|
48
|
+
}
|
49
|
+
|
50
|
+
VALUE RemotePromise::request_and_send(VALUE self, VALUE name_struct, VALUE method, VALUE data){
|
51
|
+
VALUE rb_client = rb_iv_get(self,"client");
|
52
|
+
try{
|
53
|
+
auto pipelinedClient = unwrap(self)->get(Util::toString(name_struct)).releaseAs<capnp::DynamicCapability>();
|
54
|
+
auto request = pipelinedClient.newRequest(*InterfaceMethod::unwrap(method));
|
55
|
+
setParam(&request,data);
|
56
|
+
auto promise = request.send();
|
57
|
+
VALUE new_remote_promise = create(promise);
|
58
|
+
return new_remote_promise;
|
59
|
+
}catch( kj::Exception t){
|
60
|
+
Exception::raise(t);
|
61
|
+
}
|
62
|
+
}
|
63
|
+
|
64
|
+
VALUE RemotePromise::wait(VALUE self, VALUE ezrpclient){
|
65
|
+
VALUE client = ezrpclient;
|
66
|
+
|
67
|
+
waitpacket p;
|
68
|
+
p.prom = unwrap(self);
|
69
|
+
p.client = EzRpcCapabilityClient::unwrap(client);
|
70
|
+
p.response = NULL;
|
71
|
+
|
72
|
+
// call waitIntern releasing the GIL
|
73
|
+
rb_thread_call_without_gvl(waitIntern, &p, RUBY_UBF_IO , 0);
|
74
|
+
return DynamicStructReader::create(*p.response,Qnil);
|
75
|
+
}
|
76
|
+
|
77
|
+
void * RemotePromise::waitIntern(void * p){
|
78
|
+
try {
|
79
|
+
waitpacket* pkt = (waitpacket*) p;
|
80
|
+
auto& waitscope = pkt->client->getWaitScope();
|
81
|
+
pkt->response = new capnp::Response<capnp::DynamicStruct>(pkt->prom->wait(waitscope));
|
82
|
+
}catch(kj::Exception t){
|
83
|
+
// adquire the lock to raise an exception on ruby
|
84
|
+
rb_thread_call_with_gvl(&Exception::raise,&t);
|
85
|
+
}
|
86
|
+
}
|
87
|
+
|
88
|
+
void RemotePromise::setParam(capnp::Request<capnp::DynamicStruct, capnp::DynamicStruct>* request, VALUE arys){
|
89
|
+
VALUE mainIter = rb_ary_pop(arys); // mainIter is now a array
|
90
|
+
while(mainIter != Qnil ){
|
91
|
+
|
92
|
+
VALUE val = rb_ary_pop(mainIter); // value to assign
|
93
|
+
VALUE last = rb_ary_pop(mainIter); // name of the field to assign to val
|
94
|
+
VALUE temp = rb_ary_shift(mainIter); // just to iterate
|
95
|
+
|
96
|
+
capnp::DynamicStruct::Builder builder = *request;
|
97
|
+
|
98
|
+
// follow the nodes indicated by the array
|
99
|
+
while( temp != Qnil && temp != last){
|
100
|
+
try{
|
101
|
+
builder = *DynamicStructBuilder::unwrap(DynamicValueBuilder::to_ruby(request->get(Util::toString(temp)),Qnil));
|
102
|
+
temp = rb_ary_shift(mainIter);
|
103
|
+
}catch(kj::Exception t){
|
104
|
+
Exception::raise(t);
|
105
|
+
}
|
106
|
+
}
|
107
|
+
|
108
|
+
// when arrived to last node make the assignation
|
109
|
+
VALUE rb_struct = DynamicStructBuilder::create(builder,Qnil,Qfalse);
|
110
|
+
DynamicStructBuilder::set(rb_struct,last,val);
|
111
|
+
|
112
|
+
mainIter = rb_ary_pop(arys);
|
113
|
+
}
|
114
|
+
}
|
115
|
+
|
116
|
+
}
|
@@ -0,0 +1,30 @@
|
|
1
|
+
#ifndef REMOTE_PROMISE_H
|
2
|
+
#define REMOTE_PROMISE_H
|
3
|
+
|
4
|
+
#include "ruby_capn_proto.h"
|
5
|
+
|
6
|
+
namespace ruby_capn_proto {
|
7
|
+
typedef struct _waitpacket {
|
8
|
+
capnp::EzRpcClient* client;
|
9
|
+
capnp::RemotePromise<capnp::DynamicStruct>* prom;
|
10
|
+
capnp::Response<capnp::DynamicStruct>* response;
|
11
|
+
} waitpacket;
|
12
|
+
|
13
|
+
class RemotePromise {
|
14
|
+
public:
|
15
|
+
using WrappedType = capnp::RemotePromise<capnp::DynamicStruct>;
|
16
|
+
static void Init();
|
17
|
+
static VALUE alloc(VALUE klass);
|
18
|
+
static VALUE create(WrappedType& promise);
|
19
|
+
static void free(WrappedType* p);
|
20
|
+
static WrappedType* unwrap(VALUE self);
|
21
|
+
static VALUE request_and_send(VALUE self, VALUE struct_name, VALUE method, VALUE data);
|
22
|
+
static void setParam(capnp::Request<capnp::DynamicStruct, capnp::DynamicStruct>* request, VALUE arrays);
|
23
|
+
static VALUE wait(VALUE self, VALUE ezrpclient);
|
24
|
+
static void * waitIntern(void * p);
|
25
|
+
static VALUE Class;
|
26
|
+
};
|
27
|
+
}
|
28
|
+
|
29
|
+
|
30
|
+
#endif /* REMOTE_PROMISE_H */
|
@@ -0,0 +1,23 @@
|
|
1
|
+
#include "ruby_capn_proto.h"
|
2
|
+
#include "call_context.h"
|
3
|
+
#include "ruby_capability_server.h"
|
4
|
+
#include <ruby/thread.h>
|
5
|
+
#include "util.h"
|
6
|
+
|
7
|
+
namespace capnp {
|
8
|
+
kj::Promise<void> RubyCapabilityServer::call(InterfaceSchema::Method method, CallContext<DynamicStruct, DynamicStruct> context) {
|
9
|
+
// just a dummy c++ server that calls methods on a passed ruby server.
|
10
|
+
|
11
|
+
callPacket packet;
|
12
|
+
packet.methodName = method.getProto().getName().cStr();
|
13
|
+
packet.rb_context = ruby_capn_proto::CallContext::create(context);
|
14
|
+
packet.rb_server = this->server;
|
15
|
+
rb_thread_call_with_gvl(this->rbCall, &packet);
|
16
|
+
return kj::READY_NOW;
|
17
|
+
}
|
18
|
+
|
19
|
+
void * RubyCapabilityServer::rbCall(void * p){
|
20
|
+
auto * packet = (callPacket*) p;
|
21
|
+
rb_funcall( packet->rb_server, rb_intern( packet->methodName ), 1, packet->rb_context );
|
22
|
+
}
|
23
|
+
}
|
@@ -0,0 +1,21 @@
|
|
1
|
+
#include "ruby_capn_proto.h"
|
2
|
+
|
3
|
+
namespace capnp {
|
4
|
+
// helper struct to use rb_thread_call_with_gvl
|
5
|
+
typedef struct _callPacket {
|
6
|
+
const char* methodName;
|
7
|
+
VALUE rb_context;
|
8
|
+
VALUE rb_server;
|
9
|
+
} callPacket;
|
10
|
+
|
11
|
+
class RubyCapabilityServer : public DynamicCapability::Server {
|
12
|
+
|
13
|
+
private:
|
14
|
+
VALUE server;
|
15
|
+
|
16
|
+
public:
|
17
|
+
RubyCapabilityServer( InterfaceSchema interface, VALUE server ): DynamicCapability::Server(interface) { this->server = server; }
|
18
|
+
kj::Promise<void> call( InterfaceSchema::Method method, CallContext<DynamicStruct, DynamicStruct> context );
|
19
|
+
static void * rbCall(void * p); // to be called with rb_thread_call_with_gvl
|
20
|
+
};
|
21
|
+
}
|
@@ -0,0 +1,42 @@
|
|
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
|
+
#include <kj/debug.h>
|
10
|
+
|
11
|
+
#include <capnp/any.h>
|
12
|
+
#include <capnp/serialize.h>
|
13
|
+
#include <capnp/serialize-packed.h>
|
14
|
+
#include <capnp/message.h>
|
15
|
+
#include <capnp/schema-parser.h>
|
16
|
+
#include <capnp/schema.h>
|
17
|
+
#include <capnp/message.h>
|
18
|
+
#include <capnp/dynamic.h>
|
19
|
+
|
20
|
+
#include <capnp/ez-rpc.h>
|
21
|
+
|
22
|
+
#include <ruby.h>
|
23
|
+
#ifdef __
|
24
|
+
#undef __
|
25
|
+
#endif
|
26
|
+
#ifdef HAVE_RUBY_ENCODING_H
|
27
|
+
#include "ruby/encoding.h"
|
28
|
+
#endif
|
29
|
+
|
30
|
+
#if defined(RUBY_METHOD_FUNC)
|
31
|
+
# undef RUBY_METHOD_FUNC
|
32
|
+
# if RUBY_VERSION_CODE <= 166
|
33
|
+
extern "C" typedef VALUE (*RUBY_METHOD_FUNC)();
|
34
|
+
# else
|
35
|
+
extern "C" typedef VALUE (*RUBY_METHOD_FUNC)(ANYARGS);
|
36
|
+
# endif
|
37
|
+
#endif
|
38
|
+
|
39
|
+
namespace ruby_capn_proto {
|
40
|
+
}
|
41
|
+
|
42
|
+
#endif /* RUBY_CAPN_PROTO_H */
|
@@ -0,0 +1,59 @@
|
|
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
|
+
defineMethod("interface?", &is_interface).
|
16
|
+
store(&Class);
|
17
|
+
}
|
18
|
+
|
19
|
+
VALUE SchemaNodeReader::alloc(VALUE klass) {
|
20
|
+
return Data_Wrap_Struct(klass, NULL, free, ruby_xmalloc(sizeof(WrappedType)));
|
21
|
+
}
|
22
|
+
|
23
|
+
VALUE SchemaNodeReader::create(WrappedType reader, VALUE parent) {
|
24
|
+
VALUE rb_obj = alloc(Class);
|
25
|
+
WrappedType* wrapped = unwrap(rb_obj);
|
26
|
+
*wrapped = kj::mv(reader);
|
27
|
+
|
28
|
+
rb_iv_set(rb_obj, "parent", parent);
|
29
|
+
|
30
|
+
return rb_obj;
|
31
|
+
}
|
32
|
+
|
33
|
+
void SchemaNodeReader::free(WrappedType* p) {
|
34
|
+
p->~Reader();
|
35
|
+
ruby_xfree(p);
|
36
|
+
}
|
37
|
+
|
38
|
+
WrappedType* SchemaNodeReader::unwrap(VALUE self) {
|
39
|
+
WrappedType* p;
|
40
|
+
Data_Get_Struct(self, WrappedType, p);
|
41
|
+
return p;
|
42
|
+
}
|
43
|
+
|
44
|
+
VALUE SchemaNodeReader::get_nested_nodes(VALUE self) {
|
45
|
+
return ListNestedNodeReader::create(unwrap(self)->getNestedNodes(), self);
|
46
|
+
}
|
47
|
+
|
48
|
+
VALUE SchemaNodeReader::is_struct(VALUE self) {
|
49
|
+
return unwrap(self)->isStruct() ? Qtrue : Qfalse;
|
50
|
+
}
|
51
|
+
|
52
|
+
VALUE SchemaNodeReader::is_interface(VALUE self) {
|
53
|
+
return unwrap(self)->isInterface() ? Qtrue : Qfalse;
|
54
|
+
}
|
55
|
+
|
56
|
+
VALUE SchemaNodeReader::is_enum(VALUE self) {
|
57
|
+
return unwrap(self)->isEnum() ? Qtrue : Qfalse;
|
58
|
+
}
|
59
|
+
}
|