Class: CF::Base Abstract

Inherits:
Object
  • Object
show all
Defined in:
lib/corefoundation/base.rb

Overview

This class is abstract.

The base class for all of the wrapper classes

Direct Known Subclasses

Array, Boolean, Data, Date, Dictionary, Null, Number, String

Defined Under Namespace

Classes: Releaser

Constant Summary collapse

@@type_map =
{}

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(ptr) ⇒ Base

Returns a new instance of Base.

Parameters:

  • ptr (FFI::Pointer)

    The pointer to wrap



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

Parameters:

  • cftyperef

    object to check

Raises:

  • (TypeError)


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

Parameters:

  • cftyperef (FFI::Pointer)

    Object to wrap

Returns:

  • A wrapper object inheriting from CF::Base



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

Returns:



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

Returns:



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

#hashInteger

Uses CFHash to return a hash code

Returns:



154
155
156
# File 'lib/corefoundation/base.rb', line 154

def hash
  CF.CFHash(self)
end

#inspectString

Returns a ruby string containing the output of CFCopyDescription for the wrapped object

Returns:



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

Returns:



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

#releaseObject

Calls CFRelease on the wrapped pointer

Returns:

  • Returns self



130
131
132
133
# File 'lib/corefoundation/base.rb', line 130

def release
  CF.release(self)
  self
end

#release_on_gcObject

Installs a finalizer on the wrapper object that will cause it to call CFRelease on the pointer when the wrapper is garbage collected

Returns:

  • Returns self



138
139
140
141
# File 'lib/corefoundation/base.rb', line 138

def release_on_gc
  ObjectSpace.define_finalizer(@ptr, Releaser.new(@ptr))
  self
end

#retainObject

Calls CFRetain on the wrapped pointer

Returns:

  • Returns self



123
124
125
126
# File 'lib/corefoundation/base.rb', line 123

def retain
  CF.retain(self)
  self
end

#to_cfObject

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_ptrFFI::Pointer

Returns the wrapped pointer

Returns:

  • (FFI::Pointer)


110
111
112
# File 'lib/corefoundation/base.rb', line 110

def to_ptr
  @ptr
end

#to_rubyObject

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