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.
99 100 101 |
# File 'lib/corefoundation/base.rb', line 99 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
67 68 69 |
# File 'lib/corefoundation/base.rb', line 67 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
75 76 77 78 |
# File 'lib/corefoundation/base.rb', line 75 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
163 164 165 166 167 168 169 |
# File 'lib/corefoundation/base.rb', line 163 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
174 175 176 177 178 179 180 |
# File 'lib/corefoundation/base.rb', line 174 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
156 157 158 |
# File 'lib/corefoundation/base.rb', line 156 def hash CF.CFHash(self) end |
#inspect ⇒ String
Returns a ruby string containing the output of CFCopyDescription for the wrapped object
148 149 150 151 |
# File 'lib/corefoundation/base.rb', line 148 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
106 107 108 |
# File 'lib/corefoundation/base.rb', line 106 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
119 120 121 |
# File 'lib/corefoundation/base.rb', line 119 def ptr= ptr @ptr = ptr end |
#release ⇒ Object
Calls CFRelease on the wrapped pointer
132 133 134 135 |
# File 'lib/corefoundation/base.rb', line 132 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
140 141 142 143 |
# File 'lib/corefoundation/base.rb', line 140 def release_on_gc ObjectSpace.define_finalizer(@ptr, Releaser.new(@ptr)) self end |
#retain ⇒ Object
Calls CFRetain on the wrapped pointer
125 126 127 128 |
# File 'lib/corefoundation/base.rb', line 125 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
194 195 196 |
# File 'lib/corefoundation/base.rb', line 194 def to_cf self end |
#to_ptr ⇒ FFI::Pointer
Returns the wrapped pointer
112 113 114 |
# File 'lib/corefoundation/base.rb', line 112 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)
187 188 189 |
# File 'lib/corefoundation/base.rb', line 187 def to_ruby self end |