Class: CShadow::ObjectAttribute
- Defined in:
- lib/cgen/attribute.rb
Overview
There are two kinds of object attributes. Both refer to Ruby objects and can be typed or untyped. This one is less optimized but cat refer to arbitrary ruby objects.
The syntax for adding an object attribute to a class is simple. The following code adds three object attributes, one untyped and two typed:
class A
include CShadow
shadow_attr_accessor :obj => Object, :sym => Symbol, :ary => Array
end
(See CShadow for variations on #shadow_attr_accessor.)
Assignment to obj performs no type checking. Assignment to sym raises a TypeError unless the object assigned is a Symbol or nil. Similarly ary must always be an Array or nil. Type checking always allows nil in addition to the specified type. In each case, the attribute is initialized to nil.
The referenced Ruby object is marked to protect it from the garbage collector.
Instance Attribute Summary
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
-
#initialize(*args) ⇒ ObjectAttribute
constructor
A new instance of ObjectAttribute.
- #inspect ⇒ Object
- #target_class ⇒ Object
Methods inherited from Attribute
Constructor Details
#initialize(*args) ⇒ ObjectAttribute
Returns a new instance of ObjectAttribute.
132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 |
# File 'lib/cgen/attribute.rb', line 132 def initialize(*args) super @class = @match @cvar = @var @cdecl = "VALUE #{@cvar}; // #{@class}" @init = "shadow->#{@cvar} = Qnil" # otherwise, it's Qfalse == 0 @reader = "result = shadow->#{@cvar}" @writer = "shadow->#{@cvar} = arg" @check = @class unless @class == Object @mark = "rb_gc_mark(shadow->#{@cvar})" @dump = "rb_ary_push(result, shadow->#{@cvar})" @load = "shadow->#{@cvar} = rb_ary_shift(from_array)" end |
Class Method Details
.match(decl) ⇒ Object
126 127 128 |
# File 'lib/cgen/attribute.rb', line 126 def ObjectAttribute.match decl decl if decl.is_a? Class end |
Instance Method Details
#inspect ⇒ Object
149 150 151 |
# File 'lib/cgen/attribute.rb', line 149 def inspect %{<#{self.class} #{@cvar} => #{@class.inspect}>} end |
#target_class ⇒ Object
130 |
# File 'lib/cgen/attribute.rb', line 130 def target_class; @class; end |