wrapture 0.1.0 → 0.2.0

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 67db9aad9a4383dca21f730d07b2af31642334204a66377167154a4849975005
4
- data.tar.gz: de35dd304d427fe35f768567e58684a9217714ec01447c73a5961db1760b6ed1
3
+ metadata.gz: d8d17aaf86d72f9bb0c19ce28c34f1f869f4a1bff88e9312f712f607952f6fb8
4
+ data.tar.gz: cce93f31ba213f89d569470af45b35ece17976ad555946e9769e0f27dc7a7a19
5
5
  SHA512:
6
- metadata.gz: 7afe9e387587d8efa3524566e9d9d4d2d9a87bc82fdcdb4995757c6d86a04eca17c452fee716c63681b84be536398beee5a4b429ec9f8fb34723e6650d72d219
7
- data.tar.gz: 62d4696980ec04a870f32cdfc987340f840ba269601996b824db8f4e11f564fe5836943ee1e66cebb1c4e9776b052f7adb5b2eef0f7f92514b9622f1e900200b
6
+ metadata.gz: 249236b5ddda18f3e9d2c6e0254631484f93c665cb7c831df98bee58e3b39a7240eefba10b982ac48a972c9584b3a4e3fc84c338c3f1e166d8f19b905bfb8c04
7
+ data.tar.gz: 0ed25c495034eef0ea0cbbe3acaf88bb28b99a2a80453f9ab6fd2d9a698a4c208e01a09f6a37de144f5b05dfc3acfcadea153c441c2a62f9bd5d28e8ac38622f
@@ -1,12 +1,13 @@
1
1
  #!/usr/bin/env ruby
2
2
 
3
+ # frozen_string_literal: true
4
+
3
5
  require 'yaml'
4
6
  require 'wrapture'
5
7
 
6
8
  spec = YAML.load_file ARGV[0]
7
9
 
8
- normalized_spec = Wrapture.normalize_spec spec
9
-
10
- normalized_spec['classes'].each do |class_spec|
11
- Wrapture.wrap_class class_spec
10
+ spec['classes'].each do |spec_hash|
11
+ class_spec = Wrapture::ClassSpec.new spec_hash
12
+ class_spec.generate_wrappers
12
13
  end
@@ -1,8 +1,10 @@
1
- module Wrapture
1
+ # frozen_string_literal: true
2
2
 
3
+ ##
4
+ # Classes and functions for generating language wrappers
5
+ module Wrapture
6
+ require 'wrapture/constant_spec'
3
7
  require 'wrapture/class_spec'
4
- require 'wrapture/wrapper'
5
- require 'wrapture/validator'
8
+ require 'wrapture/function_spec'
6
9
  require 'wrapture/version'
7
-
8
10
  end
@@ -1,60 +1,70 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'wrapture/constant_spec'
4
+ require 'wrapture/function_spec'
5
+
1
6
  module Wrapture
7
+ ##
8
+ # A description of a class, including its constants, functions, and other
9
+ # details.
2
10
  class ClassSpec
11
+ def self.typed_variable(type, name)
12
+ if type.end_with? '*'
13
+ "#{type}#{name}"
14
+ else
15
+ "#{type} #{name}"
16
+ end
17
+ end
18
+
3
19
  def initialize(spec)
4
20
  @spec = ClassSpec.normalize_spec_hash(spec)
21
+
22
+ @functions = []
23
+ @spec['functions'].each do |function_spec|
24
+ @functions << FunctionSpec.new(function_spec, self)
25
+ end
26
+
27
+ @constants = []
28
+ @spec['constants'].each do |constant_spec|
29
+ @constants << ConstantSpec.new(constant_spec)
30
+ end
5
31
  end
6
32
 
7
33
  def generate_wrappers
8
- generate_declaration_file
9
- generate_definition_file
34
+ files = []
35
+ files << generate_declaration_file
36
+ files << generate_definition_file
10
37
  end
11
38
 
12
- def self.normalize_spec_hash(spec)
13
- normalized_spec = spec.dup
14
-
15
- if normalized_spec['constructors'].nil?
16
- normalized_spec['constructors'] = []
39
+ def resolve_param(param)
40
+ case param
41
+ when 'equivalent-struct'
42
+ equivalent_struct
43
+ when 'equivalent-struct-pointer'
44
+ equivalent_struct_pointer
45
+ else
46
+ param
17
47
  end
48
+ end
18
49
 
19
- if normalized_spec['equivalent-struct']['members'].nil?
20
- normalized_spec['equivalent-struct']['members'] = []
21
- end
50
+ def function_call(spec)
51
+ resolved_params = []
22
52
 
23
- if normalized_spec['functions'].nil?
24
- normalized_spec['functions'] = []
25
- else
26
- normalized_spec['functions'].each do |function_spec|
27
- if function_spec['params'].nil?
28
- function_spec['params'] = []
29
- end
30
-
31
- if function_spec['wrapped-function']['params'].nil?
32
- function_spec['wrapped-function']['params'] = []
33
- end
34
-
35
- if function_spec['return'].nil?
36
- function_spec['return'] = {}
37
- function_spec['return']['type'] = 'void'
38
- end
39
-
40
- if function_spec['return']['includes'].nil?
41
- function_spec['return']['includes'] = []
42
- end
43
- end
53
+ spec['params'].each do |param|
54
+ resolved_params << resolve_param(param['name'])
44
55
  end
45
56
 
46
- if normalized_spec['constants'].nil?
47
- normalized_spec['constants'] = []
57
+ "#{spec['name']}( #{resolved_params.join ', '} )"
58
+ end
48
59
 
49
- else
50
- normalized_spec['constants'].each do |constant_spec|
51
- if constant_spec['includes'].nil?
52
- constant_spec['includes'] = []
53
- end
54
- end
55
- end
60
+ def self.normalize_spec_hash(spec)
61
+ normalized_spec = spec.dup
62
+ normalized_spec.default = []
56
63
 
57
- return normalized_spec
64
+ normalized_spec['equivalent-struct']['members'] ||= []
65
+ normalized_spec['equivalent-struct']['includes'] ||= []
66
+
67
+ normalized_spec
58
68
  end
59
69
 
60
70
  private
@@ -64,12 +74,28 @@ module Wrapture
64
74
  end
65
75
 
66
76
  def declaration_includes
67
- includes = []
77
+ includes = @spec['equivalent-struct']['includes'].dup
68
78
 
69
- includes.concat @spec['equivalent-struct']['includes']
79
+ @functions.each do |func|
80
+ includes.concat func.declaration_includes
81
+ end
70
82
 
71
- @spec['functions'].each do |function_spec|
72
- includes.concat function_spec['return']['includes']
83
+ @constants.each do |const|
84
+ includes.concat const.declaration_includes
85
+ end
86
+
87
+ includes.uniq
88
+ end
89
+
90
+ def definition_includes
91
+ includes = ["#{@spec['name']}.hpp"]
92
+
93
+ @functions.each do |func|
94
+ includes.concat func.definition_includes
95
+ end
96
+
97
+ @constants.each do |const|
98
+ includes.concat const.definition_includes
73
99
  end
74
100
 
75
101
  includes.uniq
@@ -77,12 +103,20 @@ module Wrapture
77
103
 
78
104
  def pointer_wrapper?
79
105
  @spec['constructors'].each do |constructor_spec|
80
- if constructor_spec['wrapped-function']['return']['type'] == 'equivalent-struct-pointer'
81
- return true
82
- end
106
+ return_type = constructor_spec['wrapped-function']['return']['type']
107
+
108
+ return true if return_type == 'equivalent-struct-pointer'
83
109
  end
84
110
 
85
- return false
111
+ false
112
+ end
113
+
114
+ def equivalent_member(member)
115
+ if pointer_wrapper?
116
+ "this->equivalent->#{member}"
117
+ else
118
+ "this->equivalent.#{member}"
119
+ end
86
120
  end
87
121
 
88
122
  def equivalent_name
@@ -93,36 +127,212 @@ module Wrapture
93
127
  end
94
128
  end
95
129
 
130
+ def equivalent_struct
131
+ if pointer_wrapper?
132
+ '*(this->equivalent)'
133
+ else
134
+ 'this->equivalent'
135
+ end
136
+ end
137
+
138
+ def equivalent_struct_pointer
139
+ if pointer_wrapper?
140
+ 'this->equivalent'
141
+ else
142
+ '&this->equivalent'
143
+ end
144
+ end
145
+
146
+ def wrapped_constructor_signature(index)
147
+ function_spec = @spec['constructors'][index]['wrapped-function']
148
+
149
+ "#{@spec['name']}( #{FunctionSpec.param_list function_spec} )"
150
+ end
151
+
152
+ def destructor_signature
153
+ "~#{@spec['name']}( void )"
154
+ end
155
+
156
+ def wrapped_constructor_definition(index)
157
+ constructor_spec = @spec['constructors'][index]
158
+ wrapped_function = constructor_spec['wrapped-function']
159
+
160
+ yield "#{@spec['name']}::#{wrapped_constructor_signature(index)}{"
161
+
162
+ result = resolve_param wrapped_function['return']['type']
163
+
164
+ yield " #{result} = #{function_call wrapped_function};"
165
+
166
+ yield '}'
167
+ end
168
+
169
+ def member_constructor_signature
170
+ params = []
171
+
172
+ @spec['equivalent-struct']['members'].each do |member|
173
+ params << ClassSpec.typed_variable(member['type'], member['name'])
174
+ end
175
+
176
+ "#{@spec['name']}( #{params.join ', '} )"
177
+ end
178
+
179
+ def struct_constructor_signature
180
+ struct_name = @spec['equivalent-struct']['name']
181
+ "#{@spec['name']}( struct #{struct_name} equivalent )"
182
+ end
183
+
184
+ def pointer_constructor_signature
185
+ struct_name = @spec['equivalent-struct']['name']
186
+ "#{@spec['name']}( struct #{struct_name} *equivalent )"
187
+ end
188
+
96
189
  def generate_declaration_file
97
- file = File.open("#{@spec['name']}.hpp", 'w')
190
+ filename = "#{@spec['name']}.hpp"
98
191
 
99
- file.puts "#ifndef #{header_guard}"
100
- file.puts "#define #{header_guard}"
192
+ File.open(filename, 'w') do |file|
193
+ declaration_contents do |line|
194
+ file.puts line
195
+ end
196
+ end
101
197
 
102
- file.puts unless declaration_includes.empty?
198
+ filename
199
+ end
200
+
201
+ def declaration_contents
202
+ yield "#ifndef #{header_guard}"
203
+ yield "#define #{header_guard}"
204
+
205
+ yield unless declaration_includes.empty?
103
206
  declaration_includes.each do |include_file|
104
- file.puts "#include <#{include_file}"
207
+ yield "#include <#{include_file}>"
105
208
  end
106
209
 
107
- file.puts "namespace #{@spec['namespace']} {"
108
- file.puts " class #{@spec['name']} {"
109
- file.puts ' public:'
210
+ yield
211
+ yield "namespace #{@spec['namespace']} {"
212
+ yield
213
+ yield " class #{@spec['name']} {"
214
+ yield ' public:'
110
215
 
111
- file.puts unless @spec['constants'].empty?
112
- @spec['constants'].each do |constant_spec|
113
- file.puts " static const #{constant_spec['type']} #{constant_spec['name']}"
216
+ yield unless @constants.empty?
217
+ @constants.each do |const|
218
+ yield " #{const.declaration};"
219
+ end
220
+
221
+ yield
222
+ struct_name = @spec['equivalent-struct']['name']
223
+ yield " struct #{struct_name} #{equivalent_name};"
224
+ yield
225
+
226
+ unless @spec['equivalent-struct']['members'].empty?
227
+ yield " #{member_constructor_signature};"
114
228
  end
115
229
 
116
- file.puts
117
- file.puts " struct #{@spec['equivalent-struct']['name']} #{equivalent_name}"
230
+ unless pointer_wrapper?
231
+ yield " #{struct_constructor_signature};"
232
+ yield " #{pointer_constructor_signature};"
233
+ end
118
234
 
119
- file.puts ' };' # end of class
120
- file.puts '}' # end of namespace
235
+ @spec['constructors'].each_index do |constructor|
236
+ yield " #{wrapped_constructor_signature constructor};"
237
+ end
121
238
 
122
- file.close
239
+ yield " #{destructor_signature};" if @spec.key? 'destructor'
240
+
241
+ @functions.each do |func|
242
+ yield " #{func.declaration};"
243
+ end
244
+
245
+ yield ' };' # end of class
246
+ yield
247
+ yield '}' # end of namespace
248
+ yield
249
+ yield '#endif' # end of header guard
123
250
  end
124
251
 
125
252
  def generate_definition_file
253
+ filename = "#{@spec['name']}.cpp"
254
+
255
+ File.open(filename, 'w') do |file|
256
+ definition_contents do |line|
257
+ file.puts line
258
+ end
259
+ end
260
+
261
+ filename
262
+ end
263
+
264
+ def definition_contents
265
+ definition_includes.each do |include_file|
266
+ yield "#include <#{include_file}>"
267
+ end
268
+
269
+ yield
270
+ yield "namespace #{@spec['namespace']} {"
271
+
272
+ yield unless @constants.empty?
273
+ @constants.each do |const|
274
+ yield " #{const.definition @spec['name']};"
275
+ end
276
+
277
+ unless @spec['equivalent-struct']['members'].empty?
278
+ yield
279
+ yield " #{@spec['name']}::#{member_constructor_signature} {"
280
+
281
+ @spec['equivalent-struct']['members'].each do |member|
282
+ member_decl = equivalent_member member['name']
283
+ yield " #{member_decl} = #{member['name']};"
284
+ end
285
+
286
+ yield ' }'
287
+ end
288
+
289
+ unless pointer_wrapper?
290
+ yield
291
+ yield " #{@spec['name']}::#{struct_constructor_signature} {"
292
+
293
+ @spec['equivalent-struct']['members'].each do |member|
294
+ member_decl = equivalent_member member['name']
295
+ yield " #{member_decl} = equivalent.#{member['name']};"
296
+ end
297
+
298
+ yield ' }'
299
+
300
+ yield
301
+ yield " #{@spec['name']}::#{pointer_constructor_signature} {"
302
+
303
+ @spec['equivalent-struct']['members'].each do |member|
304
+ member_decl = equivalent_member member['name']
305
+ yield " #{member_decl} = equivalent->#{member['name']};"
306
+ end
307
+
308
+ yield ' }'
309
+ end
310
+
311
+ @spec['constructors'].each_index do |constructor|
312
+ yield
313
+ wrapped_constructor_definition(constructor) do |line|
314
+ yield " #{line}"
315
+ end
316
+ end
317
+
318
+ if @spec.key? 'destructor'
319
+ yield
320
+ yield " #{@spec['name']}::#{destructor_signature} {"
321
+ func_spec = @spec['destructor']['wrapped-function']
322
+ yield " #{function_call func_spec};"
323
+ yield ' }'
324
+ end
325
+
326
+ @functions.each do |func|
327
+ yield
328
+
329
+ func.definition(@spec['name']) do |def_line|
330
+ yield " #{def_line}"
331
+ end
332
+ end
333
+
334
+ yield
335
+ yield '}' # end of namespace
126
336
  end
127
337
  end
128
338
  end
@@ -0,0 +1,37 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Wrapture
4
+ ##
5
+ # A description of a constant.
6
+ class ConstantSpec
7
+ def self.normalize_spec_hash(spec)
8
+ normalized_spec = spec.dup
9
+
10
+ normalized_spec['includes'] ||= []
11
+ normalized_spec['includes'].uniq!
12
+
13
+ normalized_spec
14
+ end
15
+
16
+ def initialize(spec)
17
+ @spec = ConstantSpec.normalize_spec_hash spec
18
+ end
19
+
20
+ def declaration_includes
21
+ @spec['includes'].dup
22
+ end
23
+
24
+ def definition_includes
25
+ @spec['includes'].dup
26
+ end
27
+
28
+ def declaration
29
+ "static const #{ClassSpec.typed_variable(@spec['type'], @spec['name'])}"
30
+ end
31
+
32
+ def definition(class_name)
33
+ expanded_name = "#{class_name}::#{@spec['name']}"
34
+ "const #{@spec['type']} #{expanded_name} = #{@spec['value']}"
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,74 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Wrapture
4
+ ##
5
+ # A description of a function to be generated, including details about the
6
+ # underlying implementation.
7
+ class FunctionSpec
8
+ def self.normalize_spec_hash(spec)
9
+ normalized_spec = spec.dup
10
+
11
+ normalized_spec['params'] ||= []
12
+ normalized_spec['wrapped-function']['params'] ||= []
13
+ normalized_spec['wrapped-function']['includes'] ||= []
14
+
15
+ if normalized_spec['return'].nil?
16
+ normalized_spec['return'] = {}
17
+ normalized_spec['return']['type'] = 'void'
18
+ end
19
+
20
+ normalized_spec['return']['includes'] ||= []
21
+
22
+ normalized_spec
23
+ end
24
+
25
+ def self.param_list(spec)
26
+ return 'void' if spec['params'].empty?
27
+
28
+ params = []
29
+
30
+ spec['params'].each do |param|
31
+ params << ClassSpec.typed_variable(param['type'], param['name'])
32
+ end
33
+
34
+ params.join ', '
35
+ end
36
+
37
+ def initialize(spec, owner)
38
+ @owner = owner
39
+ @spec = FunctionSpec.normalize_spec_hash(spec)
40
+ end
41
+
42
+ def declaration_includes
43
+ @spec['return']['includes'].dup
44
+ end
45
+
46
+ def definition_includes
47
+ includes = @spec['return']['includes'].dup
48
+ includes.concat @spec['wrapped-function']['includes']
49
+
50
+ includes.uniq
51
+ end
52
+
53
+ def signature
54
+ "#{@spec['name']}( #{FunctionSpec.param_list @spec} )"
55
+ end
56
+
57
+ def declaration
58
+ modifier_prefix = @spec['static'] ? 'static ' : ''
59
+ "#{modifier_prefix}#{@spec['return']['type']} #{signature}"
60
+ end
61
+
62
+ def definition(class_name)
63
+ return_type = @spec['return']['type']
64
+ yield "#{return_type} #{class_name}::#{signature} {"
65
+
66
+ wrapped_call = String.new
67
+ wrapped_call << "return #{return_type} ( " unless return_type == 'void'
68
+ wrapped_call << @owner.function_call(@spec['wrapped-function'])
69
+ wrapped_call << ' )' unless return_type == 'void'
70
+ yield " #{wrapped_call};"
71
+ yield '}'
72
+ end
73
+ end
74
+ end
@@ -1,5 +1,5 @@
1
- module Wrapture
2
-
3
- VERSION = '0.1.0'
1
+ # frozen_string_literal: true
4
2
 
3
+ module Wrapture
4
+ VERSION = '0.2.0'
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: wrapture
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Joel Anderson
@@ -9,7 +9,21 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
  date: 2019-04-01 00:00:00.000000000 Z
12
- dependencies: []
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: 1.6.4
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: 1.6.4
13
27
  description: Wraps C code in C++.
14
28
  email: joelanderson333@gmail.com
15
29
  executables:
@@ -20,13 +34,16 @@ files:
20
34
  - bin/wrapture
21
35
  - lib/wrapture.rb
22
36
  - lib/wrapture/class_spec.rb
23
- - lib/wrapture/validator.rb
37
+ - lib/wrapture/constant_spec.rb
38
+ - lib/wrapture/function_spec.rb
24
39
  - lib/wrapture/version.rb
25
- - lib/wrapture/wrapper.rb
26
40
  homepage: http://rubygems.org/gems/wrapture
27
41
  licenses:
28
42
  - Apache-2.0
29
- metadata: {}
43
+ metadata:
44
+ bug_tracker_uri: https://github.com/goatshriek/wrapture/issues
45
+ changelog_uri: https://github.com/goatshriek/wrapture/blob/master/ChangeLog.md
46
+ source_code_uri: https://github.com/goatshriek/wrapture/
30
47
  post_install_message:
31
48
  rdoc_options: []
32
49
  require_paths:
@@ -35,7 +52,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
35
52
  requirements:
36
53
  - - ">="
37
54
  - !ruby/object:Gem::Version
38
- version: '0'
55
+ version: '2.3'
39
56
  required_rubygems_version: !ruby/object:Gem::Requirement
40
57
  requirements:
41
58
  - - ">="
@@ -1,16 +0,0 @@
1
- module Wrapture
2
-
3
- def self.normalize_spec(spec)
4
-
5
- normalized_spec = Hash.new
6
- normalized_spec['classes'] = Array.new
7
-
8
- spec['classes'].each do |class_spec|
9
- normalized_spec['classes'] << ClassSpec.normalize_spec_hash(class_spec)
10
- end
11
-
12
- return normalized_spec
13
-
14
- end
15
-
16
- end
@@ -1,266 +0,0 @@
1
- module Wrapture
2
-
3
- def self.wrap_class( class_spec )
4
-
5
- spec = ClassSpec.new class_spec
6
- spec.generate_wrappers
7
-
8
- class_name = class_spec['name']
9
- struct_name = class_spec['equivalent-struct']['name']
10
-
11
- # generating constructor signatures
12
- constructor_signatures = Array.new
13
- equivalent_name = 'equivalent'
14
- equivalent_struct = 'this->equivalent'
15
- equivalent_struct_pointer = '&this->equivalent'
16
- member_operator = '.'
17
-
18
- class_spec['constructors'].each do |constructor_spec|
19
- if constructor_spec['wrapped-function']['return']['type'] == 'equivalent-struct-pointer'
20
- equivalent_name = '*equivalent'
21
- equivalent_struct = '*(this->equivalent)'
22
- equivalent_struct_pointer = 'this->equivalent'
23
- member_operator = '->'
24
- end
25
-
26
- params = Array.new
27
- constructor_spec['wrapped-function']['params'].each do |param|
28
- params << "#{param['type']} #{param['name']}"
29
- end
30
-
31
- constructor_signatures << "#{class_name}( #{params.join ', '} )"
32
- end
33
-
34
- members = Array.new
35
- class_spec['equivalent-struct']['members'].each do |member|
36
- members << "#{member['type']} #{member['name']}"
37
- end
38
-
39
- member_constructor_signature = "#{class_name}( #{members.join ', '} )"
40
- struct_constructor_signature = "#{class_name}( struct #{struct_name} equivalent )"
41
- pointer_constructor_signature = "#{class_name}( const struct #{struct_name} *equivalent )"
42
-
43
- # get the list of includes for declarations
44
- declaration_includes = class_spec['equivalent-struct']['includes'].dup
45
- class_spec['functions'].each do |function_spec|
46
- declaration_includes.concat function_spec['return']['includes']
47
- end
48
- declaration_includes.uniq!
49
-
50
- # generate the header file
51
- File.open("#{class_name}.hpp", 'w') do |file|
52
- # header guard
53
- file.puts "#ifndef __#{class_name.upcase}_HPP"
54
- file.puts "#define __#{class_name.upcase}_HPP"
55
-
56
- file.puts
57
-
58
- declaration_includes.each do |include_file|
59
- file.puts "#include <#{include_file}>"
60
- end
61
-
62
- file.puts unless declaration_includes.empty?
63
-
64
- file.puts "namespace #{class_spec['namespace']} {"
65
- file.puts " class #{class_name} {"
66
- file.puts ' public:'
67
-
68
- class_spec['constants'].each do |constant_spec|
69
- file.puts " static const #{constant_spec['type']} #{constant_spec['name']};"
70
- end
71
- file.puts unless class_spec['constants'].empty?
72
-
73
- file.puts " struct #{struct_name} #{equivalent_name};"
74
- file.puts
75
-
76
- constructor_signatures.each do |signature|
77
- file.puts " #{signature};"
78
- end
79
-
80
- unless members.empty?
81
- file.puts " #{member_constructor_signature};"
82
- end
83
-
84
- file.puts " #{struct_constructor_signature};"
85
- file.puts " #{pointer_constructor_signature};"
86
-
87
- class_spec['equivalent-struct']['members'].each do |member|
88
- file.puts " #{member['type']} Get#{member['name'].capitalize}( void ) const;"
89
- file.puts " void Set#{member['name'].capitalize}( #{member['type']} #{member['name']} );"
90
- end
91
-
92
- class_spec['functions'].each do |function_spec|
93
- static_modifier = if function_spec['static'] then 'static ' else '' end
94
- file.write " #{static_modifier}#{function_spec['return']['type']} #{function_spec['name']}( "
95
-
96
- wrapped_params = Array.new
97
- function_spec['params'].each do |param|
98
- wrapped_params << "#{param['type']} #{param['name']}"
99
- end
100
- file.write(wrapped_params.join ', ')
101
-
102
- file.puts ' );'
103
- end
104
-
105
- # destructor
106
- unless class_spec['destructor'].nil?
107
- file.puts " ~#{class_name}( void );"
108
- end
109
-
110
- file.puts ' };' # end of class
111
- file.puts '}' # end of namespace
112
- file.puts '#endif' # end of header guard
113
- end
114
-
115
- # get the complete list of includes for definitions
116
- definition_includes = Array.new
117
- definition_includes << "#{class_name}.hpp"
118
- class_spec['functions'].each do |function_spec|
119
- definition_includes.concat function_spec['return']['includes']
120
- definition_includes.concat function_spec['wrapped-function']['includes']
121
- end
122
-
123
- class_spec['constants'].each do |constant_spec|
124
- definition_includes.concat constant_spec['includes']
125
- end
126
- definition_includes.uniq!
127
-
128
- # generate the definition file
129
- File.open("#{class_name}.cpp", 'w') do |file|
130
- definition_includes.each do |include_file|
131
- file.puts "#include <#{include_file}>"
132
- end
133
-
134
- file.puts
135
- file.puts "namespace #{class_spec['namespace']} {"
136
-
137
- # constants
138
- class_spec['constants'].each do |constant_spec|
139
- file.puts
140
- file.puts " const #{constant_spec['type']} #{class_name}::#{constant_spec['name']} = #{constant_spec['value']};"
141
- end
142
-
143
- # constructors
144
- class_spec['constructors'].each_index do |i|
145
- wrapped_function = class_spec['constructors'][i]['wrapped-function']
146
- function_params = Array.new
147
- wrapped_function['params'].each do |param|
148
- function_params << param['name']
149
- end
150
-
151
- file.puts
152
- file.puts " #{class_name}::#{constructor_signatures[i]} {"
153
- file.write(' ')
154
- case wrapped_function['return']['type']
155
- when 'equivalent-struct'
156
- file.write(equivalent_struct)
157
- when 'equivalent-struct-pointer'
158
- file.write(equivalent_struct_pointer)
159
- end
160
- file.puts " = #{wrapped_function['name']}( #{function_params.join ', '} );"
161
- file.puts ' }'
162
- end
163
-
164
- unless members.empty?
165
- file.puts
166
- file.puts " #{class_name}::#{member_constructor_signature} {"
167
- class_spec['equivalent-struct']['members'].each do |member|
168
- file.puts " this->equivalent#{member_operator}#{member['name']} = #{member['name']};"
169
- end
170
- file.puts ' }' # end of the member constructor
171
- end
172
-
173
- file.puts
174
- file.puts " #{class_name}::#{struct_constructor_signature} {"
175
- class_spec['equivalent-struct']['members'].each do |member|
176
- file.puts " this->equivalent#{member_operator}#{member['name']} = equivalent.#{member['name']};"
177
- end
178
- file.puts ' }' # end of struct conversion
179
-
180
- file.puts
181
- file.puts " #{class_name}::#{pointer_constructor_signature} {"
182
- class_spec['equivalent-struct']['members'].each do |member|
183
- file.puts " this->equivalent#{member_operator}#{member['name']} = equivalent->#{member['name']};"
184
- end
185
- file.puts ' }' # end of pointer conversion
186
-
187
- class_spec['equivalent-struct']['members'].each do |member|
188
- file.puts # line to separate from previous functions
189
- file.puts " #{member['type']} #{class_name}::Get#{member['name'].capitalize}( void ) const {"
190
- file.puts " return this->equivalent#{member_operator}#{member['name']};"
191
- file.puts ' }'
192
- file.puts
193
- file.puts " void #{class_name}::Set#{member['name'].capitalize}( #{member['type']} #{member['name']} ) {"
194
- file.puts " this->equivalent#{member_operator}#{member['name']} = #{member['name']};"
195
- file.puts ' }'
196
- end
197
-
198
- class_spec['functions'].each do |function_spec|
199
- file.puts
200
- file.write " #{function_spec['return']['type']} #{class_name}::#{function_spec['name']}( "
201
-
202
- wrapped_params = Array.new
203
- function_spec['params'].each do |param|
204
- wrapped_params << "#{param['type']} #{param['name']}"
205
- end
206
- file.write(wrapped_params.join ', ')
207
-
208
- file.puts ' ) {'
209
-
210
- # the function body
211
- if function_spec['return']['type'] == 'void'
212
- file.write " #{function_spec['wrapped-function']['name']}( "
213
- else
214
- file.write " return #{function_spec['return']['type']}( #{function_spec['wrapped-function']['name']}( "
215
- end
216
-
217
- # building the parameters
218
- wrapped_params = Array.new
219
- function_spec['wrapped-function']['params'].each do |param|
220
- case param['name']
221
- when 'equivalent-struct'
222
- wrapped_params << equivalent_struct
223
- when 'equivalent-struct-pointer'
224
- wrapped_params << equivalent_struct_pointer
225
- else
226
- wrapped_params << param['name']
227
- end
228
- end
229
- file.write(wrapped_params.join ', ')
230
-
231
- if function_spec['return']['type'] == 'void'
232
- file.puts ' );'
233
- else
234
- file.puts ' ) );'
235
- end
236
- file.puts ' }'
237
- end
238
-
239
- # destructor
240
- unless class_spec['destructor'].nil?
241
- file.puts
242
- file.puts " #{class_name}::~#{class_name}( void ) {"
243
- file.write(" #{class_spec['destructor']['wrapped-function']['name']}( ")
244
- wrapped_params = Array.new
245
- class_spec['destructor']['wrapped-function']['params'].each do |param|
246
- case param['name']
247
- when 'equivalent-struct'
248
- wrapped_params << equivalent_struct
249
- when 'equivalent-struct-pointer'
250
- wrapped_params << equivalent_struct_pointer
251
- else
252
- wrapped_params << param['name']
253
- end
254
- end
255
- file.write(wrapped_params.join ', ')
256
- file.puts ' );'
257
- file.puts ' }'
258
- end
259
-
260
- file.puts # line after last function
261
- file.puts '}' # end of namespace
262
- end
263
-
264
- end
265
-
266
- end