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



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

Parameters:

  • cftyperef

    object to check

Raises:

  • (TypeError)


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

Parameters:

  • cftyperef (FFI::Pointer)

    Object to wrap

Returns:

  • A wrapper object inheriting from CF::Base



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

Returns:



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

Returns:



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

#hashInteger

Uses CFHash to return a hash code

Returns:



156
157
158
# File 'lib/corefoundation/base.rb', line 156

def hash
  CF.CFHash(self)
end

#inspectString

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

Returns:



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

Returns:



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

#releaseObject

Calls CFRelease on the wrapped pointer

Returns:

  • Returns self



132
133
134
135
# File 'lib/corefoundation/base.rb', line 132

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



140
141
142
143
# File 'lib/corefoundation/base.rb', line 140

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

#retainObject

Calls CFRetain on the wrapped pointer

Returns:

  • Returns self



125
126
127
128
# File 'lib/corefoundation/base.rb', line 125

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



194
195
196
# File 'lib/corefoundation/base.rb', line 194

def to_cf
  self
end

#to_ptrFFI::Pointer

Returns the wrapped pointer

Returns:

  • (FFI::Pointer)


112
113
114
# File 'lib/corefoundation/base.rb', line 112

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)



187
188
189
# File 'lib/corefoundation/base.rb', line 187

def to_ruby
  self
end