ffi 1.11.1-java → 1.13.0-java
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- checksums.yaml.gz.sig +0 -0
- data.tar.gz.sig +3 -0
- data/.appveyor.yml +30 -0
- data/.github/workflows/ci.yml +64 -0
- data/.gitignore +25 -0
- data/.gitmodules +4 -0
- data/.travis.yml +58 -0
- data/.yardopts +5 -0
- data/CHANGELOG.md +80 -0
- data/Gemfile +17 -0
- data/LICENSE.SPECS +22 -0
- data/README.md +10 -1
- data/Rakefile +25 -43
- data/ffi.gemspec +43 -0
- data/lib/ffi.rb +28 -0
- data/lib/ffi/autopointer.rb +203 -0
- data/lib/ffi/buffer.rb +4 -0
- data/lib/ffi/callback.rb +4 -0
- data/lib/ffi/data_converter.rb +67 -0
- data/lib/ffi/enum.rb +296 -0
- data/lib/ffi/errno.rb +43 -0
- data/lib/ffi/ffi.rb +46 -0
- data/lib/ffi/io.rb +62 -0
- data/lib/ffi/library.rb +592 -0
- data/lib/ffi/managedstruct.rb +84 -0
- data/lib/ffi/memorypointer.rb +1 -0
- data/lib/ffi/platform.rb +175 -0
- data/lib/ffi/platform/aarch64-freebsd/types.conf +128 -0
- data/lib/ffi/platform/aarch64-freebsd12/types.conf +128 -0
- data/lib/ffi/platform/aarch64-linux/types.conf +104 -0
- data/lib/ffi/platform/arm-freebsd/types.conf +152 -0
- data/lib/ffi/platform/arm-freebsd12/types.conf +152 -0
- data/lib/ffi/platform/arm-linux/types.conf +132 -0
- data/lib/ffi/platform/i386-cygwin/types.conf +3 -0
- data/lib/ffi/platform/i386-darwin/types.conf +100 -0
- data/lib/ffi/platform/i386-freebsd/types.conf +152 -0
- data/lib/ffi/platform/i386-freebsd12/types.conf +152 -0
- data/lib/ffi/platform/i386-gnu/types.conf +107 -0
- data/lib/ffi/platform/i386-linux/types.conf +103 -0
- data/lib/ffi/platform/i386-netbsd/types.conf +126 -0
- data/lib/ffi/platform/i386-openbsd/types.conf +128 -0
- data/lib/ffi/platform/i386-solaris/types.conf +122 -0
- data/lib/ffi/platform/i386-windows/types.conf +52 -0
- data/lib/ffi/platform/ia64-linux/types.conf +104 -0
- data/lib/ffi/platform/mips-linux/types.conf +102 -0
- data/lib/ffi/platform/mips64-linux/types.conf +104 -0
- data/lib/ffi/platform/mips64el-linux/types.conf +104 -0
- data/lib/ffi/platform/mipsel-linux/types.conf +102 -0
- data/lib/ffi/platform/mipsisa32r6-linux/types.conf +102 -0
- data/lib/ffi/platform/mipsisa32r6el-linux/types.conf +102 -0
- data/lib/ffi/platform/mipsisa64r6-linux/types.conf +104 -0
- data/lib/ffi/platform/mipsisa64r6el-linux/types.conf +104 -0
- data/lib/ffi/platform/powerpc-aix/types.conf +180 -0
- data/lib/ffi/platform/powerpc-darwin/types.conf +100 -0
- data/lib/ffi/platform/powerpc-linux/types.conf +130 -0
- data/lib/ffi/platform/powerpc-openbsd/types.conf +156 -0
- data/lib/ffi/platform/powerpc64-linux/types.conf +104 -0
- data/lib/ffi/platform/s390-linux/types.conf +102 -0
- data/lib/ffi/platform/s390x-linux/types.conf +102 -0
- data/lib/ffi/platform/sparc-linux/types.conf +102 -0
- data/lib/ffi/platform/sparc-solaris/types.conf +128 -0
- data/lib/ffi/platform/sparc64-linux/types.conf +102 -0
- data/lib/ffi/platform/sparcv9-openbsd/types.conf +156 -0
- data/lib/ffi/platform/sparcv9-solaris/types.conf +128 -0
- data/lib/ffi/platform/x86_64-cygwin/types.conf +3 -0
- data/lib/ffi/platform/x86_64-darwin/types.conf +130 -0
- data/lib/ffi/platform/x86_64-dragonflybsd/types.conf +148 -0
- data/lib/ffi/platform/x86_64-freebsd/types.conf +128 -0
- data/lib/ffi/platform/x86_64-freebsd12/types.conf +158 -0
- data/lib/ffi/platform/x86_64-linux/types.conf +132 -0
- data/lib/ffi/platform/x86_64-netbsd/types.conf +128 -0
- data/lib/ffi/platform/x86_64-openbsd/types.conf +134 -0
- data/lib/ffi/platform/x86_64-solaris/types.conf +122 -0
- data/lib/ffi/platform/x86_64-windows/types.conf +52 -0
- data/lib/ffi/pointer.rb +167 -0
- data/lib/ffi/struct.rb +316 -0
- data/lib/ffi/struct_by_reference.rb +72 -0
- data/lib/ffi/struct_layout.rb +96 -0
- data/lib/ffi/struct_layout_builder.rb +227 -0
- data/lib/ffi/tools/const_generator.rb +230 -0
- data/lib/ffi/tools/generator.rb +105 -0
- data/lib/ffi/tools/generator_task.rb +32 -0
- data/lib/ffi/tools/struct_generator.rb +194 -0
- data/lib/ffi/tools/types_generator.rb +137 -0
- data/lib/ffi/types.rb +194 -0
- data/lib/ffi/union.rb +43 -0
- data/lib/ffi/variadic.rb +78 -0
- data/lib/ffi/version.rb +3 -0
- data/samples/getlogin.rb +8 -0
- data/samples/getpid.rb +8 -0
- data/samples/gettimeofday.rb +18 -0
- data/samples/hello.rb +8 -0
- data/samples/inotify.rb +60 -0
- data/samples/pty.rb +75 -0
- data/samples/qsort.rb +20 -0
- metadata +198 -9
- metadata.gz.sig +0 -0
@@ -0,0 +1,227 @@
|
|
1
|
+
#
|
2
|
+
# Copyright (C) 2008-2010 Wayne Meissner
|
3
|
+
#
|
4
|
+
# This file is part of ruby-ffi.
|
5
|
+
#
|
6
|
+
# All rights reserved.
|
7
|
+
#
|
8
|
+
# Redistribution and use in source and binary forms, with or without
|
9
|
+
# modification, are permitted provided that the following conditions are met:
|
10
|
+
#
|
11
|
+
# * Redistributions of source code must retain the above copyright notice, this
|
12
|
+
# list of conditions and the following disclaimer.
|
13
|
+
# * Redistributions in binary form must reproduce the above copyright notice
|
14
|
+
# this list of conditions and the following disclaimer in the documentation
|
15
|
+
# and/or other materials provided with the distribution.
|
16
|
+
# * Neither the name of the Ruby FFI project nor the names of its contributors
|
17
|
+
# may be used to endorse or promote products derived from this software
|
18
|
+
# without specific prior written permission.
|
19
|
+
#
|
20
|
+
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
21
|
+
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
22
|
+
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
23
|
+
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
|
24
|
+
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
25
|
+
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
26
|
+
# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
27
|
+
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
28
|
+
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
29
|
+
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
30
|
+
#
|
31
|
+
|
32
|
+
module FFI
|
33
|
+
|
34
|
+
# Build a {StructLayout struct layout}.
|
35
|
+
class StructLayoutBuilder
|
36
|
+
attr_reader :size
|
37
|
+
attr_reader :alignment
|
38
|
+
|
39
|
+
def initialize
|
40
|
+
@size = 0
|
41
|
+
@alignment = 1
|
42
|
+
@min_alignment = 1
|
43
|
+
@packed = false
|
44
|
+
@union = false
|
45
|
+
@fields = Array.new
|
46
|
+
end
|
47
|
+
|
48
|
+
# Set size attribute with +size+ only if +size+ is greater than attribute value.
|
49
|
+
# @param [Numeric] size
|
50
|
+
def size=(size)
|
51
|
+
@size = size if size > @size
|
52
|
+
end
|
53
|
+
|
54
|
+
# Set alignment attribute with +align+ only if it is greater than attribute value.
|
55
|
+
# @param [Numeric] align
|
56
|
+
def alignment=(align)
|
57
|
+
@alignment = align if align > @alignment
|
58
|
+
@min_alignment = align
|
59
|
+
end
|
60
|
+
|
61
|
+
# Set union attribute.
|
62
|
+
# Set to +true+ to build a {Union} instead of a {Struct}.
|
63
|
+
# @param [Boolean] is_union
|
64
|
+
# @return [is_union]
|
65
|
+
def union=(is_union)
|
66
|
+
@union = is_union
|
67
|
+
end
|
68
|
+
|
69
|
+
# Building a {Union} or a {Struct} ?
|
70
|
+
#
|
71
|
+
# @return [Boolean]
|
72
|
+
#
|
73
|
+
def union?
|
74
|
+
@union
|
75
|
+
end
|
76
|
+
|
77
|
+
# Set packed attribute
|
78
|
+
# @overload packed=(packed) Set alignment and packed attributes to
|
79
|
+
# +packed+.
|
80
|
+
#
|
81
|
+
# @param [Fixnum] packed
|
82
|
+
#
|
83
|
+
# @return [packed]
|
84
|
+
# @overload packed=(packed) Set packed attribute.
|
85
|
+
# @param packed
|
86
|
+
#
|
87
|
+
# @return [0,1]
|
88
|
+
#
|
89
|
+
def packed=(packed)
|
90
|
+
if packed.is_a?(0.class)
|
91
|
+
@alignment = packed
|
92
|
+
@packed = packed
|
93
|
+
else
|
94
|
+
@packed = packed ? 1 : 0
|
95
|
+
end
|
96
|
+
end
|
97
|
+
|
98
|
+
|
99
|
+
# List of number types
|
100
|
+
NUMBER_TYPES = [
|
101
|
+
Type::INT8,
|
102
|
+
Type::UINT8,
|
103
|
+
Type::INT16,
|
104
|
+
Type::UINT16,
|
105
|
+
Type::INT32,
|
106
|
+
Type::UINT32,
|
107
|
+
Type::LONG,
|
108
|
+
Type::ULONG,
|
109
|
+
Type::INT64,
|
110
|
+
Type::UINT64,
|
111
|
+
Type::FLOAT32,
|
112
|
+
Type::FLOAT64,
|
113
|
+
Type::LONGDOUBLE,
|
114
|
+
Type::BOOL,
|
115
|
+
]
|
116
|
+
|
117
|
+
# @param [String, Symbol] name name of the field
|
118
|
+
# @param [Array, DataConverter, Struct, StructLayout::Field, Symbol, Type] type type of the field
|
119
|
+
# @param [Numeric, nil] offset
|
120
|
+
# @return [self]
|
121
|
+
# Add a field to the builder.
|
122
|
+
# @note Setting +offset+ to +nil+ or +-1+ is equivalent to +0+.
|
123
|
+
def add(name, type, offset = nil)
|
124
|
+
|
125
|
+
if offset.nil? || offset == -1
|
126
|
+
offset = @union ? 0 : align(@size, @packed ? [ @packed, type.alignment ].min : [ @min_alignment, type.alignment ].max)
|
127
|
+
end
|
128
|
+
|
129
|
+
#
|
130
|
+
# If a FFI::Type type was passed in as the field arg, try and convert to a StructLayout::Field instance
|
131
|
+
#
|
132
|
+
field = type.is_a?(StructLayout::Field) ? type : field_for_type(name, offset, type)
|
133
|
+
@fields << field
|
134
|
+
@alignment = [ @alignment, field.alignment ].max unless @packed
|
135
|
+
@size = [ @size, field.size + (@union ? 0 : field.offset) ].max
|
136
|
+
|
137
|
+
return self
|
138
|
+
end
|
139
|
+
|
140
|
+
# @param (see #add)
|
141
|
+
# @return (see #add)
|
142
|
+
# Same as {#add}.
|
143
|
+
# @see #add
|
144
|
+
def add_field(name, type, offset = nil)
|
145
|
+
add(name, type, offset)
|
146
|
+
end
|
147
|
+
|
148
|
+
# @param (see #add)
|
149
|
+
# @return (see #add)
|
150
|
+
# Add a struct as a field to the builder.
|
151
|
+
def add_struct(name, type, offset = nil)
|
152
|
+
add(name, Type::Struct.new(type), offset)
|
153
|
+
end
|
154
|
+
|
155
|
+
# @param name (see #add)
|
156
|
+
# @param type (see #add)
|
157
|
+
# @param [Numeric] count array length
|
158
|
+
# @param offset (see #add)
|
159
|
+
# @return (see #add)
|
160
|
+
# Add an array as a field to the builder.
|
161
|
+
def add_array(name, type, count, offset = nil)
|
162
|
+
add(name, Type::Array.new(type, count), offset)
|
163
|
+
end
|
164
|
+
|
165
|
+
# @return [StructLayout]
|
166
|
+
# Build and return the struct layout.
|
167
|
+
def build
|
168
|
+
# Add tail padding if the struct is not packed
|
169
|
+
size = @packed ? @size : align(@size, @alignment)
|
170
|
+
|
171
|
+
layout = StructLayout.new(@fields, size, @alignment)
|
172
|
+
layout.__union! if @union
|
173
|
+
layout
|
174
|
+
end
|
175
|
+
|
176
|
+
private
|
177
|
+
|
178
|
+
# @param [Numeric] offset
|
179
|
+
# @param [Numeric] align
|
180
|
+
# @return [Numeric]
|
181
|
+
def align(offset, align)
|
182
|
+
align + ((offset - 1) & ~(align - 1));
|
183
|
+
end
|
184
|
+
|
185
|
+
# @param (see #add)
|
186
|
+
# @return [StructLayout::Field]
|
187
|
+
def field_for_type(name, offset, type)
|
188
|
+
field_class = case
|
189
|
+
when type.is_a?(Type::Function)
|
190
|
+
StructLayout::Function
|
191
|
+
|
192
|
+
when type.is_a?(Type::Struct)
|
193
|
+
StructLayout::InnerStruct
|
194
|
+
|
195
|
+
when type.is_a?(Type::Array)
|
196
|
+
StructLayout::Array
|
197
|
+
|
198
|
+
when type.is_a?(FFI::Enum)
|
199
|
+
StructLayout::Enum
|
200
|
+
|
201
|
+
when NUMBER_TYPES.include?(type)
|
202
|
+
StructLayout::Number
|
203
|
+
|
204
|
+
when type == Type::POINTER
|
205
|
+
StructLayout::Pointer
|
206
|
+
|
207
|
+
when type == Type::STRING
|
208
|
+
StructLayout::String
|
209
|
+
|
210
|
+
when type.is_a?(Class) && type < StructLayout::Field
|
211
|
+
type
|
212
|
+
|
213
|
+
when type.is_a?(DataConverter)
|
214
|
+
return StructLayout::Mapped.new(name, offset, Type::Mapped.new(type), field_for_type(name, offset, type.native_type))
|
215
|
+
|
216
|
+
when type.is_a?(Type::Mapped)
|
217
|
+
return StructLayout::Mapped.new(name, offset, type, field_for_type(name, offset, type.native_type))
|
218
|
+
|
219
|
+
else
|
220
|
+
raise TypeError, "invalid struct field type #{type.inspect}"
|
221
|
+
end
|
222
|
+
|
223
|
+
field_class.new(name, offset, type)
|
224
|
+
end
|
225
|
+
end
|
226
|
+
|
227
|
+
end
|
@@ -0,0 +1,230 @@
|
|
1
|
+
require 'tempfile'
|
2
|
+
require 'open3'
|
3
|
+
|
4
|
+
module FFI
|
5
|
+
|
6
|
+
# ConstGenerator turns C constants into ruby values.
|
7
|
+
#
|
8
|
+
# @example a simple example for stdio
|
9
|
+
# require 'ffi/tools/const_generator'
|
10
|
+
# cg = FFI::ConstGenerator.new('stdio') do |gen|
|
11
|
+
# gen.const(:SEEK_SET)
|
12
|
+
# gen.const('SEEK_CUR')
|
13
|
+
# gen.const('seek_end') # this constant does not exist
|
14
|
+
# end # #calculate called automatically at the end of the block
|
15
|
+
#
|
16
|
+
# cg['SEEK_SET'] # => 0
|
17
|
+
# cg['SEEK_CUR'] # => 1
|
18
|
+
# cg['seek_end'] # => nil
|
19
|
+
# cg.to_ruby # => "SEEK_SET = 0\nSEEK_CUR = 1\n# seek_end not available"
|
20
|
+
class ConstGenerator
|
21
|
+
@options = {}
|
22
|
+
attr_reader :constants
|
23
|
+
|
24
|
+
# Creates a new constant generator that uses +prefix+ as a name, and an
|
25
|
+
# options hash.
|
26
|
+
#
|
27
|
+
# The only option is +:required+, which if set to +true+ raises an error if a
|
28
|
+
# constant you have requested was not found.
|
29
|
+
#
|
30
|
+
# @param [#to_s] prefix
|
31
|
+
# @param [Hash] options
|
32
|
+
# @return
|
33
|
+
# @option options [Boolean] :required
|
34
|
+
# @overload initialize(prefix, options)
|
35
|
+
# @overload initialize(prefix, options) { |gen| ... }
|
36
|
+
# @yieldparam [ConstGenerator] gen new generator is passed to the block
|
37
|
+
# When passed a block, {#calculate} is automatically called at the end of
|
38
|
+
# the block, otherwise you must call it yourself.
|
39
|
+
def initialize(prefix = nil, options = {})
|
40
|
+
@includes = ['stdio.h', 'stddef.h']
|
41
|
+
@constants = {}
|
42
|
+
@prefix = prefix
|
43
|
+
|
44
|
+
@required = options[:required]
|
45
|
+
@options = options
|
46
|
+
|
47
|
+
if block_given? then
|
48
|
+
yield self
|
49
|
+
calculate self.class.options.merge(options)
|
50
|
+
end
|
51
|
+
end
|
52
|
+
# Set class options
|
53
|
+
# These options are merged with {#initialize} options when it is called with a block.
|
54
|
+
# @param [Hash] options
|
55
|
+
# @return [Hash] class options
|
56
|
+
def self.options=(options)
|
57
|
+
@options = options
|
58
|
+
end
|
59
|
+
# Get class options.
|
60
|
+
# @return [Hash] class options
|
61
|
+
def self.options
|
62
|
+
@options
|
63
|
+
end
|
64
|
+
# @param [String] name
|
65
|
+
# @return constant value (converted if a +converter+ was defined).
|
66
|
+
# Access a constant by name.
|
67
|
+
def [](name)
|
68
|
+
@constants[name].converted_value
|
69
|
+
end
|
70
|
+
|
71
|
+
# Request the value for C constant +name+.
|
72
|
+
#
|
73
|
+
# @param [#to_s] name C constant name
|
74
|
+
# @param [String] format a printf format string to print the value out
|
75
|
+
# @param [String] cast a C cast for the value
|
76
|
+
# @param ruby_name alternate ruby name for {#to_ruby}
|
77
|
+
#
|
78
|
+
# @overload const(name, format=nil, cast='', ruby_name=nil, converter=nil)
|
79
|
+
# +converter+ is a Method or a Proc.
|
80
|
+
# @param [#call] converter convert the value from a string to the appropriate
|
81
|
+
# type for {#to_ruby}.
|
82
|
+
# @overload const(name, format=nil, cast='', ruby_name=nil) { |value| ... }
|
83
|
+
# Use a converter block. This block convert the value from a string to the
|
84
|
+
# appropriate type for {#to_ruby}.
|
85
|
+
# @yieldparam value constant value
|
86
|
+
def const(name, format = nil, cast = '', ruby_name = nil, converter = nil,
|
87
|
+
&converter_proc)
|
88
|
+
format ||= '%d'
|
89
|
+
cast ||= ''
|
90
|
+
|
91
|
+
if converter_proc and converter then
|
92
|
+
raise ArgumentError, "Supply only converter or converter block"
|
93
|
+
end
|
94
|
+
|
95
|
+
converter = converter_proc if converter.nil?
|
96
|
+
|
97
|
+
const = Constant.new name, format, cast, ruby_name, converter
|
98
|
+
@constants[name.to_s] = const
|
99
|
+
return const
|
100
|
+
end
|
101
|
+
|
102
|
+
# Calculate constants values.
|
103
|
+
# @param [Hash] options
|
104
|
+
# @option options [String] :cppflags flags for C compiler
|
105
|
+
# @return [nil]
|
106
|
+
# @raise if a constant is missing and +:required+ was set to +true+ (see {#initialize})
|
107
|
+
def calculate(options = {})
|
108
|
+
binary = File.join Dir.tmpdir, "rb_const_gen_bin_#{Process.pid}"
|
109
|
+
|
110
|
+
Tempfile.open("#{@prefix}.const_generator") do |f|
|
111
|
+
@includes.each do |inc|
|
112
|
+
f.puts "#include <#{inc}>"
|
113
|
+
end
|
114
|
+
f.puts "\nint main(int argc, char **argv)\n{"
|
115
|
+
|
116
|
+
@constants.each_value do |const|
|
117
|
+
f.puts <<-EOF
|
118
|
+
#ifdef #{const.name}
|
119
|
+
printf("#{const.name} #{const.format}\\n", #{const.cast}#{const.name});
|
120
|
+
#endif
|
121
|
+
EOF
|
122
|
+
end
|
123
|
+
|
124
|
+
f.puts "\n\treturn 0;\n}"
|
125
|
+
f.flush
|
126
|
+
|
127
|
+
output = `gcc #{options[:cppflags]} -D_DARWIN_USE_64_BIT_INODE -D_LARGEFILE_SOURCE -D_FILE_OFFSET_BITS=64 -x c -Wall -Werror #{f.path} -o #{binary} 2>&1`
|
128
|
+
|
129
|
+
unless $?.success? then
|
130
|
+
output = output.split("\n").map { |l| "\t#{l}" }.join "\n"
|
131
|
+
raise "Compilation error generating constants #{@prefix}:\n#{output}"
|
132
|
+
end
|
133
|
+
end
|
134
|
+
|
135
|
+
output = `#{binary}`
|
136
|
+
File.unlink(binary + (FFI::Platform.windows? ? ".exe" : ""))
|
137
|
+
output.each_line do |line|
|
138
|
+
line =~ /^(\S+)\s(.*)$/
|
139
|
+
const = @constants[$1]
|
140
|
+
const.value = $2
|
141
|
+
end
|
142
|
+
|
143
|
+
missing_constants = @constants.select do |name, constant|
|
144
|
+
constant.value.nil?
|
145
|
+
end.map { |name,| name }
|
146
|
+
|
147
|
+
if @required and not missing_constants.empty? then
|
148
|
+
raise "Missing required constants for #{@prefix}: #{missing_constants.join ', '}"
|
149
|
+
end
|
150
|
+
end
|
151
|
+
|
152
|
+
# Dump constants to +io+.
|
153
|
+
# @param [#puts] io
|
154
|
+
# @return [nil]
|
155
|
+
def dump_constants(io)
|
156
|
+
@constants.each do |name, constant|
|
157
|
+
name = [@prefix, name].join '.' if @prefix
|
158
|
+
io.puts "#{name} = #{constant.converted_value}"
|
159
|
+
end
|
160
|
+
end
|
161
|
+
|
162
|
+
# Outputs values for discovered constants. If the constant's value was
|
163
|
+
# not discovered it is not omitted.
|
164
|
+
# @return [String]
|
165
|
+
def to_ruby
|
166
|
+
@constants.sort_by { |name,| name }.map do |name, constant|
|
167
|
+
if constant.value.nil? then
|
168
|
+
"# #{name} not available"
|
169
|
+
else
|
170
|
+
constant.to_ruby
|
171
|
+
end
|
172
|
+
end.join "\n"
|
173
|
+
end
|
174
|
+
|
175
|
+
# Add additional C include file(s) to calculate constants from.
|
176
|
+
# @note +stdio.h+ and +stddef.h+ automatically included
|
177
|
+
# @param [List<String>, Array<String>] i include file(s)
|
178
|
+
# @return [Array<String>] array of include files
|
179
|
+
def include(*i)
|
180
|
+
@includes |= i.flatten
|
181
|
+
end
|
182
|
+
|
183
|
+
end
|
184
|
+
|
185
|
+
# This class hold constants for {ConstGenerator}
|
186
|
+
class ConstGenerator::Constant
|
187
|
+
|
188
|
+
attr_reader :name, :format, :cast
|
189
|
+
attr_accessor :value
|
190
|
+
|
191
|
+
# @param [#to_s] name
|
192
|
+
# @param [String] format a printf format string to print the value out
|
193
|
+
# @param [String] cast a C cast for the value
|
194
|
+
# @param ruby_name alternate ruby name for {#to_ruby}
|
195
|
+
# @param [#call] converter convert the value from a string to the appropriate
|
196
|
+
# type for {#to_ruby}.
|
197
|
+
def initialize(name, format, cast, ruby_name = nil, converter=nil)
|
198
|
+
@name = name
|
199
|
+
@format = format
|
200
|
+
@cast = cast
|
201
|
+
@ruby_name = ruby_name
|
202
|
+
@converter = converter
|
203
|
+
@value = nil
|
204
|
+
end
|
205
|
+
|
206
|
+
# Return constant value (converted if a +converter+ was defined).
|
207
|
+
# @return constant value.
|
208
|
+
def converted_value
|
209
|
+
if @converter
|
210
|
+
@converter.call(@value)
|
211
|
+
else
|
212
|
+
@value
|
213
|
+
end
|
214
|
+
end
|
215
|
+
|
216
|
+
# get constant ruby name
|
217
|
+
# @return [String]
|
218
|
+
def ruby_name
|
219
|
+
@ruby_name || @name
|
220
|
+
end
|
221
|
+
|
222
|
+
# Get an evaluable string from constant.
|
223
|
+
# @return [String]
|
224
|
+
def to_ruby
|
225
|
+
"#{ruby_name} = #{converted_value}"
|
226
|
+
end
|
227
|
+
|
228
|
+
end
|
229
|
+
|
230
|
+
end
|