Class: Rbind::GeneratorRuby::RBindHelper

Inherits:
HelperBase
  • Object
show all
Defined in:
lib/rbind/generator_ruby.rb

Instance Attribute Summary collapse

Attributes inherited from HelperBase

#name

Attributes included from Logger

#log

Instance Method Summary collapse

Methods inherited from HelperBase

#binding, #full_name

Methods included from Logger

extend_object

Constructor Details

#initialize(name, root, compact_namespace = true) ⇒ RBindHelper

Returns a new instance of RBindHelper.



270
271
272
273
# File 'lib/rbind/generator_ruby.rb', line 270

def initialize(name, root,compact_namespace=true)
    @compact_namespace = compact_namespace
    super(name,root)
end

Instance Attribute Details

#compact_namespaceObject (readonly)

Returns the value of attribute compact_namespace.



268
269
270
# File 'lib/rbind/generator_ruby.rb', line 268

def compact_namespace
  @compact_namespace
end

Instance Method Details

#add_accessorsObject



314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
# File 'lib/rbind/generator_ruby.rb', line 314

def add_accessors
    str = ""
    @root.root.each_type do |t|
        next if t.basic_type? && !t.is_a?(RNamespace)
        str += "\n#methods for #{t.full_name}\n"
        if t.cdelete_method
            str += "attach_function :#{normalize_m t.cdelete_method},"\
            ":#{t.cdelete_method},[#{normalize_t(t.full_name)}],:void\n"
            str += "attach_function :#{normalize_m t.cdelete_method}_struct,"\
            ":#{t.cdelete_method},[#{normalize_t(t.full_name)}Struct],:void\n"
        end
        t.each_operation do |op|
            return_type = if op.constructor?
                              "#{normalize_t op.owner.full_name}"
                          else
                              op_return_type = op.return_type
                              if op_return_type.respond_to?(:get_base_delegate)
                                  if klass = op_return_type.get_base_delegate
                                      op_return_type = klass if klass.kind_of?(REnum)
                                  end
                              end

                              if op_return_type.basic_type?
                                  if op_return_type.ptr?
                                      ":pointer"
                                  elsif op_return_type.kind_of?(REnum)
                                      ":#{normalize_enum op_return_type.to_raw.csignature}"
                                  else
                                      ":#{normalize_bt op_return_type.to_raw.csignature}"
                                  end
                              else
                                  if op_return_type.extern_package_name
                                      normalize_t("::#{op_return_type.extern_package_name}::#{op_return_type.to_raw.full_name}")
                                  elsif op_return_type.kind_of?(REnum)
                                      ":#{normalize_enum op_return_type.to_raw.full_name}"
                                  else
                                      normalize_t op_return_type.to_raw.full_name
                                  end
                              end
                          end
            args = op.cparameters.map do |p|
                p_type = p.type
                if p_type.respond_to?(:get_base_delegate)
                    if klass = p_type.get_base_delegate
                        p_type = klass if klass.kind_of?(REnum)
                    end
                end
                if p_type.basic_type?
                    if p_type.ptr? || p.type.ref?
                        ":pointer"
                    elsif p.type.kind_of?(REnum)
                        # Includes enums, which need to be defined accordingly
                        # using ffi:
                        # enum :normalized_name, [:first, 1,
                        #                         :second,
                        #                         :third]
                        #
                        ":#{normalize_enum p.type.to_raw.csignature}"
                    else
                        ":#{normalize_bt p.type.to_raw.csignature}"
                    end
                else
                    if p_type.extern_package_name
                        normalize_t("::#{p_type.extern_package_name}::#{p_type.to_raw.full_name}")
                    elsif p_type.kind_of?(REnum)
                        ":#{normalize_enum p_type.to_raw.full_name}"
                    else
                        normalize_t p_type.to_raw.full_name
                    end
                end
            end
            fct_name = normalize_m op.cname
            str += "attach_function :#{fct_name},:#{op.cname},[#{args.join(",")}],#{return_type}\n"
            str
        end
        str+"\n"
    end
    str+"\n"
    str.gsub(/\n/,"\n        ")
end

#add_docObject



284
285
286
# File 'lib/rbind/generator_ruby.rb', line 284

def add_doc
    GeneratorRuby.normalize_doc(@root.root.doc) if @root.root.doc?
end

#add_enumsObject



395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
# File 'lib/rbind/generator_ruby.rb', line 395

def add_enums
    str = "\n"
    @root.root.each_type do |t|
        if t.kind_of?(REnum)
            str += "\tenum :#{GeneratorRuby::normalize_enum_name(t.to_raw.csignature)}, ["
            t.values.each do |name,value|
                if value
                    str += ":#{name},#{value}, "
                else
                    str += ":#{name}, "
                end
            end
            str += "]\n\n"
        end
    end
    str
end

#file_prefixObject



310
311
312
# File 'lib/rbind/generator_ruby.rb', line 310

def file_prefix
    @root.file_prefix
end

#library_nameObject



300
301
302
# File 'lib/rbind/generator_ruby.rb', line 300

def library_name
    @root.library_name
end

#normalize_bt(name) ⇒ Object



288
289
290
# File 'lib/rbind/generator_ruby.rb', line 288

def normalize_bt(name)
    GeneratorRuby.normalize_basic_type_name_ffi name
end

#normalize_enum(name) ⇒ Object



292
293
294
# File 'lib/rbind/generator_ruby.rb', line 292

def normalize_enum(name)
    GeneratorRuby.normalize_enum_name(name)
end

#normalize_m(name) ⇒ Object



296
297
298
# File 'lib/rbind/generator_ruby.rb', line 296

def normalize_m(name)
    GeneratorRuby.normalize_method_name name
end

#normalize_t(name) ⇒ Object



275
276
277
278
279
280
281
282
# File 'lib/rbind/generator_ruby.rb', line 275

def normalize_t(name)
    t = GeneratorRuby.normalize_type_name name
    if compact_namespace
        t.gsub(/^#{self.name}::/,"")
    else
        t
    end
end

#required_module_namesObject



304
305
306
307
308
# File 'lib/rbind/generator_ruby.rb', line 304

def required_module_names
    @root.required_module_names.map do |name|
        "require '#{name.downcase}'\n"
    end.join
end