Class: CGenerator::CFile

Inherits:
CFragment show all
Defined in:
lib/cgen/cgen.rb

Overview

File templates are managed by the Library, and most users do not need to interact with them directly. They are structured into four sections: includes, structure declarations, variable and function declarations, and function definitions. Each source file automatically includes its corresponding header file and the main header file for the library (which includes ruby.h). The main source file for the library includes each additional header file.

The File#preamble accumulator wraps its input in C comments and places it at the head of the source file.

Defined Under Namespace

Classes: CommentAccumulator, FunctionAccumulator, IncludeAccumulator

Instance Attribute Summary collapse

Attributes inherited from Accumulator

#name, #parent

Instance Method Summary collapse

Methods inherited from Template

accumulator

Methods inherited from Accumulator

#accept?, #add, #add_one, #add_one_really, #inspect, #inspect_one, #output, #output_one

Constructor Details

#initialize(name, library, include_file = nil, bare = !!include_file) ⇒ CFile

Returns a new instance of CFile.



1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
# File 'lib/cgen/cgen.rb', line 1387

def initialize name, library, include_file = nil, bare = !!include_file
  super name, library
  @include_file = include_file
  if bare
    add preamble!, include!, declare!, define!
  else
    ## it's a little hacky to decide in this way that this is a .h file
    sym = name.gsub(/\W/, '_')
    add "#ifndef #{sym}\n#define #{sym}",
        preamble!, include!, declare!, define!,
        "#endif"
  end
end

Instance Attribute Details

#include_fileObject (readonly)

Returns the value of attribute include_file.



1385
1386
1387
# File 'lib/cgen/cgen.rb', line 1385

def include_file
  @include_file
end

Instance Method Details

#declare_extern(*args) ⇒ Object



1521
1522
1523
1524
1525
1526
1527
# File 'lib/cgen/cgen.rb', line 1521

def declare_extern(*args)
  if @include_file
    @include_file.declare(*args)
  else
    declare(*args)
  end
end

#declare_extern_struct(struct_name, *rest) ⇒ Object



1515
1516
1517
# File 'lib/cgen/cgen.rb', line 1515

def declare_extern_struct struct_name, *rest
  @include_file.declare_struct struct_name, *rest
end

#declare_struct(struct_name, *rest) ⇒ Object



1509
1510
1511
1512
1513
# File 'lib/cgen/cgen.rb', line 1509

def declare_struct struct_name, *rest
  struct = CGenerator::Structure.new struct_name, self, *rest
  declare struct_name => ["\n", struct, ";\n"]
  struct
end

#define_alloc_func(klass) ⇒ Object



1504
1505
1506
1507
# File 'lib/cgen/cgen.rb', line 1504

def define_alloc_func klass
  c_name = rb_define_alloc_func :class => klass, :cfile => self
  define c_name, Function
end

#define_c_function(c_name, subclass = Function) ⇒ Object

As for the Library, but can be used on any source file within the library. Used to break large projects up into many files.



1465
1466
1467
# File 'lib/cgen/cgen.rb', line 1465

def define_c_function c_name, subclass = Function
  define c_name, subclass
end

#define_c_global_function(name, subclass = GlobalFunction) ⇒ Object

As for the Library, but can be used on any source file within the library. Used to break large projects up into many files.



1489
1490
1491
1492
1493
# File 'lib/cgen/cgen.rb', line 1489

def define_c_global_function name, subclass = GlobalFunction
  raise unless subclass <= GlobalFunction
  c_name = rb_define_global_function :rb_name => name, :cfile => self
  define c_name, subclass
end

#define_c_method(mod, name, subclass = Method) ⇒ Object

As for the Library, but can be used on any source file within the library. Used to break large projects up into many files.



1471
1472
1473
1474
1475
1476
1477
# File 'lib/cgen/cgen.rb', line 1471

def define_c_method mod, name, subclass = Method
  unless subclass <= Method ## should use assert
    raise "#{subclass.name} is not <= Method"
  end
  c_name = rb_define_method :mod => mod, :rb_name => name, :cfile => self
  define c_name, subclass
end

#define_c_module_function(mod, name, subclass = ModuleFunction) ⇒ Object

As for the Library, but can be used on any source file within the library. Used to break large projects up into many files.



1481
1482
1483
1484
1485
# File 'lib/cgen/cgen.rb', line 1481

def define_c_module_function mod, name, subclass = ModuleFunction
  raise unless subclass <= ModuleFunction
  c_name = rb_define_module_function :mod => mod, :rb_name => name, :cfile => self
  define c_name, subclass
end

#define_c_singleton_method(mod, name, subclass = SingletonMethod) ⇒ Object Also known as: define_c_class_method

As for the Library, but can be used on any source file within the library. Used to break large projects up into many files.



1497
1498
1499
1500
1501
# File 'lib/cgen/cgen.rb', line 1497

def define_c_singleton_method mod, name, subclass = SingletonMethod
  raise unless subclass <= SingletonMethod
  c_name = rb_define_singleton_method :mod => mod, :rb_name => name, :cfile => self
  define c_name, subclass
end

#fileObject



1533
1534
1535
# File 'lib/cgen/cgen.rb', line 1533

def file
  self
end

#separatorObject



1401
1402
1403
# File 'lib/cgen/cgen.rb', line 1401

def separator
  "\n\n"
end

#to_sObject



1529
1530
1531
# File 'lib/cgen/cgen.rb', line 1529

def to_s
  super + "\n"
end