cgen 0.16.1 → 0.16.2
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.
- data/.gitignore +2 -2
- data/History.txt +10 -0
- data/examples/example-ruby-talk-26July2009.rb +43 -0
- data/lib/cgen/cgen.rb +12 -3
- data/lib/cgen/cshadow.rb +33 -31
- data/rakefile +1 -2
- metadata +4 -3
data/.gitignore
CHANGED
data/History.txt
CHANGED
@@ -0,0 +1,43 @@
|
|
1
|
+
require 'cgen/cshadow'
|
2
|
+
|
3
|
+
class Complex < Numeric
|
4
|
+
include CShadow
|
5
|
+
|
6
|
+
shadow_attr_accessor :re => "double re"
|
7
|
+
shadow_attr_accessor :im => "double im"
|
8
|
+
|
9
|
+
def initialize re, im
|
10
|
+
self.re = re
|
11
|
+
self.im = im
|
12
|
+
end
|
13
|
+
|
14
|
+
define_c_method(:abs) {
|
15
|
+
include "<math.h>"
|
16
|
+
returns "rb_float_new(sqrt(pow(shadow->re, 2) + pow(shadow->im, 2)))"
|
17
|
+
}
|
18
|
+
|
19
|
+
define_c_method(:scale!) {
|
20
|
+
c_array_args {
|
21
|
+
optional :factor
|
22
|
+
default :factor => "INT2NUM(10)"
|
23
|
+
typecheck :factor => Numeric
|
24
|
+
}
|
25
|
+
body %{
|
26
|
+
shadow->re *= NUM2DBL(factor);
|
27
|
+
shadow->im *= NUM2DBL(factor);
|
28
|
+
}
|
29
|
+
returns "self"
|
30
|
+
}
|
31
|
+
end
|
32
|
+
|
33
|
+
Complex.commit
|
34
|
+
|
35
|
+
z = Complex.new 5, 1.3
|
36
|
+
p z # ==> #<Complex:0xb7dc0098 re=5.0, im=1.3>
|
37
|
+
puts z.abs # ==> 5.1662365412358
|
38
|
+
z.scale! 3.0 # float
|
39
|
+
p [z.re, z.im] # ==> [15.0, 3.9]
|
40
|
+
z.scale! 3 # int
|
41
|
+
p [z.re, z.im] # ==> [45.0, 11.7]
|
42
|
+
z.scale! # use default value
|
43
|
+
p [z.re, z.im] # ==> [450.0, 117]
|
data/lib/cgen/cgen.rb
CHANGED
@@ -418,7 +418,7 @@ require 'cgen/inherit'
|
|
418
418
|
# way that makes clear that the problem is really with commit.
|
419
419
|
module CGenerator
|
420
420
|
|
421
|
-
VERSION = '0.16.
|
421
|
+
VERSION = '0.16.2'
|
422
422
|
|
423
423
|
class Accumulator ## should be a mixin? "Cumulative"?
|
424
424
|
|
@@ -888,10 +888,19 @@ class Library < Template
|
|
888
888
|
build_wrapper do
|
889
889
|
cfg = Config::CONFIG
|
890
890
|
dirs = [cfg["sitearchdir"], cfg["archdir"], cfg["includedir"]]
|
891
|
+
|
892
|
+
makedepend_cmd =
|
893
|
+
case cfg["CC"]
|
894
|
+
when "gcc"
|
895
|
+
"gcc -MM -MF depend"
|
896
|
+
else
|
897
|
+
"makedepend -fdepend"
|
898
|
+
end
|
899
|
+
|
891
900
|
result = system %{
|
892
901
|
touch depend
|
893
|
-
|
894
|
-
}
|
902
|
+
#{makedepend_cmd} *.c -I#{dirs.join " -I"} >#{@logname} 2>&1
|
903
|
+
}
|
895
904
|
unless result
|
896
905
|
log_data = File.read(@logname) rescue nil
|
897
906
|
msg = "\n makedepend failed for #{@name}."
|
data/lib/cgen/cshadow.rb
CHANGED
@@ -23,8 +23,10 @@ require "cgen/attribute"
|
|
23
23
|
#
|
24
24
|
# ==Usage
|
25
25
|
#
|
26
|
-
#
|
27
|
-
#
|
26
|
+
# class MyClass
|
27
|
+
# include CShadow
|
28
|
+
# shadow_attr_accessor :x => "int x" # fox example
|
29
|
+
# end
|
28
30
|
#
|
29
31
|
# Include CShadow in the base class(es) that need to have shadow attributes. The
|
30
32
|
# base class is assigned a CGenerator::Library, which can be accessed using
|
@@ -54,7 +56,7 @@ require "cgen/attribute"
|
|
54
56
|
# Shadow classes that are placed in the same library can still be put in
|
55
57
|
# separate C source files, using #shadow_library_file:
|
56
58
|
#
|
57
|
-
#
|
59
|
+
# shadow_library_file <CFile or String>
|
58
60
|
#
|
59
61
|
# This setting is inherited (and can be overridden) by subclasses of the current
|
60
62
|
# class. If a class calls both #shadow_library and #shadow_library_file then
|
@@ -181,66 +183,66 @@ require "cgen/attribute"
|
|
181
183
|
# ==Limitations:
|
182
184
|
#
|
183
185
|
# * Hash args are ordered unpredictably, so if struct member order is
|
184
|
-
#
|
185
|
-
#
|
186
|
-
# take note of the self pointer at the beginning of the struct.
|
186
|
+
# significant (for example, because you want to pass the struct to C code that
|
187
|
+
# expects it that way), use a separate declare statement for each member.
|
188
|
+
# Also, take note of the self pointer at the beginning of the struct.
|
187
189
|
#
|
188
190
|
# * Creating a ruby+shadow object has a bit more time/space overhead than just a
|
189
|
-
#
|
190
|
-
#
|
191
|
-
#
|
192
|
-
#
|
191
|
+
# C object, so CShadow may not be the best mechansism for managing heap
|
192
|
+
# allocated data (for example, if you want lots of 2-element arrays). Also,
|
193
|
+
# CShadow objects are fixed in size (though their members can point to other
|
194
|
+
# structures).
|
193
195
|
#
|
194
196
|
# * CShadow attributes are, of course, not dynamic. They are fixed at the time
|
195
|
-
#
|
196
|
-
#
|
197
|
-
#
|
198
|
-
#
|
199
|
-
#
|
200
|
-
# receiverless form.
|
197
|
+
# of #commit. Otherwise, they behave essentially like Ruby attributes, except
|
198
|
+
# that they can be accessed only with methods or from C code; they cannot be
|
199
|
+
# accessed with the @ notation. Of course, the reader and writer for a shadow
|
200
|
+
# attribute can be flagged as protected or private. However, a private writer
|
201
|
+
# cannot be used, since by definition private methods can only be called in
|
202
|
+
# the receiverless form.
|
201
203
|
#
|
202
204
|
# * CShadow is designed for efficient in-memory structs, not packed,
|
203
|
-
#
|
204
|
-
#
|
205
|
+
# network-ordered data as for example in network protocols. See the
|
206
|
+
# bit-struct project for the latter.
|
205
207
|
#
|
206
208
|
# ==To do:
|
207
209
|
#
|
208
210
|
# * It should be easier to get a handle to entities. Below, shadow_attr has been
|
209
|
-
#
|
210
|
-
#
|
211
|
+
# hacked to return a list of pairs of functions. But it should be easier and
|
212
|
+
# more general.
|
211
213
|
#
|
212
214
|
# * Optimization: if class A<B, and their free func have the same content, use
|
213
|
-
#
|
214
|
-
# the other kinds of functions.
|
215
|
+
# B's function in A, and don't generate a new function for A. Similarly for
|
216
|
+
# all the other kinds of functions.
|
215
217
|
#
|
216
218
|
# * Allow
|
217
219
|
#
|
218
|
-
#
|
220
|
+
# shadow_attr "int x", "double y"
|
219
221
|
#
|
220
|
-
#
|
222
|
+
# or even
|
221
223
|
#
|
222
|
-
#
|
224
|
+
# attr_accessor :a, :b, "int x", "double y"
|
223
225
|
#
|
224
|
-
#
|
226
|
+
# and (in cgen)
|
225
227
|
#
|
226
|
-
#
|
228
|
+
# declare "int x", "double y"
|
227
229
|
#
|
228
|
-
#
|
230
|
+
# The ruby name will be extracted from the string using the matching pattern.
|
229
231
|
#
|
230
232
|
# * Generate documentation for the shadow class.
|
231
233
|
#
|
232
234
|
# * Change name to CStruct? CStructure?
|
233
235
|
#
|
234
236
|
# * Find a way to propagate append_features so that CShadow can be included in
|
235
|
-
#
|
237
|
+
# modules and modules can contribute shadow_attrs.
|
236
238
|
#
|
237
239
|
# * option to omit the "self" pointer, or put it at the end of the struct
|
238
|
-
#
|
240
|
+
# automatically omit it in a class if no ShadowObjectAttributes point to it?
|
239
241
|
#
|
240
242
|
# * shadow_struct_constructor class method to use DATA_WRAP_STRUCT
|
241
243
|
#
|
242
244
|
# * Use CNativeAttribute as a default attribute? Or use the attr class hierarchy
|
243
|
-
#
|
245
|
+
# to supply defaults?
|
244
246
|
#
|
245
247
|
module CShadow
|
246
248
|
SHADOW_SUFFIX = "_Shadow"
|
data/rakefile
CHANGED
@@ -31,12 +31,11 @@ programs. Includes CShadow module for defining classes in terms of C structs
|
|
31
31
|
with accessors and inheritance.
|
32
32
|
END
|
33
33
|
PROJ.changes = File.read(PROJ.history_file)[/^\w.*?(?=^\w)/m]
|
34
|
+
(PROJ.rdoc.dir = File.readlink(PROJ.rdoc.dir)) rescue nil
|
34
35
|
|
35
36
|
PROJ.spec.opts << '--color'
|
36
37
|
PROJ.test.files = Dir["test/test-*.rb"]
|
37
38
|
|
38
|
-
### pkg dir in tmp
|
39
|
-
|
40
39
|
task :release => ["gem:release", "doc:release"]
|
41
40
|
|
42
41
|
# EOF
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: cgen
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.16.
|
4
|
+
version: 0.16.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Joel VanderWerf
|
@@ -9,7 +9,7 @@ autorequire:
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date: 2009-
|
12
|
+
date: 2009-09-20 00:00:00 -07:00
|
13
13
|
default_executable:
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
@@ -50,6 +50,7 @@ files:
|
|
50
50
|
- examples/ctest3.rb
|
51
51
|
- examples/ctest4.rb
|
52
52
|
- examples/ctest5.rb
|
53
|
+
- examples/example-ruby-talk-26July2009.rb
|
53
54
|
- examples/example-ruby-talk-30April2004.rb
|
54
55
|
- examples/fixed-array.rb
|
55
56
|
- examples/inherit-example.rb
|
@@ -118,6 +119,6 @@ signing_key:
|
|
118
119
|
specification_version: 3
|
119
120
|
summary: Library for packed binary data stored in ruby Strings
|
120
121
|
test_files:
|
121
|
-
- test/test-attribute.rb
|
122
122
|
- test/test-cgen.rb
|
123
|
+
- test/test-attribute.rb
|
123
124
|
- test/test-cshadow.rb
|