rbind 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (48) hide show
  1. data/lib/rbind.rb +5 -0
  2. data/lib/rbind/.default_parser.rb.swp +0 -0
  3. data/lib/rbind/.generator_c.rb.swp +0 -0
  4. data/lib/rbind/core.rb +13 -0
  5. data/lib/rbind/core/.rbase.rb.swp +0 -0
  6. data/lib/rbind/core/.rclass.rb.swp +0 -0
  7. data/lib/rbind/core/.rdata_type.rb.swp +0 -0
  8. data/lib/rbind/core/.roperation.rb.swp +0 -0
  9. data/lib/rbind/core/rattribute.rb +45 -0
  10. data/lib/rbind/core/rbase.rb +172 -0
  11. data/lib/rbind/core/rclass.rb +149 -0
  12. data/lib/rbind/core/rconst.rb +35 -0
  13. data/lib/rbind/core/rdata_type.rb +93 -0
  14. data/lib/rbind/core/renum.rb +5 -0
  15. data/lib/rbind/core/rgetter.rb +19 -0
  16. data/lib/rbind/core/rnamespace.rb +305 -0
  17. data/lib/rbind/core/roperation.rb +128 -0
  18. data/lib/rbind/core/rparameter.rb +40 -0
  19. data/lib/rbind/core/rsetter.rb +20 -0
  20. data/lib/rbind/core/rstruct.rb +87 -0
  21. data/lib/rbind/default_parser.rb +253 -0
  22. data/lib/rbind/generator_c.rb +352 -0
  23. data/lib/rbind/generator_ruby.rb +357 -0
  24. data/lib/rbind/logger.rb +13 -0
  25. data/lib/rbind/rbind.rb +68 -0
  26. data/lib/rbind/templates/c/CMakeLists.txt +11 -0
  27. data/lib/rbind/templates/c/consts.h +5 -0
  28. data/lib/rbind/templates/c/conversions.cc +4 -0
  29. data/lib/rbind/templates/c/conversions.hpp +10 -0
  30. data/lib/rbind/templates/c/find_package.txt +5 -0
  31. data/lib/rbind/templates/c/operation_wrapper.cc +15 -0
  32. data/lib/rbind/templates/c/operations.cc +23 -0
  33. data/lib/rbind/templates/c/operations.h +20 -0
  34. data/lib/rbind/templates/c/type_conversion.cc +45 -0
  35. data/lib/rbind/templates/c/type_conversion.hpp +5 -0
  36. data/lib/rbind/templates/c/type_delete.h +14 -0
  37. data/lib/rbind/templates/c/type_typedef.h +2 -0
  38. data/lib/rbind/templates/c/type_wrapper.h +12 -0
  39. data/lib/rbind/templates/c/types.cc +6 -0
  40. data/lib/rbind/templates/c/types.h +17 -0
  41. data/lib/rbind/templates/ruby/rbind.rb +44 -0
  42. data/lib/rbind/templates/ruby/rmethod.rb +12 -0
  43. data/lib/rbind/templates/ruby/rnamespace.rb +7 -0
  44. data/lib/rbind/templates/ruby/rstatic_method.rb +5 -0
  45. data/lib/rbind/templates/ruby/rtype.rb +77 -0
  46. data/lib/rbind/templates/ruby/rtype_constructor.rb +4 -0
  47. data/rbind.gemspec +17 -0
  48. metadata +101 -0
@@ -0,0 +1,5 @@
1
+ #ifndef <%= name %>
2
+ #define <%= name %>
3
+ <%= wrap_includes %>
4
+ <%= wrap_consts %>
5
+ #endif
@@ -0,0 +1,4 @@
1
+ #include "conversions.hpp"
2
+ <%= wrap_includes %>
3
+
4
+ <%= wrap_conversions %>
@@ -0,0 +1,10 @@
1
+ #ifndef <%= name %>
2
+ #define <%= name %>
3
+
4
+ #include <stdexcept>
5
+ #include <typeinfo>
6
+ #include "types.h"
7
+ <%= wrap_includes %>
8
+
9
+ <%= wrap_conversions %>
10
+ #endif
@@ -0,0 +1,5 @@
1
+ pkg_check_modules(<%= upcase %> REQUIRED <%= self %>)
2
+ add_definitions(${<%= upcase %>_CFLAGS})
3
+ include_directories(${<%= upcase %>_INCLUDE_DIRS})
4
+ link_directories(${<%= upcase %>_LIBRARY_DIRS})
5
+
@@ -0,0 +1,15 @@
1
+ // wrapper for <%= attribute? ? "#{owner.full_name}.#{attribute.name}" : signature %>
2
+ <%= csignature %>
3
+ {
4
+ try
5
+ {
6
+ <%= wrap_parameters %><%= wrap_call %>
7
+ }
8
+ catch(std::exception &error){strncpy(&last_error_message[0],error.what(),255);}
9
+ catch(...){strncpy(&last_error_message[0],"Unknown Exception",255);}
10
+ <%- if !return_type || !return_type.basic_type? -%>
11
+ return NULL;
12
+ <%- elsif return_type.name != "void" || return_type.ptr?-%>
13
+ return <%= return_type.invalid_value %>;
14
+ <%- end -%>
15
+ }
@@ -0,0 +1,23 @@
1
+
2
+ #include "conversions.hpp"
3
+ #include "operations.h"
4
+ <%= wrap_includes %>
5
+
6
+ char last_error_message[255] = {0};
7
+ const char* rbindGetLastError()
8
+ {
9
+ return &last_error_message[0];
10
+ }
11
+
12
+ bool rbindHasError()
13
+ {
14
+ return (*rbindGetLastError() != '\0');
15
+ }
16
+
17
+ void rbindClearError()
18
+ {
19
+ last_error_message[0] = '\0';
20
+ }
21
+
22
+
23
+ <%= wrap_operations%>
@@ -0,0 +1,20 @@
1
+ #ifndef <%= name %>
2
+ #define <%= name %>
3
+ <%= wrap_includes %>
4
+
5
+ #ifdef __cplusplus
6
+ extern "C"
7
+ {
8
+ #endif
9
+
10
+ // general rbind functions
11
+ const char* rbindGetLastError();
12
+ bool rbindHasError();
13
+ void rbindClearError();
14
+
15
+ <%= wrap_operations %>
16
+
17
+ #ifdef __cplusplus
18
+ }
19
+ #endif
20
+ #endif
@@ -0,0 +1,45 @@
1
+ // converts <%= full_name %>* to <%= cname %>*
2
+ <%= cname %>* toC(<%= full_name %>* ptr, bool owner)
3
+ {
4
+ <%= cname %>* r_ptr = new <%= cname %>;
5
+ r_ptr->version = <%= version %>;
6
+ r_ptr->size = sizeof(*ptr);
7
+ r_ptr->type_id = (void*) &typeid(*ptr);
8
+ r_ptr->bowner = owner;
9
+ r_ptr->obj_ptr = (void*) ptr;
10
+ return r_ptr;
11
+ }
12
+
13
+ // converts const <%= cname %> to const <%= full_name %>
14
+ const <%= full_name %>* fromC(const <%= cname %>* ptr)
15
+ {
16
+ <%- if check_type? -%>
17
+ // check typeid if available
18
+ if(ptr->type_id && typeid(<%= full_name %>) != *static_cast<const std::type_info*>(ptr->type_id))
19
+ {
20
+ std::string str("wrong object type for <%= full_name %>: got ");
21
+ throw std::runtime_error(str + static_cast<const std::type_info*>(ptr->type_id)->name()
22
+ + " but was expecting " + typeid(<%= full_name %>).name());
23
+ }
24
+
25
+ // check version
26
+ if(ptr->version != <%= version %>)
27
+ throw std::runtime_error("wrong object version for <%= full_name %>");
28
+ <%- end %>
29
+
30
+ // check size
31
+ if(sizeof(<%= full_name %>) > ptr->size)
32
+ throw std::runtime_error("wrong object size for <%= full_name %>");
33
+
34
+ if(!ptr->obj_ptr)
35
+ throw std::runtime_error("object for <%= full_name %> was deleted!");
36
+
37
+ return static_cast<const <%= full_name %>*>(ptr->obj_ptr);
38
+ }
39
+
40
+ // converts <%= cname %>* to <%= full_name %>*
41
+ <%= full_name %>* fromC(<%= cname %>* ptr)
42
+ {
43
+ return const_cast<<%= full_name %>*>(fromC(static_cast<const <%= cname %>*>(ptr)));
44
+ }
45
+
@@ -0,0 +1,5 @@
1
+ // convert functions for <%= full_name %>* to and from <%= cname %>*
2
+ <%= cname %>* toC(<%= full_name %>* ptr, bool owner = true);
3
+ const <%= full_name %>* fromC(const <%= cname %>* ptr);
4
+ <%= full_name %>* fromC(<%= cname %>* ptr);
5
+
@@ -0,0 +1,14 @@
1
+ // deletes <%= cname %> and the underlying object
2
+ // <%= full_name %> if the struct is owner of it
3
+ void <%= cdelete_method %>(<%= cname %> *ptr)
4
+ {
5
+ try
6
+ {
7
+ if(ptr->bowner && ptr->obj_ptr)
8
+ delete fromC(ptr);
9
+ ptr->obj_ptr = NULL;
10
+ }
11
+ catch(std::exception &error){strncpy(&last_error_message[0],error.what(),255);}
12
+ catch(...){strncpy(&last_error_message[0],"Unknown Exception",255);}
13
+ delete ptr;
14
+ }
@@ -0,0 +1,2 @@
1
+ typedef <%= "#{typedef} #{full_name}" %>;
2
+
@@ -0,0 +1,12 @@
1
+ // wrapper for <%= full_name %>
2
+ typedef struct <%= cname %>
3
+ {
4
+ unsigned char version; // version of the object
5
+ size_t size; // size of the object in bytes
6
+ void *type_id; // type id of the object
7
+ void *obj_ptr; // ptr to <%= full_name %>
8
+ bool bowner; // true if struct is the owner of the object
9
+ }<%= cname %>;
10
+
11
+ void <%= cdelete_method %>(<%= cname %> *ptr);
12
+
@@ -0,0 +1,6 @@
1
+ #include "conversions.hpp"
2
+ <%= wrap_includes%>
3
+
4
+ extern char last_error_message[255];
5
+
6
+ <%= wrap_types%>
@@ -0,0 +1,17 @@
1
+ #ifndef <%= name %>
2
+ #define <%= name %>
3
+
4
+ #include <cstddef>
5
+ <%= wrap_includes %>
6
+
7
+ #ifdef __cplusplus
8
+ extern "C"
9
+ {
10
+ #endif
11
+
12
+ <%= wrap_types%>
13
+
14
+ #ifdef __cplusplus
15
+ }
16
+ #endif
17
+ #endif
@@ -0,0 +1,44 @@
1
+ require 'ffi'
2
+ require File.join(File.dirname(__FILE__),'opencv_types')
3
+
4
+ module <%= name %>
5
+ # low level accessors the wrapped library
6
+ module Rbind
7
+ extend FFI::Library
8
+ ffi_lib "<%= library_name %>"
9
+
10
+ #add error checking
11
+ #rbindCreateMatrix -> create_matrix
12
+ def self.attach_function(ruby_name,c_name, args, returns,error_checking=true)
13
+ return super(ruby_name,c_name,args,returns) unless error_checking || !returns
14
+
15
+ #add accessor for c function
16
+ super("orig_#{ruby_name}", c_name, args, returns)
17
+
18
+ #add ruby method that does error checking after the c
19
+ #function was called
20
+ line_no = __LINE__; str = %Q{
21
+ def #{ruby_name}(*args, &block)
22
+ val = orig_#{ruby_name}(*args,&block)
23
+ if has_error
24
+ except = RuntimeError.new(last_error)
25
+ except.set_backtrace(caller)
26
+ clear_error
27
+ raise except
28
+ end
29
+ val
30
+ end
31
+ }
32
+ instance_eval(str,__FILE__,line_no)
33
+ self
34
+ end
35
+
36
+ # add basic rbind functions which must not get error checking
37
+ attach_function :has_error,:rbindHasError,[],:bool,false
38
+ attach_function :last_error,:rbindGetLastError,[],:string,false
39
+ attach_function :clear_error,:rbindClearError,[],:void,false
40
+
41
+ # add accessor for wrapped methods
42
+ <%= add_accessors %>
43
+ end
44
+ end
@@ -0,0 +1,12 @@
1
+ # wrapper for <%= signature %>
2
+ def <%=name%>(<%= wrap_parameters_signature %>)
3
+ <%- if return_type.basic_type? || operator? -%>
4
+ Rbind::<%= cname %>( <%= wrap_parameters_call %>)
5
+ <%- else -%>
6
+ result = Rbind::<%= cname %>( <%= wrap_parameters_call %>)
7
+ # store owner insight the pointer to not get garbage collected
8
+ result.instance_variable_get(:@__obj_ptr__).instance_variable_set(:@__owner__,self) if !result.__owner__?
9
+ result
10
+ <%- end -%>
11
+ end
12
+
@@ -0,0 +1,7 @@
1
+ # wrapper for <%= full_name %>
2
+ module <%= name %>
3
+ <%= add_consts%>
4
+ <%= add_methods %>
5
+ <%= add_types %>
6
+ end
7
+
@@ -0,0 +1,5 @@
1
+ # wrapper for <%= signature %>
2
+ def self.<%= name %>(<%= wrap_parameters_signature %>)
3
+ Rbind::<%= cname %>(<%= wrap_parameters_call %>)
4
+ end
5
+
@@ -0,0 +1,77 @@
1
+ # object wrapping <%= full_name %>
2
+ class <%= name %>Struct < FFI::Struct
3
+ layout :version,:uchar,
4
+ :size,:size_t,
5
+ :type_id,:pointer,
6
+ :obj_ptr,:pointer,
7
+ :bowner,:bool
8
+ # auto delete
9
+ def self.release(pointer)
10
+ Rbind::<%= cdelete_method %>_struct(pointer) unless pointer.null?
11
+ rescue Exception => e
12
+ puts e
13
+ end
14
+ end
15
+
16
+ class <%= name %>
17
+ extend FFI::DataConverter
18
+ native_type FFI::Type::POINTER
19
+
20
+ def self.new(*args)
21
+ if args.first.is_a?(FFI::Pointer)
22
+ raise ArgumentError, "too many arguments for creating #{self.name} from Pointer" unless args.size == 1
23
+ return super(args.first)
24
+ end
25
+ <%= add_constructor %>
26
+ raise ArgumentError, "no constructor for #{self}(#{args.inspect})"
27
+ end
28
+
29
+ def self.rbind_to_native(obj,context)
30
+ if obj.is_a? <%= name %>
31
+ obj.__obj_ptr__
32
+ else
33
+ raise TypeError, "expected kind of #{name}, was #{obj.class}"
34
+ end
35
+ end
36
+
37
+ def self.rbind_from_native(ptr,context)
38
+ <%= name %>.new(ptr)
39
+ end
40
+
41
+ # can be overwritten by the user
42
+ def self.to_native(obj,context)
43
+ rbind_to_native(obj,context)
44
+ end
45
+
46
+ # can be overwritten by the user
47
+ def self.from_native(ptr,context)
48
+ rbind_from_native(ptr,context)
49
+ end
50
+
51
+ attr_reader :__obj_ptr__
52
+ def initialize(ptr)
53
+ @__obj_ptr__ = if ptr.is_a? <%= name %>Struct
54
+ ptr
55
+ else
56
+ <%= name %>Struct.new(FFI::AutoPointer.new(ptr,<%= name %>Struct.method(:release)))
57
+ end
58
+ end
59
+
60
+ # returns true if the underlying pointer is owner of
61
+ # the real object
62
+ def __owner__?
63
+ @__obj_ptr__[:bowner]
64
+ end
65
+
66
+ # custom specializing
67
+ <%= add_specializing %>
68
+
69
+ # consts
70
+ <%= add_consts %>
71
+
72
+ # methods
73
+ <%= add_methods %>
74
+ # types
75
+ <%= add_types %>
76
+ end
77
+
@@ -0,0 +1,4 @@
1
+ # wrapper for <%= signature %>
2
+ if args.size == <%= parameters.size %>
3
+ return Rbind::<%= cname %>(*args)
4
+ end
@@ -0,0 +1,17 @@
1
+ Gem::Specification.new do |s|
2
+ s.name = 'rbind'
3
+ s.version = '0.0.1'
4
+ s.date = '2013-06-17'
5
+ s.platform = Gem::Platform::RUBY
6
+ s.authors = ['Alexander Duda']
7
+ s.email = ['Alexander.Duda@dfki.de']
8
+ s.homepage = 'http://github.com/'
9
+ s.summary = 'Library for genereating automated ffi-bindings for c/c++ libraries'
10
+ s.description = ''
11
+ s.files = `git ls-files`.split("\n")
12
+ s.require_path = 'lib'
13
+ s.required_rubygems_version = ">= 1.3.6"
14
+
15
+ #s.rubyforge_project = s.name
16
+ #s.add_runtime_dependency "other", "~> 1.2"
17
+ end
metadata ADDED
@@ -0,0 +1,101 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rbind
3
+ version: !ruby/object:Gem::Version
4
+ prerelease:
5
+ version: 0.0.1
6
+ platform: ruby
7
+ authors:
8
+ - Alexander Duda
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+
13
+ date: 2013-06-17 00:00:00 Z
14
+ dependencies: []
15
+
16
+ description: ""
17
+ email:
18
+ - Alexander.Duda@dfki.de
19
+ executables: []
20
+
21
+ extensions: []
22
+
23
+ extra_rdoc_files: []
24
+
25
+ files:
26
+ - lib/rbind.rb
27
+ - lib/rbind/.default_parser.rb.swp
28
+ - lib/rbind/.generator_c.rb.swp
29
+ - lib/rbind/core.rb
30
+ - lib/rbind/core/.rbase.rb.swp
31
+ - lib/rbind/core/.rclass.rb.swp
32
+ - lib/rbind/core/.rdata_type.rb.swp
33
+ - lib/rbind/core/.roperation.rb.swp
34
+ - lib/rbind/core/rattribute.rb
35
+ - lib/rbind/core/rbase.rb
36
+ - lib/rbind/core/rclass.rb
37
+ - lib/rbind/core/rconst.rb
38
+ - lib/rbind/core/rdata_type.rb
39
+ - lib/rbind/core/renum.rb
40
+ - lib/rbind/core/rgetter.rb
41
+ - lib/rbind/core/rnamespace.rb
42
+ - lib/rbind/core/roperation.rb
43
+ - lib/rbind/core/rparameter.rb
44
+ - lib/rbind/core/rsetter.rb
45
+ - lib/rbind/core/rstruct.rb
46
+ - lib/rbind/default_parser.rb
47
+ - lib/rbind/generator_c.rb
48
+ - lib/rbind/generator_ruby.rb
49
+ - lib/rbind/logger.rb
50
+ - lib/rbind/rbind.rb
51
+ - lib/rbind/templates/c/CMakeLists.txt
52
+ - lib/rbind/templates/c/consts.h
53
+ - lib/rbind/templates/c/conversions.cc
54
+ - lib/rbind/templates/c/conversions.hpp
55
+ - lib/rbind/templates/c/find_package.txt
56
+ - lib/rbind/templates/c/operation_wrapper.cc
57
+ - lib/rbind/templates/c/operations.cc
58
+ - lib/rbind/templates/c/operations.h
59
+ - lib/rbind/templates/c/type_conversion.cc
60
+ - lib/rbind/templates/c/type_conversion.hpp
61
+ - lib/rbind/templates/c/type_delete.h
62
+ - lib/rbind/templates/c/type_typedef.h
63
+ - lib/rbind/templates/c/type_wrapper.h
64
+ - lib/rbind/templates/c/types.cc
65
+ - lib/rbind/templates/c/types.h
66
+ - lib/rbind/templates/ruby/rbind.rb
67
+ - lib/rbind/templates/ruby/rmethod.rb
68
+ - lib/rbind/templates/ruby/rnamespace.rb
69
+ - lib/rbind/templates/ruby/rstatic_method.rb
70
+ - lib/rbind/templates/ruby/rtype.rb
71
+ - lib/rbind/templates/ruby/rtype_constructor.rb
72
+ - rbind.gemspec
73
+ homepage: http://github.com/
74
+ licenses: []
75
+
76
+ post_install_message:
77
+ rdoc_options: []
78
+
79
+ require_paths:
80
+ - lib
81
+ required_ruby_version: !ruby/object:Gem::Requirement
82
+ none: false
83
+ requirements:
84
+ - - ">="
85
+ - !ruby/object:Gem::Version
86
+ version: "0"
87
+ required_rubygems_version: !ruby/object:Gem::Requirement
88
+ none: false
89
+ requirements:
90
+ - - ">="
91
+ - !ruby/object:Gem::Version
92
+ version: 1.3.6
93
+ requirements: []
94
+
95
+ rubyforge_project:
96
+ rubygems_version: 1.8.23
97
+ signing_key:
98
+ specification_version: 3
99
+ summary: Library for genereating automated ffi-bindings for c/c++ libraries
100
+ test_files: []
101
+