Class: CF::Base Abstract
- Inherits:
-
Object
- Object
- CF::Base
- Defined in:
- lib/corefoundation/base.rb
Overview
The base class for all of the wrapper classes
Defined Under Namespace
Classes: Releaser
Constant Summary collapse
- @@type_map =
{}
Class Method Summary collapse
-
.check_cftype(cftyperef) ⇒ Object
Raises an exception if the argument does not inherit from CF::Base.
-
.typecast(cftyperef) ⇒ Object
Wraps an FFI::Pointer with the appropriate subclass of CF::Base If the pointer is not a CFTypeRef behaviour is undefined.
Instance Method Summary collapse
-
#eql?(other) ⇒ Boolean
(also: #==)
eql? (and ==) are implemented using CFEqual.
-
#equals?(other) ⇒ Boolean
equals? is defined as returning true if the wrapped pointer is the same.
-
#hash ⇒ Integer
Uses CFHash to return a hash code.
-
#initialize(ptr) ⇒ Base
constructor
A new instance of Base.
-
#inspect ⇒ String
Returns a ruby string containing the output of CFCopyDescription for the wrapped object.
-
#null? ⇒ Boolean
Whether the instance is the CFNull singleton.
-
#ptr=(ptr) ⇒ Object
Sets the wrapper pointer.
-
#release ⇒ Object
Calls CFRelease on the wrapped pointer.
-
#release_on_gc ⇒ Object
Installs a finalizer on the wrapper object that will cause it to call CFRelease on the pointer when the wrapper is garbage collected.
-
#retain ⇒ Object
Calls CFRetain on the wrapped pointer.
-
#to_cf ⇒ Object
to_cf is a no-op on CF::Base and its descendants - it always returns self.
-
#to_ptr ⇒ FFI::Pointer
Returns the wrapped pointer.
-
#to_ruby ⇒ Object
Converts the CF object into a ruby object.
Constructor Details
#initialize(ptr) ⇒ Base
Returns a new instance of Base.
97 98 99 |
# File 'lib/corefoundation/base.rb', line 97 def initialize(ptr) @ptr = FFI::Pointer.new(ptr) end |
Class Method Details
.check_cftype(cftyperef) ⇒ Object
Raises an exception if the argument does not inherit from CF::Base
65 66 67 |
# File 'lib/corefoundation/base.rb', line 65 def check_cftype(cftyperef) raise TypeError, "#{cftyperef.inspect} is not a cftype" unless cftyperef.is_a?(CF::Base) end |
.typecast(cftyperef) ⇒ Object
Wraps an FFI::Pointer with the appropriate subclass of CF::Base If the pointer is not a CFTypeRef behaviour is undefined
73 74 75 76 |
# File 'lib/corefoundation/base.rb', line 73 def typecast(cftyperef) klass = klass_from_cf_type cftyperef klass.new(cftyperef) end |
Instance Method Details
#eql?(other) ⇒ Boolean Also known as: ==
eql? (and ==) are implemented using CFEqual
161 162 163 164 165 166 167 |
# File 'lib/corefoundation/base.rb', line 161 def eql?(other) if other.is_a?(CF::Base) CF.CFEqual(self, other) != 0 else false end end |
#equals?(other) ⇒ Boolean
equals? is defined as returning true if the wrapped pointer is the same
172 173 174 175 176 177 178 |
# File 'lib/corefoundation/base.rb', line 172 def equals?(other) if other.is_a?(CF::Base) @ptr.address == other.to_ptr.address else false end end |
#hash ⇒ Integer
Uses CFHash to return a hash code
154 155 156 |
# File 'lib/corefoundation/base.rb', line 154 def hash CF.CFHash(self) end |
#inspect ⇒ String
Returns a ruby string containing the output of CFCopyDescription for the wrapped object
146 147 148 149 |
# File 'lib/corefoundation/base.rb', line 146 def inspect cf = CF::String.new(CF.CFCopyDescription(self)) cf.to_s.tap {cf.release} end |
#null? ⇒ Boolean
Whether the instance is the CFNull singleton
104 105 106 |
# File 'lib/corefoundation/base.rb', line 104 def null? equals?(CF::NULL) end |
#ptr=(ptr) ⇒ Object
Sets the wrapper pointer. You should only do this rarely. If you have used release_on_gc then that finalizer will still be called on the original pointer value
117 118 119 |
# File 'lib/corefoundation/base.rb', line 117 def ptr= ptr @ptr = ptr end |
#release ⇒ Object
Calls CFRelease on the wrapped pointer
130 131 132 133 |
# File 'lib/corefoundation/base.rb', line 130 def release CF.release(self) self end |
#release_on_gc ⇒ Object
Installs a finalizer on the wrapper object that will cause it to call CFRelease on the pointer when the wrapper is garbage collected
138 139 140 141 |
# File 'lib/corefoundation/base.rb', line 138 def release_on_gc ObjectSpace.define_finalizer(@ptr, Releaser.new(@ptr)) self end |
#retain ⇒ Object
Calls CFRetain on the wrapped pointer
123 124 125 126 |
# File 'lib/corefoundation/base.rb', line 123 def retain CF.retain(self) self end |
#to_cf ⇒ Object
to_cf is a no-op on CF::Base and its descendants - it always returns self
192 193 194 |
# File 'lib/corefoundation/base.rb', line 192 def to_cf self end |
#to_ptr ⇒ FFI::Pointer
Returns the wrapped pointer
110 111 112 |
# File 'lib/corefoundation/base.rb', line 110 def to_ptr @ptr end |
#to_ruby ⇒ Object
Converts the CF object into a ruby object. Subclasses typically override this to return an appropriate object (for example CF::String returns a String)
185 186 187 |
# File 'lib/corefoundation/base.rb', line 185 def to_ruby self end |