Class: AutoC::Reference
- Extended by:
- Forwardable
- Defined in:
- lib/autoc/type.rb
Overview
Reference represents a managed counted reference for any type. It can be used with any type, including AutoC collections themselves.
Generated C interface
Type management
Type * typeNew(…) |
Create and return a reference to Type with reference count set to one. The storage for the returned instance is malloc()'ed. The instance is constructed with the type’s constructor typeCtor(…). NOTE: The generated method borrows the second and subsequent arguments from the respective constructor. |
Type * typeRef(Type * self) |
Increment the self's reference count and return self. |
void typeFree(Type * self) |
Decrement the self's reference count. If the reference count reaches zero, free the storage and destroy the instance with the type’s destructor typeDtor(). |
Instance Attribute Summary collapse
-
#target ⇒ Object
readonly
Returns the value of attribute target.
Attributes inherited from Type
Instance Method Summary collapse
- #==(other) ⇒ Object (also: #eql?)
-
#copyable? ⇒ Boolean
Return true since reference copying involves no call to the underlying type’s copy constructor.
- #entities ⇒ Object
-
#initialize(target) ⇒ Reference
constructor
A new instance of Reference.
- #write_impls(stream, define) ⇒ Object
- #write_intf_decls(stream, declare, define) ⇒ Object
Methods inherited from Type
#abort, #assert, #calloc, coerce, #comparable?, #constructible?, #destructible?, #extern, #free, #hash, #hashable?, #initializable?, #inline, #malloc, #method_missing, #orderable?, #prefix, #private?, #public?, #sortable?, #static, #static?, #write_decls, #write_defs, #write_intf
Methods inherited from Code
#attach, #priority, #source_size, #write_decls, #write_defs, #write_intf
Constructor Details
#initialize(target) ⇒ Reference
Returns a new instance of Reference.
428 429 430 431 432 433 434 435 436 437 438 |
# File 'lib/autoc/type.rb', line 428 def initialize(target) @target = Type.coerce(target) super(@target.type_ref) # NOTE : the type of the Reference instance itself is actually a pointer type @init = Dispatcher::ParameterArray.new(@target.ctor.parameters[1..-1]) # Capture extra parameters from the target type constructor define_callable(:ctor, @init) {def call(obj, *params) "((#{obj}) = #{@ref.new?}(#{params.join(',')}))" end} define_callable(:dtor, [type]) {def call(obj) "#{@ref.free?}(#{obj})" end} define_callable(:copy, [type, type]) {def call(dst, src) "((#{dst}) = #{@ref.ref?}(#{src}))" end} define_callable(:equal, [type, type]) {def call(lt, rt) @target.equal("*#{lt}", "*#{rt}") end} define_callable(:less, [type, type]) {def call(lt, rt) @target.less("*#{lt}", "*#{rt}") end} define_callable(:identify, [type]) {def call(obj) @target.identify("*#{obj}") end} end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method in the class AutoC::Type
Instance Attribute Details
#target ⇒ Object (readonly)
Returns the value of attribute target.
426 427 428 |
# File 'lib/autoc/type.rb', line 426 def target @target end |
Instance Method Details
#==(other) ⇒ Object Also known as: eql?
440 |
# File 'lib/autoc/type.rb', line 440 def ==(other) @target == other.instance_variable_get(:@target) end |
#copyable? ⇒ Boolean
Return true since reference copying involves no call to the underlying type’s copy constructor
424 |
# File 'lib/autoc/type.rb', line 424 def copyable?; true end |
#entities ⇒ Object
444 |
# File 'lib/autoc/type.rb', line 444 def entities; super << @target end |
#write_impls(stream, define) ⇒ Object
457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 |
# File 'lib/autoc/type.rb', line 457 def write_impls(stream, define) stream << %$ #define AUTOC_COUNTER(p) (*(size_t*)((char*)(p) + sizeof(#{@target.type}))) #{define} #{type} #{new?}(#{@init.definition}) { #{type} self = (#{type})#{malloc}(sizeof(#{@target.type}) + sizeof(size_t)); #{assert}(self); #{@target.ctor("*self", *@init.names)}; AUTOC_COUNTER(self) = 1; return self; } #{define} #{type} #{ref?}(#{type} self) { #{assert}(self); ++AUTOC_COUNTER(self); return self; } #{define} void #{free?}(#{type} self) { #{assert}(self); if(--AUTOC_COUNTER(self) == 0) { #{@target.dtor("*self")}; #{free}(self); } } #undef AUTOC_COUNTER $ end |
#write_intf_decls(stream, declare, define) ⇒ Object
446 447 448 449 450 451 452 453 454 455 |
# File 'lib/autoc/type.rb', line 446 def write_intf_decls(stream, declare, define) stream << %$ /*** **** <#{type}> (#{self.class}) ***/ #{declare} #{type} #{new?}(#{@init.declaration}); #{declare} #{type} #{ref?}(#{type}); #{declare} void #{free?}(#{type}); $ end |