rucy 0.1.2 → 0.1.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,222 @@
1
+ #include <rucy.h>
2
+
3
+
4
+ using namespace Rucy;
5
+
6
+
7
+ struct Tester
8
+ {
9
+
10
+ int value;
11
+
12
+ };// Tester
13
+
14
+
15
+ static Class cTester;
16
+
17
+
18
+ Class
19
+ tester_class ()
20
+ {
21
+ return cTester;
22
+ }
23
+
24
+
25
+ namespace Rucy
26
+ {
27
+
28
+
29
+ static Value
30
+ value (const Tester& obj)
31
+ {
32
+ return new_type<Tester>(tester_class(), new Tester(obj));
33
+ }
34
+
35
+ template <> inline Tester*
36
+ value_to<Tester*> (Value obj, bool)
37
+ {
38
+ return get_type<Tester>(obj, tester_class());
39
+ }
40
+
41
+
42
+ }// Rucy
43
+
44
+
45
+ /*
46
+ alloc function.
47
+ */
48
+ static
49
+ VALUE alloc(VALUE klass)
50
+ {
51
+ return new_type<Tester>(klass);
52
+ }
53
+
54
+ /*
55
+ get value.
56
+ */
57
+ static
58
+ VALUE get_value(VALUE self)
59
+ {
60
+ Tester* t = to<Tester*>(self);
61
+ if (t) return value(t->value);
62
+ }
63
+
64
+ /*
65
+ set value.
66
+ */
67
+ static
68
+ VALUE set_value(VALUE self, VALUE value)
69
+ {
70
+ Tester* t = to<Tester*>(self);
71
+ if (t) t->value = to<int>(value);
72
+ }
73
+
74
+ /*
75
+ do nothing.
76
+ */
77
+ static
78
+ VALUE do_nothing(VALUE self)
79
+ {
80
+ }
81
+
82
+ /*
83
+ return nil.
84
+ */
85
+ static
86
+ VALUE return_nil(VALUE self)
87
+ {
88
+ return Qnil;
89
+ }
90
+
91
+ /*
92
+ return int.
93
+ */
94
+ static
95
+ VALUE return_int(VALUE self)
96
+ {
97
+ return value(1);
98
+ }
99
+
100
+ /*
101
+ return flaot.
102
+ */
103
+ static
104
+ VALUE return_float(VALUE self)
105
+ {
106
+ return value(1.0f);
107
+ }
108
+
109
+ /*
110
+ return string.
111
+ */
112
+ static
113
+ VALUE return_string(VALUE self)
114
+ {
115
+ return value("");
116
+ }
117
+
118
+ /*
119
+ raise ruby's exception.
120
+ */
121
+ static
122
+ VALUE raise_ruby_exception(VALUE self)
123
+ {
124
+ throw RubyException(rb_eStandardError, "raise_ruby_exception");
125
+ }
126
+
127
+ /*
128
+ raise in eval.
129
+ */
130
+ static
131
+ VALUE raise_in_eval(VALUE self)
132
+ {
133
+ eval("raise 'raise_in_eval'");
134
+ }
135
+
136
+ /*
137
+ throw nothing.
138
+ */
139
+ static
140
+ VALUE throw_nothing(VALUE self)
141
+ {
142
+ throw;
143
+ }
144
+
145
+ /*
146
+ throw std::exception.
147
+ */
148
+ static
149
+ VALUE throw_std_exception(VALUE self)
150
+ {
151
+ throw std::exception();
152
+ }
153
+
154
+ /*
155
+ throw std::runtime_error.
156
+ */
157
+ static
158
+ VALUE throw_std_runtime_error(VALUE self)
159
+ {
160
+ throw std::runtime_error("std::runtime_error");
161
+ }
162
+
163
+ struct MyException : public std::runtime_error
164
+ {
165
+ MyException() : runtime_error("") {}
166
+ };
167
+
168
+ /*
169
+ throw custom exception class.
170
+ */
171
+ static
172
+ VALUE throw_custom_exception(VALUE self)
173
+ {
174
+ throw MyException();
175
+ }
176
+
177
+ /*
178
+ throw std::string.
179
+ */
180
+ static
181
+ VALUE throw_std_string(VALUE self)
182
+ {
183
+ throw std::string("std::string");
184
+ }
185
+
186
+ /*
187
+ throw char*.
188
+ */
189
+ static
190
+ VALUE throw_cstring(VALUE self)
191
+ {
192
+ throw "cstring";
193
+ }
194
+
195
+
196
+ extern "C" void
197
+ Init_tester ()
198
+ {
199
+ if (!init()) return;
200
+
201
+ Module m = rb_define_module("Rucy");
202
+
203
+ Class c = rb_define_class_under(m, "Tester", rb_cObject);
204
+ cTester = c;
205
+
206
+ rb_define_alloc_func(c, alloc);
207
+ rb_define_method(c, "value", RUBY_METHOD_FUNC(get_value), 0);
208
+ rb_define_method(c, "value=", RUBY_METHOD_FUNC(set_value), 1);
209
+ rb_define_method(c, "do_nothing", RUBY_METHOD_FUNC(do_nothing), -1);
210
+ rb_define_method(c, "return_nil", RUBY_METHOD_FUNC(return_nil), 0);
211
+ rb_define_method(c, "return_int", RUBY_METHOD_FUNC(return_int), 0);
212
+ rb_define_method(c, "return_float", RUBY_METHOD_FUNC(return_float), 0);
213
+ rb_define_method(c, "return_string", RUBY_METHOD_FUNC(return_string), 0);
214
+ rb_define_method(c, "raise_ruby_exception", RUBY_METHOD_FUNC(raise_ruby_exception), 0);
215
+ rb_define_method(c, "raise_in_eval", RUBY_METHOD_FUNC(raise_in_eval), 0);
216
+ rb_define_method(c, "throw_nothing", RUBY_METHOD_FUNC(throw_nothing), 0);
217
+ rb_define_method(c, "throw_std_exception", RUBY_METHOD_FUNC(throw_std_exception), 0);
218
+ rb_define_method(c, "throw_std_runtime_error", RUBY_METHOD_FUNC(throw_std_runtime_error), 0);
219
+ rb_define_method(c, "throw_custom_exception", RUBY_METHOD_FUNC(throw_custom_exception), 0);
220
+ rb_define_method(c, "throw_std_string", RUBY_METHOD_FUNC(throw_std_string), 0);
221
+ rb_define_method(c, "throw_cstring", RUBY_METHOD_FUNC(throw_cstring), 0);
222
+ }
data/ChangeLog CHANGED
@@ -1,3 +1,8 @@
1
+ 2011-09-2 snori <snori@xord.org>
2
+
3
+ * add bin/rucy2rdoc.
4
+
5
+
1
6
  2011-08-28 snori <snori@xord.org>
2
7
 
3
8
  * v0.1.1
data/README CHANGED
@@ -1,4 +1,31 @@
1
1
 
2
2
  = Rucy - A Ruby C++ Extension Helper Library
3
3
 
4
- == by snori@xord.org
4
+ by snori@xord.org
5
+
6
+
7
+ = What is this library?
8
+
9
+ This library helps you to develop Ruby C extension library with few useful
10
+ classes.
11
+
12
+ * Wrap VALUE for easy handling.
13
+ * Exception safe (Rucy/C++).
14
+
15
+
16
+ = How to use
17
+
18
+ $ gem install rucy
19
+
20
+ write C++ source like ext/rucy/tester.cpp.
21
+
22
+
23
+ = Examples
24
+
25
+ see ext/rucy/tester.cpp.
26
+
27
+
28
+ = Contact information.
29
+
30
+ Web: http://blog.xord.org/
31
+ Mail: snori@xord.org
data/Rakefile CHANGED
@@ -1,55 +1,28 @@
1
1
  # -*- mode: ruby; coding: utf-8 -*-
2
2
 
3
3
 
4
- %w[. ./lib].each do |path|
4
+ %w[. ../xot].map {|s| "#{s}/lib"}.each do |path|
5
5
  $: << File.expand_path(File.join File.dirname(__FILE__), *path.split('/'))
6
6
  end
7
7
 
8
8
  require 'rubygems'
9
- require 'rbconfig'
10
- require 'support'
9
+ require 'xot/rake/helpers'
10
+ require 'xot/module'
11
11
  require 'rucy/module'
12
12
 
13
+ include Xot::Rake
13
14
 
14
- MODULE = Rucy
15
- NAME = MODULE.name.downcase
16
15
 
17
- SRCDIR = 'src'
18
- INCDIR = 'include'
19
- LIBDIR = 'lib'
20
- EXTDIR = 'ext'
21
- TASKDIR = 'task'
22
-
23
- EXTEXT = RbConfig::CONFIG['DLEXT'] || 'so'
24
-
25
- DEFS = %w[]
26
- DEFS << 'WIN32' if win32?
27
- DEFS << 'COCOA' if cocoa?
28
-
29
- incroot = RbConfig::CONFIG['rubyhdrdir']
30
- INCDIRS = [
31
- File.expand_path('include'),
32
- incroot,
33
- "#{incroot}/#{RUBY_PLATFORM}",
34
- '/opt/local/include',
35
- '/opt/include'
36
- ]
37
-
38
- RBS = glob '**/*.rb'
39
-
40
- RUBY = ENV['RUBY'] || 'ruby'
41
- GEM = ENV['GEM'] || 'gem'
42
- GIT = ENV['GIT'] || 'git'
43
- MAKE = ENV['MAKE'] || 'make'
44
- CC = RbConfig::CONFIG['CC'] || ENV['CC'] || 'g++'
45
- CFLAGS = '-Wall -O' + DEFS.map{|s| " -D#{s}"}.join
46
- AR = ENV['AR'] || 'ar'
47
- ARFLAGS = 'crs'
16
+ MODULE = Rucy
17
+ INCDIRS = [Rucy].map {|m| m.include_dirs}.flatten
18
+ RUCY2RDOC = 'bin/rucy2rdoc'
19
+ NPARAM_MAX = 8
20
+ NTIMES = (0..NPARAM_MAX)
48
21
 
49
22
 
50
23
  task :default => :build
51
24
 
52
- task :build => :ext
25
+ task :build => :lib
53
26
 
54
27
  task :rebuild => [:clean, :build]
55
28
 
@@ -57,6 +30,8 @@ task :lib => 'lib:build'
57
30
 
58
31
  task :ext => 'ext:build'
59
32
 
33
+ task :doc => 'ext:doc'
34
+
60
35
  task :gem => 'gem:build'
61
36
 
62
37
  task :install => 'gem:install'
@@ -72,4 +47,4 @@ task :test => :ext do
72
47
  end
73
48
 
74
49
 
75
- Dir["#{TASKDIR}/**/*.rake"].each {|path| load path}
50
+ [Xot, Rucy].each {|m| m.load_tasks}
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.2
1
+ 0.1.3
data/bin/rucy2rdoc ADDED
@@ -0,0 +1,113 @@
1
+ #!/usr/bin/env ruby
2
+
3
+
4
+ class Parser
5
+
6
+ def initialize (lines)
7
+ @funs = {}
8
+ @lines = lines
9
+ end
10
+
11
+ def parse ()
12
+ parse_functions @lines
13
+ parse_Init @lines
14
+ @lines
15
+ end
16
+
17
+ private
18
+
19
+ def process_lines (lines, regexp, &block)
20
+ lines.gsub! regexp do |str|
21
+ params = $~[1..-1]
22
+ params.unshift str
23
+ block.call *params
24
+ end
25
+ end
26
+
27
+ def parse_functions (lines)
28
+ re = %r{
29
+ (?:RUBY_)?DEF(\d+|N|_ALLOC)\( (\w+) \s* ((?:\, \s* \w+)*) \s* \) \s*
30
+ (\{ \s*
31
+ .*?
32
+ \}) \s*
33
+ (?:RUBY_)?END
34
+ }mx
35
+ process_lines lines, re do |all, type, name, params, body|
36
+ type = '-1' if type == 'N'
37
+ params.gsub! /\,/, ', VALUE'
38
+ params = '' unless params
39
+ if type == '_ALLOC'
40
+ params.gsub! /^\,\s*/, ''
41
+ else
42
+ params = 'VALUE self' + params
43
+ end
44
+ @funs[name] = {
45
+ :type => type,
46
+ :params => params,
47
+ :body => body
48
+ }
49
+ "VALUE #{name}(#{params})\n#{body}"
50
+ end
51
+ end
52
+
53
+ def parse_Init (lines)
54
+ re = %r{
55
+ Init_(\w*) \s* \( \s* \) \s*
56
+ \{ \s*
57
+ .*?
58
+ \} \s*
59
+ }mx
60
+ process_lines lines, re do |all, name|
61
+ parse_module_and_class all
62
+ parse_methods all
63
+ all
64
+ end
65
+ end
66
+
67
+ def parse_module_and_class (lines)
68
+ re = %r{
69
+ (\w+) \s* \= \s*
70
+ (?: (\w+) \s* \. )? \s* (define_(?:module|class)) \s*
71
+ \( \s* (\"\w+\") \s* (?: \, \s* (\w+) )? \s* \) \s* ;
72
+ }mx
73
+ process_lines lines, re do |all, var, recv, define, name, super_|
74
+ define += '_under' if recv
75
+ recv += ", " if recv
76
+ super_ = 'rb_cObject' if !super_ && define =~ /class/
77
+ super_ = ', ' + super_ if super_
78
+ "#{var} = rb_#{define}(#{recv}#{name}#{super_});"
79
+ end
80
+ end
81
+
82
+ def parse_methods (lines)
83
+ re = %r{
84
+ (\w+) \s* \. \s* (define_\w+) \s*
85
+ \( \s* (?: (\"\w+(?:\=|\?\!)?\") \s* \, \s* )? (\w+) \s* \) \s* ;
86
+ }mx
87
+ process_lines lines, re do |all, obj, define, symbol, name|
88
+ type = @funs[name][:type]
89
+ if type == '_ALLOC'
90
+ "rb_#{define}(#{obj}, #{name});"
91
+ else
92
+ "rb_#{define}(#{obj}, #{symbol}, RUBY_METHOD_FUNC(#{name}), #{type});"
93
+ end
94
+ end
95
+ end
96
+
97
+ end# Parser
98
+
99
+
100
+ def usage ()
101
+ "rucy2rdoc FILE"
102
+ end
103
+
104
+ def main (argv)
105
+ if path = argv.shift
106
+ print Parser.new(File.read path).parse
107
+ else
108
+ puts usage
109
+ end
110
+ end
111
+
112
+
113
+ main ARGV.dup
data/ext/rucy/extconf.rb CHANGED
@@ -1,48 +1,57 @@
1
1
  # -*- coding: utf-8 -*-
2
2
 
3
3
 
4
- %w[../../lib].each do |path|
4
+ %w[. ../xot].map {|s| "../../#{s}/lib"}.each do |path|
5
5
  $: << File.expand_path(File.join File.dirname(__FILE__), *path.split('/'))
6
6
  end
7
7
 
8
8
  require 'rubygems'
9
9
  require 'mkmf'
10
+ require 'xot/rake/helpers'
11
+ require 'xot/module'
10
12
  require 'rucy/module'
11
13
 
14
+ include Xot::Rake
12
15
 
13
- DEBUG = ENV['DEBUG'] || false
16
+
17
+ DEBUG = env :DEBUG, false
14
18
 
15
19
  DEFS = []
16
- INCDIRS = %w[/opt/local/include /opt/include]
20
+ INCDIRS = %w[
21
+ /opt/local/include
22
+ /opt/include
23
+ ]
17
24
  LIBDIRS = []
18
25
 
19
26
  HEADERS = %w[
20
27
  boost/noncopyable.hpp
21
28
  ruby.h
29
+ xot.h
22
30
  rucy.h
23
31
  ]
24
- LIBS = %w[stdc++ rucy]
32
+ LIBS = %w[
33
+ stdc++
34
+ xot
35
+ rucy
36
+ ]
25
37
 
26
38
 
27
39
  DEFS << '_DEBUG' if DEBUG
28
-
29
- case RUBY_PLATFORM
30
- when /mswin|ming|cygwin/
31
- DEFS << 'WINDOWS' << 'WIN32' << $~[0].upcase
32
- when /darwin/
33
- DEFS << 'COCOA'
34
- end
40
+ DEFS << 'WINDOWS' << 'WIN32' if win32?
41
+ DEFS << 'COCOA' if cocoa?
42
+ DEFS << $~[0].upcase if RUBY_PLATFORM =~ /mswin|ming|cygwin|darwin/i
35
43
 
36
44
  $CPPFLAGS << DEFS.map {|s| " -D#{s}"}.join
37
45
  $CPPFLAGS << INCDIRS.map {|s| " -I#{s}"}.join
38
46
  $LDFLAGS << LIBDIRS.map {|s| " -L#{s}"}.join
39
47
  $LOCAL_LIBS << ' -lrucy'
40
48
 
41
- dir_config 'boost'
42
- dir_config 'rucy', Rucy.root_dir
49
+ Config::CONFIG.each {|key, val| val.gsub!(/gcc/, 'g++')}
43
50
 
44
51
 
45
- Config::CONFIG.each {|key, val| val.gsub!(/gcc/, 'g++')}
52
+ dir_config 'boost'
53
+ dir_config 'xot', Xot.root_dir
54
+ dir_config 'rucy', Rucy.root_dir
46
55
 
47
56
  exit 1 unless HEADERS.all? {|s| have_header(s)}
48
57
  exit 1 unless LIBS.all? {|s| have_library(s)}