utilrb 2.0.2.b2 → 2.1.0.rc1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (75) hide show
  1. checksums.yaml +4 -4
  2. data/.boring +40 -0
  3. data/.gitignore +13 -0
  4. data/.travis.yml +5 -0
  5. data/CMakeLists.txt +18 -0
  6. data/Gemfile +3 -0
  7. data/Makefile +8 -0
  8. data/Manifest.txt +0 -8
  9. data/{README.rd → README.md} +11 -7
  10. data/Rakefile +16 -63
  11. data/benchmarks/validate_options.rb +79 -0
  12. data/ext/utilrb/extconf.rb +1 -17
  13. data/ext/utilrb/utilrb.cc +0 -23
  14. data/lib/utilrb/column_formatter.rb +8 -5
  15. data/lib/utilrb/common.rb +1 -6
  16. data/lib/utilrb/enumerable/uniq.rb +2 -8
  17. data/lib/utilrb/event_loop.rb +5 -10
  18. data/lib/utilrb/kernel/load_dsl_file.rb +1 -2
  19. data/lib/utilrb/kernel/options.rb +25 -29
  20. data/lib/utilrb/logger/hierarchy.rb +0 -1
  21. data/lib/utilrb/logger/io.rb +3 -3
  22. data/lib/utilrb/logger/root.rb +12 -6
  23. data/lib/utilrb/module/ancestor_p.rb +0 -12
  24. data/lib/utilrb/module/is_singleton.rb +6 -0
  25. data/lib/utilrb/module/singleton_class_p.rb +14 -0
  26. data/lib/utilrb/object/attribute.rb +33 -65
  27. data/lib/utilrb/object/singleton_class.rb +1 -20
  28. data/lib/utilrb/pkgconfig.rb +21 -10
  29. data/lib/utilrb/socket/tcp_server.rb +2 -2
  30. data/lib/utilrb/spawn.rb +1 -1
  31. data/lib/utilrb/test.rb +65 -0
  32. data/lib/utilrb/thread_pool.rb +11 -13
  33. data/lib/utilrb/timepoints.rb +15 -0
  34. data/lib/utilrb/value_set.rb +10 -1
  35. data/lib/utilrb/version.rb +4 -0
  36. data/lib/utilrb/weakref.rb +11 -12
  37. data/lib/utilrb/yard.rb +0 -111
  38. data/lib/utilrb.rb +6 -1
  39. data/lib/yard-utilrb.rb +1 -0
  40. data/manifest.xml +19 -0
  41. data/package.xml +29 -0
  42. data/utilrb.gemspec +27 -0
  43. metadata +56 -107
  44. data/ext/utilrb/proc.c +0 -39
  45. data/ext/utilrb/readline.c +0 -52
  46. data/ext/utilrb/weakref.cc +0 -143
  47. data/lib/utilrb/models/inherited_enumerable.rb +0 -341
  48. data/lib/utilrb/models/registration.rb +0 -115
  49. data/lib/utilrb/module/inherited_enumerable.rb +0 -6
  50. data/lib/utilrb/objectstats.rb +0 -193
  51. data/lib/utilrb/ruby_object_graph.rb +0 -384
  52. data/test/data/test_pkgconfig.pc +0 -9
  53. data/test/data/test_pkgconfig_empty.pc +0 -10
  54. data/test/test_array.rb +0 -15
  55. data/test/test_config.rb +0 -4
  56. data/test/test_dir.rb +0 -22
  57. data/test/test_enumerable.rb +0 -119
  58. data/test/test_event_loop.rb +0 -407
  59. data/test/test_exception.rb +0 -38
  60. data/test/test_gc.rb +0 -34
  61. data/test/test_hash.rb +0 -102
  62. data/test/test_kernel.rb +0 -300
  63. data/test/test_logger.rb +0 -204
  64. data/test/test_misc.rb +0 -42
  65. data/test/test_models.rb +0 -212
  66. data/test/test_module.rb +0 -126
  67. data/test/test_object.rb +0 -77
  68. data/test/test_objectstats.rb +0 -26
  69. data/test/test_pkgconfig.rb +0 -84
  70. data/test/test_proc.rb +0 -31
  71. data/test/test_set.rb +0 -19
  72. data/test/test_thread_pool.rb +0 -409
  73. data/test/test_time.rb +0 -47
  74. data/test/test_unbound_method.rb +0 -23
  75. data/test/test_weakref.rb +0 -81
data/manifest.xml ADDED
@@ -0,0 +1,19 @@
1
+ <package>
2
+ <description brief="Ruby toolkit">
3
+ This library is a collection of useful Ruby classes
4
+ </description>
5
+ <maintainer>Sylvain Joyeux/sylvain.joyeux@m4x.org</maintainer>
6
+ <copyright>
7
+ LAAS-CNRS/openrobots@laas.fr
8
+ DFKI/robotik@dfki.de
9
+ Sylvain Joyeux/sylvain.joyeux@m4x.org
10
+ </copyright>
11
+ <license>CeCILL-B (BSD-like)</license>
12
+
13
+ <rosdep name="ruby"/>
14
+ <rosdep name="bundler"/>
15
+ <rosdep name="facets" />
16
+ <rosdep name="rake-compiler" />
17
+ <test_depend package="flexmock" />
18
+
19
+ </package>
data/package.xml ADDED
@@ -0,0 +1,29 @@
1
+ <package>
2
+ <name>utilrb</name>
3
+ <version>1.6.6</version>
4
+ <description>
5
+ Ruby toolkit: This library is a collection of useful Ruby classes
6
+ </description>
7
+ <author>Sylvain Joyeux/sylvain.joyeux@m4x.org</author>
8
+ <maintainer email="orocos-dev@lists.mech.kuleuven.be">Orocos Development Team</maintainer>
9
+ <license>CeCILL-B (BSD-like)</license>
10
+
11
+ <buildtool_depend>cmake</buildtool_depend>
12
+ <run_depend>catkin</run_depend>
13
+
14
+ <build_depend>ruby</build_depend>
15
+ <build_depend>facets</build_depend>
16
+ <build_depend>hoe</build_depend>
17
+ <build_depend>rake-compiler</build_depend>
18
+
19
+ <run_depend>ruby</run_depend>
20
+ <run_depend>facets</run_depend>
21
+ <run_depend>hoe</run_depend>
22
+
23
+ <export>
24
+ <build_type>
25
+ cmake
26
+ </build_type>
27
+ </export>
28
+
29
+ </package>
data/utilrb.gemspec ADDED
@@ -0,0 +1,27 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'utilrb/version'
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = "utilrb"
8
+ s.version = Utilrb::VERSION
9
+ s.authors = ["Sylvain Joyeux"]
10
+ s.email = "sylvain.joyeux@m4x.org"
11
+ s.summary = "Utilrb is yet another Ruby toolkit, in the spirit of facets"
12
+ s.description = "Utilrb is yet another Ruby toolkit, in the spirit of facets. It includes all\nthe standard class extensions I use in other projects."
13
+ s.homepage = "http://rock-robotics.org"
14
+ s.licenses = ["BSD"]
15
+
16
+ s.require_paths = ["lib"]
17
+ s.extensions = []
18
+ s.extra_rdoc_files = ["History.txt", "License.txt", "Manifest.txt", "History.txt"]
19
+ s.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
20
+
21
+ s.add_runtime_dependency "facets", ">= 2.4.0"
22
+ s.add_runtime_dependency "rake", ">= 0.9"
23
+ s.add_runtime_dependency "rake-compiler", "~> 0.8.0"
24
+ s.add_development_dependency "flexmock", ">= 2.0.0"
25
+ s.add_development_dependency "minitest", ">= 5.0", "~> 5.0"
26
+ s.add_development_dependency "coveralls"
27
+ end
metadata CHANGED
@@ -1,141 +1,135 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: utilrb
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.0.2.b2
4
+ version: 2.1.0.rc1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sylvain Joyeux
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-06-13 00:00:00.000000000 Z
11
+ date: 2015-09-26 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: facets
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - '>='
17
+ - - ">="
18
18
  - !ruby/object:Gem::Version
19
19
  version: 2.4.0
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
- - - '>='
24
+ - - ">="
25
25
  - !ruby/object:Gem::Version
26
26
  version: 2.4.0
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: rake
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
- - - '>='
31
+ - - ">="
32
32
  - !ruby/object:Gem::Version
33
33
  version: '0.9'
34
34
  type: :runtime
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
- - - '>='
38
+ - - ">="
39
39
  - !ruby/object:Gem::Version
40
40
  version: '0.9'
41
41
  - !ruby/object:Gem::Dependency
42
- name: hoe
42
+ name: rake-compiler
43
43
  requirement: !ruby/object:Gem::Requirement
44
44
  requirements:
45
- - - '>='
45
+ - - "~>"
46
46
  - !ruby/object:Gem::Version
47
- version: 3.7.1
47
+ version: 0.8.0
48
48
  type: :runtime
49
49
  prerelease: false
50
50
  version_requirements: !ruby/object:Gem::Requirement
51
51
  requirements:
52
- - - '>='
52
+ - - "~>"
53
53
  - !ruby/object:Gem::Version
54
- version: 3.7.1
54
+ version: 0.8.0
55
55
  - !ruby/object:Gem::Dependency
56
- name: hoe-yard
56
+ name: flexmock
57
57
  requirement: !ruby/object:Gem::Requirement
58
58
  requirements:
59
- - - '>='
59
+ - - ">="
60
60
  - !ruby/object:Gem::Version
61
- version: 0.1.2
62
- type: :runtime
61
+ version: 2.0.0
62
+ type: :development
63
63
  prerelease: false
64
64
  version_requirements: !ruby/object:Gem::Requirement
65
65
  requirements:
66
- - - '>='
66
+ - - ">="
67
67
  - !ruby/object:Gem::Version
68
- version: 0.1.2
68
+ version: 2.0.0
69
69
  - !ruby/object:Gem::Dependency
70
- name: rake-compiler
70
+ name: minitest
71
71
  requirement: !ruby/object:Gem::Requirement
72
72
  requirements:
73
- - - '>='
74
- - !ruby/object:Gem::Version
75
- version: '0'
76
- type: :runtime
77
- prerelease: false
78
- version_requirements: !ruby/object:Gem::Requirement
79
- requirements:
80
- - - '>='
73
+ - - ">="
81
74
  - !ruby/object:Gem::Version
82
- version: '0'
83
- - !ruby/object:Gem::Dependency
84
- name: flexmock
85
- requirement: !ruby/object:Gem::Requirement
86
- requirements:
87
- - - '>='
75
+ version: '5.0'
76
+ - - "~>"
88
77
  - !ruby/object:Gem::Version
89
- version: 0.8.6
78
+ version: '5.0'
90
79
  type: :development
91
80
  prerelease: false
92
81
  version_requirements: !ruby/object:Gem::Requirement
93
82
  requirements:
94
- - - '>='
83
+ - - ">="
95
84
  - !ruby/object:Gem::Version
96
- version: 0.8.6
85
+ version: '5.0'
86
+ - - "~>"
87
+ - !ruby/object:Gem::Version
88
+ version: '5.0'
97
89
  - !ruby/object:Gem::Dependency
98
- name: debugger-ruby_core_source
90
+ name: coveralls
99
91
  requirement: !ruby/object:Gem::Requirement
100
92
  requirements:
101
- - - '>='
93
+ - - ">="
102
94
  - !ruby/object:Gem::Version
103
95
  version: '0'
104
96
  type: :development
105
97
  prerelease: false
106
98
  version_requirements: !ruby/object:Gem::Requirement
107
99
  requirements:
108
- - - '>='
100
+ - - ">="
109
101
  - !ruby/object:Gem::Version
110
102
  version: '0'
111
103
  description: |-
112
104
  Utilrb is yet another Ruby toolkit, in the spirit of facets. It includes all
113
105
  the standard class extensions I use in other projects.
114
- email:
115
- - sylvain.joyeux@m4x.org
106
+ email: sylvain.joyeux@m4x.org
116
107
  executables: []
117
- extensions:
118
- - ext/utilrb/extconf.rb
108
+ extensions: []
119
109
  extra_rdoc_files:
120
110
  - History.txt
121
111
  - License.txt
122
112
  - Manifest.txt
123
113
  files:
124
- - .gemtest
114
+ - ".boring"
115
+ - ".gemtest"
116
+ - ".gitignore"
117
+ - ".travis.yml"
118
+ - CMakeLists.txt
119
+ - Gemfile
125
120
  - History.txt
126
121
  - License.txt
122
+ - Makefile
127
123
  - Manifest.txt
128
- - README.rd
124
+ - README.md
129
125
  - Rakefile
126
+ - benchmarks/validate_options.rb
130
127
  - bm/allocation.rb
131
128
  - bm/speed.rb
132
129
  - ext/utilrb/extconf.rb
133
- - ext/utilrb/proc.c
134
- - ext/utilrb/readline.c
135
130
  - ext/utilrb/ruby_allocator.hh
136
131
  - ext/utilrb/utilrb.cc
137
132
  - ext/utilrb/value_set.cc
138
- - ext/utilrb/weakref.cc
139
133
  - lib/utilrb.rb
140
134
  - lib/utilrb/array.rb
141
135
  - lib/utilrb/array/to_s.rb
@@ -180,8 +174,6 @@ files:
180
174
  - lib/utilrb/logger/root.rb
181
175
  - lib/utilrb/logger/silent.rb
182
176
  - lib/utilrb/marshal/load_with_missing_constants.rb
183
- - lib/utilrb/models/inherited_enumerable.rb
184
- - lib/utilrb/models/registration.rb
185
177
  - lib/utilrb/module.rb
186
178
  - lib/utilrb/module/ancestor_p.rb
187
179
  - lib/utilrb/module/attr_enumerable.rb
@@ -192,26 +184,26 @@ files:
192
184
  - lib/utilrb/module/define_or_reuse.rb
193
185
  - lib/utilrb/module/dsl_attribute.rb
194
186
  - lib/utilrb/module/include.rb
195
- - lib/utilrb/module/inherited_enumerable.rb
187
+ - lib/utilrb/module/is_singleton.rb
188
+ - lib/utilrb/module/singleton_class_p.rb
196
189
  - lib/utilrb/object.rb
197
190
  - lib/utilrb/object/address.rb
198
191
  - lib/utilrb/object/attribute.rb
199
192
  - lib/utilrb/object/scoped_eval.rb
200
193
  - lib/utilrb/object/singleton_class.rb
201
- - lib/utilrb/objectstats.rb
202
194
  - lib/utilrb/pathname.rb
203
195
  - lib/utilrb/pathname/find_matching_parent.rb
204
196
  - lib/utilrb/pkgconfig.rb
205
197
  - lib/utilrb/qt/mime_data/mime_data.rb
206
198
  - lib/utilrb/qt/variant/from_ruby.rb
207
199
  - lib/utilrb/rake_common.rb
208
- - lib/utilrb/ruby_object_graph.rb
209
200
  - lib/utilrb/set.rb
210
201
  - lib/utilrb/set/to_s.rb
211
202
  - lib/utilrb/socket/tcp_server.rb
212
203
  - lib/utilrb/socket/tcp_socket.rb
213
204
  - lib/utilrb/spawn.rb
214
205
  - lib/utilrb/symbol/to_str.rb
206
+ - lib/utilrb/test.rb
215
207
  - lib/utilrb/thread_pool.rb
216
208
  - lib/utilrb/time.rb
217
209
  - lib/utilrb/time/to_hms.rb
@@ -219,80 +211,37 @@ files:
219
211
  - lib/utilrb/unbound_method.rb
220
212
  - lib/utilrb/unbound_method/call.rb
221
213
  - lib/utilrb/value_set.rb
214
+ - lib/utilrb/version.rb
222
215
  - lib/utilrb/weakref.rb
223
216
  - lib/utilrb/yard.rb
217
+ - lib/yard-utilrb.rb
218
+ - manifest.xml
219
+ - package.xml
224
220
  - patches/gc_live_objects.patch
225
- - test/data/test_pkgconfig.pc
226
- - test/data/test_pkgconfig_empty.pc
227
- - test/test_array.rb
228
- - test/test_config.rb
229
- - test/test_dir.rb
230
- - test/test_enumerable.rb
231
- - test/test_event_loop.rb
232
- - test/test_exception.rb
233
- - test/test_gc.rb
234
- - test/test_hash.rb
235
- - test/test_kernel.rb
236
- - test/test_logger.rb
237
- - test/test_misc.rb
238
- - test/test_models.rb
239
- - test/test_module.rb
240
- - test/test_object.rb
241
- - test/test_objectstats.rb
242
- - test/test_pkgconfig.rb
243
- - test/test_proc.rb
244
- - test/test_set.rb
245
- - test/test_thread_pool.rb
246
- - test/test_time.rb
247
- - test/test_unbound_method.rb
248
- - test/test_weakref.rb
221
+ - utilrb.gemspec
249
222
  homepage: http://rock-robotics.org
250
223
  licenses:
251
224
  - BSD
252
225
  metadata: {}
253
226
  post_install_message:
254
- rdoc_options:
255
- - --title
256
- - TestUtilrb Documentation
257
- - --quiet
227
+ rdoc_options: []
258
228
  require_paths:
259
229
  - lib
260
230
  required_ruby_version: !ruby/object:Gem::Requirement
261
231
  requirements:
262
- - - '>='
232
+ - - ">="
263
233
  - !ruby/object:Gem::Version
264
- version: '2.0'
234
+ version: '0'
265
235
  required_rubygems_version: !ruby/object:Gem::Requirement
266
236
  requirements:
267
- - - '>'
237
+ - - ">"
268
238
  - !ruby/object:Gem::Version
269
239
  version: 1.3.1
270
240
  requirements: []
271
241
  rubyforge_project:
272
- rubygems_version: 2.0.14
242
+ rubygems_version: 2.2.3
273
243
  signing_key:
274
244
  specification_version: 4
275
245
  summary: Utilrb is yet another Ruby toolkit, in the spirit of facets
276
- test_files:
277
- - test/test_objectstats.rb
278
- - test/test_gc.rb
279
- - test/test_array.rb
280
- - test/test_enumerable.rb
281
- - test/test_weakref.rb
282
- - test/test_config.rb
283
- - test/test_event_loop.rb
284
- - test/test_unbound_method.rb
285
- - test/test_set.rb
286
- - test/test_models.rb
287
- - test/test_time.rb
288
- - test/test_kernel.rb
289
- - test/test_proc.rb
290
- - test/test_object.rb
291
- - test/test_thread_pool.rb
292
- - test/test_dir.rb
293
- - test/test_hash.rb
294
- - test/test_exception.rb
295
- - test/test_logger.rb
296
- - test/test_misc.rb
297
- - test/test_module.rb
298
- - test/test_pkgconfig.rb
246
+ test_files: []
247
+ has_rdoc:
data/ext/utilrb/proc.c DELETED
@@ -1,39 +0,0 @@
1
- #ifdef HAS_RUBY_SOURCE
2
- #include <vm_core.h>
3
-
4
- static VALUE env_references(VALUE rbenv)
5
- {
6
- rb_env_t* env;
7
-
8
- VALUE result = rb_ary_new();
9
- GetEnvPtr(rbenv, env);
10
- if (env->env)
11
- {
12
- int i;
13
- for (i = 0; i < env->env_size; ++i)
14
- rb_ary_push(result, rb_obj_id(env->env[i]));
15
- }
16
- return result;
17
- }
18
-
19
- static VALUE proc_references(VALUE rbproc)
20
- {
21
- rb_proc_t* proc;
22
- GetProcPtr(rbproc, proc);
23
-
24
- if (!NIL_P(proc->envval))
25
- return env_references(proc->envval);
26
- return rb_ary_new();
27
- }
28
- #elif RUBY_IS_18
29
- #warning "compiling on Ruby 1.8, Proc#references will not be available"
30
- #else
31
- #warning "Ruby core sources cannot be found, Proc#references will not be available. Install the debugger-ruby_core_source gem to enable"
32
- #endif
33
-
34
- void Init_proc()
35
- {
36
- #ifdef HAS_RUBY_SOURCE
37
- rb_define_method(rb_cProc, "references", RUBY_METHOD_FUNC(proc_references), 0);
38
- #endif
39
- }
@@ -1,52 +0,0 @@
1
- #include <stdio.h>
2
- #include <readline/readline.h>
3
- #include <ruby.h>
4
-
5
- static VALUE readline_save_prompt(VALUE self)
6
- {
7
- rl_save_prompt();
8
- return Qnil;
9
- }
10
-
11
- static VALUE readline_message(VALUE self, VALUE msg)
12
- {
13
- rl_message("%s", StringValuePtr(msg));
14
- rl_redisplay();
15
- return Qnil;
16
- }
17
-
18
- static VALUE readline_print(VALUE self, VALUE msg)
19
- {
20
- int need_hack = (rl_readline_state & RL_STATE_READCMD) > 0;
21
- char *saved_line;
22
- int saved_point;
23
- if (need_hack)
24
- {
25
- saved_point = rl_point;
26
- saved_line = rl_copy_text(0, rl_end);
27
- rl_save_prompt();
28
- rl_replace_line("", 0);
29
- rl_redisplay();
30
- }
31
-
32
- printf("%s", StringValuePtr(msg));
33
-
34
- if (need_hack)
35
- {
36
- rl_restore_prompt();
37
- rl_replace_line(saved_line, 0);
38
- rl_point = saved_point;
39
- rl_redisplay();
40
- free(saved_line);
41
- }
42
- return Qnil;
43
- }
44
-
45
- extern void Init_utilrb_readline()
46
- {
47
- VALUE mReadline = rb_define_module("Readline");
48
- rb_define_singleton_method(mReadline, "save_prompt", readline_save_prompt, 0);
49
- rb_define_singleton_method(mReadline, "message", readline_message, 1);
50
- rb_define_singleton_method(mReadline, "print", readline_print, 1);
51
- }
52
-
@@ -1,143 +0,0 @@
1
- #include <set>
2
- #include <map>
3
- #include <ruby.h>
4
- #ifdef RUBY_IS_18
5
- #include <intern.h>
6
- #else
7
- #include <ruby/intern.h>
8
- #endif
9
-
10
- using std::set;
11
- using std::map;
12
-
13
- static VALUE cWeakRef;
14
- static VALUE cRefError;
15
-
16
- /* Weakref internal structure. +obj+ is Qnil before initialization and Qundef
17
- * after finalization */
18
- struct WeakRef {
19
- VALUE ruby_ref;
20
- VALUE obj;
21
- };
22
-
23
- // Map from real objects to the set of associated WeakRef objects
24
- typedef set<VALUE> ObjSet;
25
- typedef map< VALUE, ObjSet > RefFromObjID;
26
- typedef map< VALUE, VALUE > ObjFromRefID;
27
- RefFromObjID from_obj_id;
28
- ObjFromRefID from_ref_id;
29
-
30
- static void weakref_free(WeakRef const* ref)
31
- {
32
- VALUE ref_id = rb_obj_id(ref->ruby_ref);
33
- ObjFromRefID::iterator obj_it = from_ref_id.find(ref_id);
34
- if (obj_it != from_ref_id.end())
35
- {
36
- VALUE obj_id = rb_obj_id(obj_it->second);
37
- RefFromObjID::iterator ref_set = from_obj_id.find(obj_id);
38
- ref_set->second.erase(ref->ruby_ref);
39
- from_ref_id.erase(obj_it);
40
- }
41
- delete ref;
42
- }
43
- static VALUE weakref_alloc(VALUE klass)
44
- {
45
- WeakRef* ref = new WeakRef;
46
- ref->obj = Qnil;
47
- ref->ruby_ref = Data_Wrap_Struct(klass, NULL, weakref_free, ref);
48
- return ref->ruby_ref;
49
- }
50
-
51
- static WeakRef& get_weakref(VALUE self)
52
- {
53
- WeakRef* object = 0;
54
- Data_Get_Struct(self, WeakRef, object);
55
- return *object;
56
- }
57
-
58
- static VALUE do_object_finalize(VALUE mod, VALUE obj_id)
59
- {
60
- RefFromObjID::iterator ref_set = from_obj_id.find(obj_id);
61
- if (ref_set != from_obj_id.end())
62
- {
63
- ObjSet::iterator it = ref_set->second.begin();
64
- ObjSet::iterator const end = ref_set->second.end();
65
- for (; it != end; ++it)
66
- {
67
- // Do NOT use Data_Wrap_Struct here, it would break on recent Ruby
68
- // interpreters. The reason is that the object type is reset during
69
- // GC -- and the call to free functions is deferred.
70
- //
71
- // So, at this point, we're sure we have a RDATA in *it (otherwise
72
- // weakref_free would have been called), but we can't use
73
- // Data_Wrap_Struct because that would crash.
74
- WeakRef& ref = *reinterpret_cast<WeakRef*>(RDATA(*it)->data);;
75
- ref.obj = Qundef;
76
- from_ref_id.erase(rb_obj_id(*it));
77
- }
78
- from_obj_id.erase(obj_id);
79
- }
80
- return Qnil;
81
- }
82
-
83
- // Note: the Ruby code has already registered +do_object_finalize+ as the
84
- // finalizer for +obj+.
85
- //
86
- // It is forbidden to make a weakref-of-weakref or a weakref of an immediate
87
- // object
88
- static VALUE weakref_do_initialize(VALUE self, VALUE obj)
89
- {
90
- if (!FL_ABLE(obj))
91
- {
92
- VALUE str = rb_any_to_s(obj);
93
- rb_raise(rb_eArgError, "%s cannot be finalized", StringValuePtr(str));
94
- }
95
-
96
- WeakRef& ref = get_weakref(self);
97
- ref.obj = obj;
98
-
99
- RefFromObjID::iterator it = from_obj_id.find(rb_obj_id(obj));
100
- if (it == from_obj_id.end())
101
- it = from_obj_id.insert( make_pair(rb_obj_id(obj), ObjSet()) ).first;
102
-
103
- it->second.insert(self);
104
- from_ref_id.insert( std::make_pair(rb_obj_id(self), obj) );
105
-
106
- return Qnil;
107
- }
108
-
109
- static VALUE weakref_get(VALUE self)
110
- {
111
- WeakRef const& ref = get_weakref(self);
112
-
113
- if (ref.obj == Qnil)
114
- rb_raise(cRefError, "initialized weakref");
115
- if (ref.obj == Qundef)
116
- rb_raise(cRefError, "finalized object");
117
- return ref.obj;
118
- }
119
-
120
- static VALUE refcount(VALUE mod, VALUE obj)
121
- {
122
- if (0 == (obj & FIXNUM_FLAG))
123
- obj = rb_obj_id(obj);
124
-
125
- RefFromObjID::const_iterator it = from_obj_id.find(obj);
126
- if (it == from_obj_id.end())
127
- return Qnil;
128
- else
129
- return INT2FIX(it->second.size());
130
- }
131
-
132
- extern "C" void Init_weakref(VALUE mUtilrb)
133
- {
134
- cWeakRef = rb_define_class_under(mUtilrb, "WeakRef", rb_cObject);
135
- cRefError = rb_define_class_under(cWeakRef, "RefError", rb_eStandardError);
136
- rb_define_alloc_func(cWeakRef, weakref_alloc);
137
-
138
- rb_define_singleton_method(cWeakRef, "do_object_finalize", RUBY_METHOD_FUNC(do_object_finalize), 1);
139
- rb_define_singleton_method(cWeakRef, "refcount", RUBY_METHOD_FUNC(refcount), 1);
140
- rb_define_method(cWeakRef, "do_initialize", RUBY_METHOD_FUNC(weakref_do_initialize), 1);
141
- rb_define_method(cWeakRef, "get", RUBY_METHOD_FUNC(weakref_get), 0);
142
- }
143
-