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.



277
278
279
280
# File 'lib/rbind/generator_ruby.rb', line 277

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.



275
276
277
# File 'lib/rbind/generator_ruby.rb', line 275

def compact_namespace
  @compact_namespace
end

Instance Method Details

#add_accessorsObject



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
394
395
396
397
398
399
400
# File 'lib/rbind/generator_ruby.rb', line 321

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



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

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

#add_enumsObject



402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
# File 'lib/rbind/generator_ruby.rb', line 402

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



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

def file_prefix
    @root.file_prefix
end

#library_nameObject



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

def library_name
    @root.library_name
end

#normalize_bt(name) ⇒ Object



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

def normalize_bt(name)
    GeneratorRuby.normalize_basic_type_name_ffi name
end

#normalize_enum(name) ⇒ Object



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

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

#normalize_m(name) ⇒ Object



303
304
305
# File 'lib/rbind/generator_ruby.rb', line 303

def normalize_m(name)
    GeneratorRuby.normalize_method_name name
end

#normalize_t(name) ⇒ Object



282
283
284
285
286
287
288
289
# File 'lib/rbind/generator_ruby.rb', line 282

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

#required_module_namesObject



311
312
313
314
315
# File 'lib/rbind/generator_ruby.rb', line 311

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