Class: CShadow::CNativeAttribute

Inherits:
Attribute show all
Defined in:
lib/cgen/attribute.rb

Overview

CNativeAttribute and its subclasses handle all but the two special cases described above. The general form for declarations of such attributes is:

shadow_attr_accessor ruby_var => c_declaration

where ruby_var is the name (symbol or string) which will access the data from Ruby, and c_declaration is the string used to declare the data. For example:

shadow_attr_accessor :x => "double x", :y => "int yyy"

Note that the symbol and C identifier need not be the same.

Native attributes fall into two categories: those that embed data within the struct, and those that point to a separately allocated block. Embedded attributes are limited in that they are of fixed size. Pointer attributes do not have this limitation. But programmers should be wary of treating them as separate objects: the lifespan of the referenced data block is the same as the lifespan of the Ruby object. If the Ruby object and its shadow are garbage collected while the data is in use, the data will be freed and no longer valid.

When using a separately allocated data block, it is a good practice is to use “copy” semantics, so that there can be no other references to the data. See CharPointerAttribute, for example. Reading or writing to such an attribute has copy semantics, in the following sense. On assignment, the Ruby string argument is copied into an allocated block; later references to this attribute generate a new Ruby string which is a copy of that array of char.

Some CNativeAttribute classes are included for int, long, double, double *, char *, etc.

Uninitialized numeric members are 0. Accessors for uninitialized strings return nil.

Instance Attribute Summary collapse

Attributes inherited from Attribute

#cdecl, #check, #cvar, #dump, #free, #init, #load, #mark, #owner_class, #persists, #reader, #var, #writer

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Attribute

inherited, #inspect

Constructor Details

#initialize(*args) ⇒ CNativeAttribute

Returns a new instance of CNativeAttribute.



290
291
292
293
294
295
# File 'lib/cgen/attribute.rb', line 290

def initialize(*args)
  super
  @cdecl = @match[0]
  @ctype = @match[1]
  @cvar = @match[2]
end

Instance Attribute Details

#ctypeObject (readonly)

Returns the value of attribute ctype.



278
279
280
# File 'lib/cgen/attribute.rb', line 278

def ctype
  @ctype
end

Class Method Details

.match(decl) ⇒ Object



282
283
284
285
286
287
288
# File 'lib/cgen/attribute.rb', line 282

def CNativeAttribute.match decl
  if decl.is_a? String and @pattern and @pattern =~ decl
    Regexp.last_match
  else
    false
  end
end