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.



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

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.



290
291
292
# File 'lib/rbind/generator_ruby.rb', line 290

def compact_namespace
  @compact_namespace
end

Instance Method Details

#add_accessorsObject



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
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
# File 'lib/rbind/generator_ruby.rb', line 336

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.to_raw.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.to_raw.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.to_raw.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.to_raw.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 += "@blocking = true; "if op.blocking?
            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



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

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

#add_enumsObject



417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
# File 'lib/rbind/generator_ruby.rb', line 417

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
                    value = GeneratorRuby.normalize_const_value(value)
                    str += ":#{name},#{GeneratorRuby.normalize_type_name(value)}, "
                else
                    str += ":#{name}, "
                end
            end
            str += "]\n"
        end
    end
    str
end

#file_prefixObject



332
333
334
# File 'lib/rbind/generator_ruby.rb', line 332

def file_prefix
    @root.file_prefix
end

#library_nameObject



322
323
324
# File 'lib/rbind/generator_ruby.rb', line 322

def library_name
    @root.library_name
end

#normalize_bt(name) ⇒ Object



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

def normalize_bt(name)
    GeneratorRuby.normalize_basic_type_name_ffi name
end

#normalize_enum(name) ⇒ Object



314
315
316
# File 'lib/rbind/generator_ruby.rb', line 314

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

#normalize_m(name) ⇒ Object



318
319
320
# File 'lib/rbind/generator_ruby.rb', line 318

def normalize_m(name)
    GeneratorRuby.normalize_method_name name
end

#normalize_t(name) ⇒ Object



297
298
299
300
301
302
303
304
# File 'lib/rbind/generator_ruby.rb', line 297

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

#required_module_namesObject



326
327
328
329
330
# File 'lib/rbind/generator_ruby.rb', line 326

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