ruby-gr 0.0.5 → 0.0.6

Sign up to get free protection for your applications and to get access to all the features.
data/lib/gr3/ffi.rb CHANGED
@@ -1,95 +1,131 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'ffi'
3
+ require 'fiddle/import'
4
4
 
5
5
  module GR3
6
6
  # FFI Wrapper module for GR3
7
7
  module FFI
8
- extend ::FFI::Library
8
+ extend Fiddle::Importer
9
9
 
10
10
  begin
11
- ffi_lib GR3.ffi_lib
11
+ dlload GR3.ffi_lib
12
12
  rescue LoadError
13
13
  raise LoadError, 'Could not find GR Framework'
14
14
  end
15
15
 
16
- extend GRCommons::AttachFunction
16
+ extend GRCommons::Extern
17
17
 
18
18
  # https://github.com/sciapp/gr/blob/master/lib/gr3/gr3.h
19
19
 
20
- attach_function :gr3_init, %i[int], :int
21
- attach_function :gr3_free, %i[pointer], :void
22
- attach_function :gr3_terminate, %i[], :void
23
- attach_function :gr3_geterror, %i[int pointer pointer], :int
24
- attach_function :gr3_getrenderpathstring, %i[], :string
25
- attach_function :gr3_geterrorstring, %i[int], :string
26
- # callback :gr3_log_func, %i[string], :void
27
- # attach_function :gr3_setlogcallback, %i[:gr3_log_func], :void
28
- attach_function :gr3_clear, %i[], :int
29
- attach_function :gr3_usecurrentframebuffer, %i[], :void
30
- attach_function :gr3_useframebuffer, %i[uint], :void
31
- attach_function :gr3_setquality, %i[int], :int
32
- attach_function :gr3_getimage, %i[int int int pointer], :int
33
- attach_function :gr3_export, %i[pointer int int], :int
34
- attach_function :gr3_drawimage, %i[float float float float int int int], :int
35
- attach_function :gr3_createmesh_nocopy, %i[pointer int pointer pointer pointer], :int
36
- attach_function :gr3_createmesh, %i[pointer int pointer pointer pointer], :int
37
- attach_function :gr3_createindexedmesh_nocopy, %i[pointer int pointer pointer pointer int pointer], :int
38
- attach_function :gr3_createindexedmesh, %i[pointer int pointer pointer pointer int pointer], :int
39
- attach_function :gr3_drawmesh, %i[int int pointer pointer pointer pointer pointer], :void
40
- attach_function :gr3_deletemesh, %i[int], :void
41
- attach_function :gr3_cameralookat, %i[float float float float float float float float float], :void
42
- attach_function :gr3_setcameraprojectionparameters, %i[float float float], :int
43
- attach_function :gr3_getcameraprojectionparameters, %i[pointer pointer pointer], :int
44
- attach_function :gr3_setlightdirection, %i[float float float], :void
45
- attach_function :gr3_setbackgroundcolor, %i[float float float float], :void
46
- # getbackgroundcolor not implemented
47
- attach_function :gr3_createheightmapmesh, %i[pointer int int], :void
48
- attach_function :gr3_drawheightmap, %i[pointer int int pointer pointer], :void
49
- attach_function :gr3_drawconemesh, %i[int pointer pointer pointer pointer pointer], :void
50
- attach_function :gr3_drawcylindermesh, %i[int pointer pointer pointer pointer pointer], :void
51
- attach_function :gr3_drawspheremesh, %i[int pointer pointer pointer], :void
52
- attach_function :gr3_drawcubemesh, %i[int pointer pointer pointer pointer pointer], :void
53
- attach_function :gr3_setobjectid, %i[int], :void
54
- attach_function :gr3_selectid, %i[int int int int pointer], :int
55
- attach_function :gr3_getviewmatrix, %i[pointer], :void
56
- attach_function :gr3_setviewmatrix, %i[pointer], :void
57
- attach_function :gr3_getprojectiontype, %i[], :int
58
- attach_function :gr3_setprojectiontype, %i[int], :void
59
- # attach_function :gr3_triangulate,
60
- # %i[pointer ushort uint uint uint uint uint uint double double double double double double pointer], :void
61
- # attach_function :gr3_triangulateindexed,
62
- # %i[pointer ushort uint uint uint uint uint uint double double double double double double pointer pointer pointer pointer poiter], :void
63
- attach_function :gr3_createisosurfacemesh,
64
- %i[pointer pointer ushort uint uint uint uint uint uint double double double double double double],
65
- :int
66
- attach_function :gr3_createsurfacemesh, %i[pointer int int pointer pointer pointer int], :int
67
- attach_function :gr3_drawmesh_grlike, %i[int int pointer pointer pointer pointer pointer], :void
68
- attach_function :gr3_drawsurface, %i[int], :void
69
- attach_function :gr3_surface, %i[int int pointer pointer pointer int], :void
70
- attach_function :gr3_drawtubemesh, %i[int pointer pointer pointer int int], :int
71
- attach_function :gr3_createtubemesh, %i[pointer int pointer pointer pointer int int], :int
72
- attach_function :gr3_drawspins, %i[int pointer pointer pointer float float float float], :void
73
- attach_function :gr3_drawmolecule, %i[int pointer pointer pointer float pointer float], :void
74
- attach_function :gr3_createxslicemesh,
75
- %i[pointer pointer uint uint uint uint uint uint uint double double double double double double],
76
- :void
77
- attach_function :gr3_createyslicemesh,
78
- %i[pointer pointer uint uint uint uint uint uint uint double double double double double double],
79
- :void
80
- attach_function :gr3_createzslicemesh,
81
- %i[pointer pointer uint uint uint uint uint uint uint double double double double double double],
82
- :void
83
- attach_function :gr3_drawxslicemesh,
84
- %i[pointer uint uint uint uint uint uint uint double double double double double double],
85
- :void
86
- attach_function :gr3_drawyslicemesh,
87
- %i[pointer uint uint uint uint uint uint uint double double double double double double],
88
- :void
89
- attach_function :gr3_drawzslicemesh,
90
- %i[pointer uint uint uint uint uint uint uint double double double double double double],
91
- :void
92
- # attach_function :gr3_drawtrianglesurface, %i[int pointer], :void
93
- attach_function :gr_volume, %i[int int int pointer int pointer pointer], :void
20
+ extern 'int gr3_init(int *attrib_list)'
21
+ extern 'void gr3_free(void *pointer)'
22
+ extern 'void gr3_terminate(void)'
23
+ extern 'int gr3_geterror(int clear, int *line, const char **file)'
24
+ extern 'const char *gr3_getrenderpathstring(void)'
25
+ extern 'const char *gr3_geterrorstring(int error)'
26
+ # extern 'void gr3_setlogcallback(void (*gr3_log_func)(const char *log_message))'
27
+ extern 'int gr3_clear(void)'
28
+ extern 'void gr3_usecurrentframebuffer()'
29
+ extern 'void gr3_useframebuffer(unsigned int framebuffer)'
30
+ extern 'int gr3_setquality(int quality)'
31
+ extern 'int gr3_getimage(int width, int height, int use_alpha, char *pixels)'
32
+ extern 'int gr3_export(const char *filename, int width, int height)'
33
+ extern 'int gr3_drawimage(float xmin, float xmax, float ymin, float ymax, ' \
34
+ 'int width, int height, int drawable_type)'
35
+ extern 'int gr3_createmesh_nocopy(int *mesh, int n, ' \
36
+ 'float *vertices, float *normals, float *colors)'
37
+ extern 'int gr3_createmesh(int *mesh, int n, ' \
38
+ 'const float *vertices, const float *normals, const float *colors)'
39
+ extern 'int gr3_createindexedmesh_nocopy(int *mesh, int number_of_vertices, ' \
40
+ 'float *vertices, float *normals, float *colors, int number_of_indices, int *indices)'
41
+ extern 'int gr3_createindexedmesh(int *mesh, int number_of_vertices, ' \
42
+ 'const float *vertices, const float *normals, const float *colors, ' \
43
+ 'int number_of_indices, const int *indices)'
44
+ extern 'void gr3_drawmesh(int mesh, int n, ' \
45
+ 'const float *positions, const float *directions, const float *ups, ' \
46
+ 'const float *colors, const float *scales)'
47
+ extern 'void gr3_deletemesh(int mesh)'
48
+ extern 'void gr3_cameralookat(float camera_x, float camera_y, float camera_z, ' \
49
+ 'float center_x, float center_y, float center_z, ' \
50
+ 'float up_x, float up_y, float up_z)'
51
+ extern 'int gr3_setcameraprojectionparameters(float vertical_field_of_view, ' \
52
+ 'float zNear, float zFar)'
53
+ extern 'int gr3_getcameraprojectionparameters(float *vfov, float *znear, float *zfar)'
54
+ extern 'void gr3_setlightdirection(float x, float y, float z)'
55
+ extern 'void gr3_setbackgroundcolor(float red, float green, float blue, float alpha)'
56
+ extern 'int gr3_createheightmapmesh(const float *heightmap, int num_columns, int num_rows)'
57
+ extern 'void gr3_drawheightmap(const float *heightmap, ' \
58
+ 'int num_columns, int num_rows, const float *positions, const float *scales)'
59
+ extern 'void gr3_drawconemesh(int n, const float *positions, const float *directions, ' \
60
+ 'const float *colors, const float *radii, const float *lengths)'
61
+ extern 'void gr3_drawcylindermesh(int n, const float *positions, const float *directions, ' \
62
+ 'const float *colors, const float *radii, const float *lengths)'
63
+ extern 'void gr3_drawspheremesh(int n, const float *positions, ' \
64
+ 'const float *colors, const float *radii)'
65
+ extern 'void gr3_drawcubemesh(int n, ' \
66
+ 'const float *positions, const float *directions, const float *ups, ' \
67
+ 'const float *colors, const float *scales)'
68
+ extern 'void gr3_setobjectid(int id)'
69
+ extern 'int gr3_selectid(int x, int y, int width, int height, int *selection_id)'
70
+ extern 'void gr3_getviewmatrix(float *m)'
71
+ extern 'void gr3_setviewmatrix(const float *m)'
72
+ extern 'int gr3_getprojectiontype(void)'
73
+ extern 'void gr3_setprojectiontype(int type)'
74
+ # extern 'unsigned int gr3_triangulate(const unsigned short *data, unsigned short isolevel, unsigned int dim_x, unsigned int dim_y, unsigned int dim_z, unsigned int stride_x, unsigned int stride_y, unsigned int stride_z, double step_x, double step_y, double step_z, double offset_x, double offset_y, double offset_z, gr3_triangle_t **triangles_p)'
75
+ # extern 'void gr3_triangulateindexed(const unsigned short *data, unsigned short isolevel, unsigned int dim_x, unsigned int dim_y, unsigned int dim_z, unsigned int stride_x, unsigned int stride_y, unsigned int stride_z, double step_x, double step_y, double step_z, double offset_x, double offset_y, double offset_z, unsigned int *num_vertices, gr3_coord_t **vertices, gr3_coord_t **normals, unsigned int *num_indices, unsigned int **indices)'
76
+ extern 'int gr3_createisosurfacemesh(int *mesh, unsigned short *data, ' \
77
+ 'unsigned short isolevel, unsigned int dim_x, unsigned int dim_y, unsigned int dim_z, ' \
78
+ 'unsigned int stride_x, unsigned int stride_y, unsigned int stride_z, ' \
79
+ 'double step_x, double step_y, double step_z, ' \
80
+ 'double offset_x, double offset_y, double offset_z)'
81
+ extern 'int gr3_createsurfacemesh(int *mesh, int nx, int ny, ' \
82
+ 'float *px, float *py, float *pz, int option)'
83
+ extern 'void gr3_drawmesh_grlike(int mesh, int n, ' \
84
+ 'const float *positions, const float *directions, const float *ups, ' \
85
+ 'const float *colors, const float *scales)'
86
+ extern 'void gr3_drawsurface(int mesh)'
87
+ extern 'void gr3_surface(int nx, int ny, float *px, float *py, float *pz, int option)'
88
+ extern 'int gr3_drawtubemesh(int n, float *points, float *colors, float *radii, ' \
89
+ 'int num_steps, int num_segments)'
90
+ extern 'int gr3_createtubemesh(int *mesh, int n, ' \
91
+ 'const float *points, const float *colors, const float *radii, ' \
92
+ 'int num_steps, int num_segments)'
93
+ extern 'void gr3_drawspins(int n, const float *positions, const float *directions, const float *colors, ' \
94
+ 'float cone_radius, float cylinder_radius, float cone_height, float cylinder_height)'
95
+ extern 'void gr3_drawmolecule(int n, const float *positions, const float *colors, const float *radii, ' \
96
+ 'float bond_radius, const float bond_color[3], float bond_delta)'
97
+ extern 'void gr3_createxslicemesh(int *mesh, const unsigned short *data, ' \
98
+ 'unsigned int ix, unsigned int dim_x, unsigned int dim_y, unsigned int dim_z, ' \
99
+ 'unsigned int stride_x, unsigned int stride_y, unsigned int stride_z, ' \
100
+ 'double step_x, double step_y, double step_z, ' \
101
+ 'double offset_x, double offset_y, double offset_z)'
102
+ extern 'void gr3_createyslicemesh(int *mesh, const unsigned short *data, ' \
103
+ 'unsigned int iy, unsigned int dim_x, unsigned int dim_y, unsigned int dim_z, ' \
104
+ 'unsigned int stride_x, unsigned int stride_y, unsigned int stride_z, ' \
105
+ 'double step_x, double step_y, double step_z, ' \
106
+ 'double offset_x, double offset_y, double offset_z)'
107
+ extern 'void gr3_createzslicemesh(int *mesh, const unsigned short *data, ' \
108
+ 'unsigned int iz, unsigned int dim_x, unsigned int dim_y, unsigned int dim_z, ' \
109
+ 'unsigned int stride_x, unsigned int stride_y, unsigned int stride_z, ' \
110
+ 'double step_x, double step_y, double step_z, ' \
111
+ 'double offset_x, double offset_y, double offset_z)'
112
+ extern 'void gr3_drawxslicemesh(const unsigned short *data, unsigned int ix,' \
113
+ 'unsigned int dim_x, unsigned int dim_y, unsigned int dim_z, ' \
114
+ 'unsigned int stride_x, unsigned int stride_y, unsigned int stride_z, ' \
115
+ 'double step_x, double step_y, double step_z, ' \
116
+ 'double offset_x, double offset_y, double offset_z)'
117
+ extern 'void gr3_drawyslicemesh(const unsigned short *data, unsigned int iy, ' \
118
+ 'unsigned int dim_x, unsigned int dim_y, unsigned int dim_z, ' \
119
+ 'unsigned int stride_x, unsigned int stride_y, unsigned int stride_z, ' \
120
+ 'double step_x, double step_y, double step_z, ' \
121
+ 'double offset_x, double offset_y, double offset_z)'
122
+ extern 'void gr3_drawzslicemesh(const unsigned short *data, unsigned int iz, ' \
123
+ 'unsigned int dim_x, unsigned int dim_y, unsigned int dim_z, ' \
124
+ 'unsigned int stride_x, unsigned int stride_y, unsigned int stride_z, ' \
125
+ 'double step_x, double step_y, double step_z, ' \
126
+ 'double offset_x, double offset_y, double offset_z)'
127
+ # extern 'void gr3_drawtrianglesurface(int n, const float *triangles)'
128
+ extern 'void gr_volume(int nx, int ny, int nz, double *data, ' \
129
+ 'int algorithm, double *dmin_ptr, double *dmax_ptr)'
94
130
  end
95
131
  end
data/lib/gr3/gr3base.rb CHANGED
@@ -1,11 +1,16 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module GR3
4
+ # This module automatically converts Ruby arrays and Numo::Narray into pointers.
4
5
  module GR3Base
5
6
  extend GRCommons::DefineMethods
6
7
  define_ffi_methods(FFI,
7
8
  prefix: 'gr3_',
8
9
  default_type: :float)
10
+ # workaround for `gr_volume`
11
+ define_ffi_methods(FFI,
12
+ prefix: 'gr_',
13
+ default_type: :double)
9
14
  end
10
15
  private_constant :GR3Base
11
16
 
data/lib/gr_commons.rb CHANGED
@@ -4,7 +4,7 @@
4
4
  module GRCommons
5
5
  end
6
6
 
7
- require 'gr_commons/attach_function'
7
+ require 'gr_commons/extern'
8
8
  require 'gr_commons/define_methods'
9
9
  require 'gr_commons/gr_common_utils'
10
10
  require 'gr_commons/jupyter_support'
@@ -10,12 +10,14 @@ module GRCommons
10
10
  # delete_prefix (Ruby >= 2.5)
11
11
  method_name = method.to_s.sub(/^#{prefix}/, '')
12
12
 
13
+ # FIXME: Refactoring required
14
+
13
15
  define_method(method_name) do |*args|
14
16
  args.map! do |arg|
15
17
  case arg
16
18
  when Array
17
19
  send(default_type, arg)
18
- when ->(x) { narray? x }
20
+ when ->(x) { defined?(Numo::NArray) && x.is_a?(Numo::NArray) }
19
21
  send(default_type, arg)
20
22
  else
21
23
  arg
@@ -0,0 +1,16 @@
1
+ # frozen_string_literal: true
2
+
3
+ module GRCommons
4
+ # This module records the names of the methods defined by Fiddle::Importer.
5
+ module Extern
6
+ attr_reader :ffi_methods
7
+
8
+ # Remember added method name
9
+ def extern(*args)
10
+ @ffi_methods ||= []
11
+ func = super(*args)
12
+ @ffi_methods << func.name
13
+ func
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,419 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Fiddley v0.0.8
4
+ # https://github.com/unak/fiddley
5
+ #
6
+ # Copyright (c) 2017 NAKAMURA Usaku usa@garbagecollect.jp
7
+ #
8
+ # Released under the 2-Clause BSD License.
9
+
10
+ # NOTE: kojix2 added, removed, and modified several methods.
11
+
12
+ require 'fiddle/import'
13
+
14
+ module GRCommons
15
+ # Ruby-FFI compatible API layer for Fiddle.
16
+ module Fiddley
17
+ # NOTE: GR.rb supports 2.4 +. Unpack 1 does not work under 2.3.
18
+
19
+ module Utils
20
+ # assumes short = 16bit, int = 32bit, long long = 64bit
21
+ SIZET_FORMAT = Fiddle::SIZEOF_VOIDP == Fiddle::SIZEOF_LONG ? 'l!' : 'q'
22
+ SIZET_TYPE = Fiddle::SIZEOF_VOIDP == Fiddle::SIZEOF_LONG ? 'unsigned long' : 'unsigned long long'
23
+
24
+ module_function
25
+
26
+ def type2size(type)
27
+ case type
28
+ when :char, :uchar, :int8, :uint8
29
+ Fiddle::SIZEOF_CHAR
30
+ when :short, :ushort, :int16, :uint16
31
+ Fiddle::SIZEOF_SHORT
32
+ when :int, :uint, :int32, :uint32, :bool
33
+ Fiddle::SIZEOF_INT
34
+ when :long, :ulong
35
+ Fiddle::SIZEOF_LONG
36
+ when :int64, :uint64, :long_long, :ulong_long
37
+ Fiddle::SIZEOF_LONG_LONG
38
+ when :float
39
+ Fiddle::SIZEOF_FLOAT
40
+ when :double
41
+ Fiddle::SIZEOF_DOUBLE
42
+ when :size_t
43
+ Fiddle::SIZEOF_SIZE_T
44
+ when :string, :pointer
45
+ Fiddle::SIZEOF_VOIDP
46
+ else
47
+ raise "unknown type #{type}"
48
+ end
49
+ end
50
+
51
+ # `type2offset_size` is not used in GR.rb, so deleted.
52
+
53
+ # `str2value` is not used in GR.rb, so deleted.
54
+
55
+ # added
56
+ def str2array(type, str)
57
+ case type
58
+ when :char, :int8
59
+ str.unpack('c*')
60
+ when :uchar, :uint8
61
+ str.unpack('C*')
62
+ when :short, :int16
63
+ str.unpack('s*')
64
+ when :ushort, :uint16
65
+ str.unpack('S*')
66
+ when :int32
67
+ str.unpack('l*')
68
+ when :uint32
69
+ str.unpack('L*')
70
+ when :int
71
+ str.unpack('i!*')
72
+ when :uint
73
+ str.unpack('I!*')
74
+ when :bool
75
+ str.unpack('i!*') != 0
76
+ when :long
77
+ str.unpack('l!*')
78
+ when :ulong
79
+ str.unpack('L!*')
80
+ when :long_long, :int64
81
+ str.unpack('q*')
82
+ when :ulong_long, :uint64
83
+ str.unpack('Q*')
84
+ when :size_t
85
+ str.unpack(SIZET_FORMAT)
86
+ when :float
87
+ str.unpack('f*')
88
+ when :double
89
+ str.unpack('d*')
90
+ when :string, :pointer
91
+ str.unpack('p*')
92
+ else
93
+ raise "unknown type #{type}"
94
+ end
95
+ end
96
+
97
+ # `value2str` is not used in GR.rb, so deleted.
98
+
99
+ # added
100
+ def array2str(type, arr)
101
+ case type
102
+ when :char, :int8
103
+ arr.pack('c*')
104
+ when :uchar, :uint8
105
+ arr.pack('C*')
106
+ when :short, :int16
107
+ arr.pack('s*')
108
+ when :ushort, :uint16
109
+ arr.pack('S*')
110
+ when :int32
111
+ arr.pack('l*')
112
+ when :uint32
113
+ arr.pack('L*')
114
+ when :int
115
+ arr.pack('i!*')
116
+ when :uint
117
+ arr.pack('I!*')
118
+ when :bool
119
+ [arr ? 1 : 0].pack('i!*')
120
+ when :long
121
+ arr.pack('l!*')
122
+ when :ulong
123
+ arr.pack('L!*')
124
+ when :long_long, :int64
125
+ arr.pack('q*')
126
+ when :ulong_long, :uint64
127
+ arr.pack('Q*')
128
+ when :size_t
129
+ arr.pack(SIZET_FORMAT)
130
+ when :float
131
+ arr.pack('f*')
132
+ when :double
133
+ arr.pack('d*')
134
+ when :string, :pointer
135
+ arr.pack('p*')
136
+ else
137
+ raise "unknown type #{type}"
138
+ end
139
+ end
140
+
141
+ # `type2str` is not used in GR.rb, so deleted.
142
+
143
+ def type2type(type)
144
+ case type
145
+ when :char, :int8
146
+ Fiddle::TYPE_CHAR
147
+ when :uchar, :uint8
148
+ -Fiddle::TYPE_CHAR
149
+ when :short, :int16
150
+ Fiddle::TYPE_SHORT
151
+ when :ushort, :uint16
152
+ -Fiddle::TYPE_SHORT
153
+ when :int, :int32
154
+ Fiddle::TYPE_INT
155
+ when :uint, :uint32
156
+ -Fiddle::TYPE_INT
157
+ when :bool
158
+ Fiddle::TYPE_INT
159
+ when :long
160
+ Fiddle::TYPE_LONG
161
+ when :ulong
162
+ -Fiddle::TYPE_LONG
163
+ when :long_long, :int64
164
+ Fiddle::TYPE_LONG_LONG
165
+ when :ulong_long, :uint64
166
+ -Fiddle::TYPE_LONG_LONG
167
+ when :float
168
+ Fiddle::TYPE_FLOAT
169
+ when :double
170
+ Fiddle::TYPE_DOUBLE
171
+ when :size_t
172
+ Fiddle::TYPE_SIZE_T
173
+ when :string, :pointer
174
+ Fiddle::TYPE_VOIDP
175
+ when :void
176
+ Fiddle::TYPE_VOID
177
+ else
178
+ raise "unknown type #{type}"
179
+ end
180
+ end
181
+ end
182
+
183
+ class MemoryPointer
184
+ using Fiddley::RefineStringUnpack1 if defined?(Fiddley::RefineStringUnpack1)
185
+
186
+ include Fiddley::Utils
187
+
188
+ def initialize(type, num = 1, _clear = true)
189
+ if num.is_a?(Fiddle::Pointer)
190
+ @ptr = num
191
+ @size = @ptr.size
192
+ else
193
+ @size = type2size(type) * num
194
+ @ptr = Fiddle::Pointer.malloc(@size)
195
+ end
196
+ end
197
+
198
+ attr_reader :size
199
+
200
+ def to_ptr
201
+ @ptr
202
+ end
203
+
204
+ { 8 => 'c', 16 => 's', 32 => 'l', 64 => 'q' }.each do |bits, form|
205
+ bytes = bits / 8
206
+
207
+ define_method("put_int#{bits}") do |offset, val|
208
+ @ptr[offset, bytes] = [val].pack(form)
209
+ end
210
+
211
+ define_method("write_int#{bits}") do |val|
212
+ __send__("put_int#{bits}", 0, val)
213
+ end
214
+
215
+ define_method("put_array_of_int#{bits}") do |offset, ary|
216
+ put_bytes(offset, ary.pack(form + '*'))
217
+ end
218
+
219
+ define_method("write_array_of_int#{bits}") do |ary|
220
+ __send__("put_array_of_int#{bits}", 0, ary)
221
+ end
222
+
223
+ define_method("get_int#{bits}") do |offset|
224
+ @ptr[offset, bytes].unpack1(form)
225
+ end
226
+
227
+ define_method("read_int#{bits}") do
228
+ __send__("get_int#{bits}", 0)
229
+ end
230
+
231
+ define_method("get_array_of_int#{bits}") do |offset, num|
232
+ @ptr[offset, bytes * num].unpack(form + '*')
233
+ end
234
+
235
+ define_method("read_array_of_int#{bits}") do |num|
236
+ __send__("get_array_of_int#{bits}", 0, num)
237
+ end
238
+
239
+ form2 = form.upcase
240
+
241
+ define_method("put_uint#{bits}") do |offset, val|
242
+ @ptr[offset, bytes] = [val].pack(form2)
243
+ end
244
+
245
+ define_method("write_uint#{bits}") do |val|
246
+ __send__("put_uint#{bits}", 0, val)
247
+ end
248
+
249
+ define_method("put_array_of_uint#{bits}") do |offset, ary|
250
+ put_bytes(offset, ary.pack(form2 + '*'))
251
+ end
252
+
253
+ define_method("write_array_of_uint#{bits}") do |ary|
254
+ __send__("put_array_of_uint#{bits}", 0, ary)
255
+ end
256
+
257
+ define_method("get_uint#{bits}") do |offset|
258
+ @ptr[offset, bytes].unpack1(form2)
259
+ end
260
+
261
+ define_method("read_uint#{bits}") do
262
+ __send__("get_uint#{bits}", 0)
263
+ end
264
+
265
+ define_method("get_array_of_uint#{bits}") do |offset, num|
266
+ @ptr[offset, bytes * num].unpack(form2 + '*')
267
+ end
268
+
269
+ define_method("read_array_of_uint#{bits}") do |num|
270
+ __send__("get_array_of_uint#{bits}", 0, num)
271
+ end
272
+ end
273
+
274
+ # added
275
+ define_method('put_double') do |offset, val|
276
+ @ptr[offset, 8] = [val].pack('d')
277
+ end
278
+
279
+ # added
280
+ define_method('put_float') do |offset, val|
281
+ @ptr[offset, 4] = [val].pack('f')
282
+ end
283
+
284
+ # added
285
+ define_method('write_double') do |val|
286
+ __send__('put_double', 0, val)
287
+ end
288
+
289
+ # added
290
+ define_method('write_float') do |val|
291
+ __send__('put_float', 0, val)
292
+ end
293
+
294
+ # added
295
+ define_method('put_array_of_double') do |offset, ary|
296
+ put_bytes(offset, ary.pack('d*'))
297
+ end
298
+
299
+ # added
300
+ define_method('write_array_of_double') do |ary|
301
+ __send__('put_array_of_double', 0, ary)
302
+ end
303
+
304
+ # added
305
+ define_method('put_array_of_float') do |offset, ary|
306
+ put_bytes(offset, ary.pack('f*'))
307
+ end
308
+
309
+ # added
310
+ define_method('write_array_of_float') do |ary|
311
+ __send__('put_array_of_float', 0, ary)
312
+ end
313
+
314
+ # added
315
+ define_method('get_double') do |offset|
316
+ @ptr[offset, 8].unpack1('d')
317
+ end
318
+
319
+ # added
320
+ define_method('read_double') do
321
+ __send__('get_double', 0)
322
+ end
323
+
324
+ # added
325
+ define_method('get_float') do |offset|
326
+ @ptr[offset, 4].unpack1('f')
327
+ end
328
+
329
+ # added
330
+ define_method('read_float') do
331
+ __send__('get_double', 0)
332
+ end
333
+
334
+ # added
335
+ define_method('get_array_of_double') do |offset, num|
336
+ @ptr[offset, 8 * num].unpack('d' + '*')
337
+ end
338
+
339
+ # added
340
+ define_method('get_array_of_float') do |offset, num|
341
+ @ptr[offset, 4 * num].unpack('f' + '*')
342
+ end
343
+
344
+ # added
345
+ define_method('read_array_of_double') do |num|
346
+ __send__('get_array_of_double', 0, num)
347
+ end
348
+
349
+ # added
350
+ define_method('read_array_of_float') do |num|
351
+ __send__('get_array_of_float', 0, num)
352
+ end
353
+
354
+ def put_bytes(offset, str, idx = 0, len = str.bytesize - idx)
355
+ @ptr[offset, len] = str[idx, len]
356
+ end
357
+
358
+ def write_bytes(str, idx = 0, len = nil)
359
+ put_bytes(0, str, idx, len)
360
+ end
361
+
362
+ def get_bytes(offset, len)
363
+ @ptr[offset, len]
364
+ end
365
+
366
+ def read_bytes(len)
367
+ get_bytes(0, len)
368
+ end
369
+
370
+ def put_string(offset, str)
371
+ @ptr[offset, str.bytesize] = str
372
+ end
373
+
374
+ def write_string(str, len = nil)
375
+ put_string(0, len ? str[0, len] : str)
376
+ end
377
+
378
+ def write_string_length(str, len)
379
+ put_string(0, str[0, len])
380
+ end
381
+
382
+ def get_string(offset, len = nil)
383
+ @ptr[offset, len || @size - offset]
384
+ end
385
+
386
+ def read_string(len = nil)
387
+ get_string(0, len)
388
+ end
389
+
390
+ # added
391
+ # NOTE: Return Fiddle::Pointer. Not MemoryPointer.
392
+ def read_pointer
393
+ to_ptr.ptr
394
+ end
395
+
396
+ alias put_int put_int32
397
+ alias write_int write_int32
398
+ alias get_int get_int32
399
+ alias read_int read_int32
400
+ alias write_array_of_int write_array_of_int32 # added
401
+ alias read_array_of_int read_array_of_int32 # added
402
+
403
+ alias put_uint put_uint32
404
+ alias write_uint write_uint32
405
+ alias get_uint get_uint32
406
+ alias read_uint read_uint32
407
+ alias write_array_of_uint write_array_of_uint32 # added
408
+ alias read_array_of_uint read_array_of_uint32 # added
409
+ end
410
+
411
+ class Function < Fiddle::Closure::BlockCaller
412
+ include Fiddley::Utils
413
+
414
+ def initialize(ret, params, &blk)
415
+ super(type2type(ret), params.map { |e| type2type(e) }, &blk)
416
+ end
417
+ end
418
+ end
419
+ end