Class: CF::Base

Inherits:
Object
  • Object
show all
Includes:
Memory, Register
Defined in:
lib/corefoundation/base.rb

Overview

The base class for all of the wrapper classes in CF module.

Direct Known Subclasses

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

Constant Summary collapse

@@type_map =
{}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Memory

#inspect, #release, #retain, #to_ptr

Methods included from Register

included, #register_type

Constructor Details

#initialize(pointer) ⇒ Base



33
34
35
36
# File 'lib/corefoundation/base.rb', line 33

def initialize(pointer)
  @ptr = FFI::Pointer.new(pointer)
  ObjectSpace.define_finalizer(self, self.class.finalize(ptr))
end

Instance Attribute Details

#ptrObject (readonly)

Returns the value of attribute ptr.



13
14
15
# File 'lib/corefoundation/base.rb', line 13

def ptr
  @ptr
end

Class Method Details

.check_cftype(cftyperef) ⇒ Object

Raises an exception if the argument does not inherit from CF::Base

Raises:

  • (TypeError)


18
19
20
# File 'lib/corefoundation/base.rb', line 18

def self.check_cftype(cftyperef)
  raise TypeError, "#{cftyperef.inspect} is not a cftype" unless cftyperef.is_a?(CF::Base)
end

.finalize(pointer) ⇒ Object



39
40
41
# File 'lib/corefoundation/base.rb', line 39

def self.finalize(pointer)
  proc { CF.release(pointer) unless pointer.address.zero }
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



27
28
29
30
# File 'lib/corefoundation/base.rb', line 27

def self.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



58
59
60
61
62
63
64
# File 'lib/corefoundation/base.rb', line 58

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



69
70
71
72
73
74
75
# File 'lib/corefoundation/base.rb', line 69

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



53
54
55
# File 'lib/corefoundation/base.rb', line 53

def hash
  CF.CFHash(self)
end

#null?Boolean

Whether the instance is the CFNull singleton



46
47
48
# File 'lib/corefoundation/base.rb', line 46

def null?
  equals?(CF::NULL)
end

#to_cfObject

This is a no-op on CF::Base and its subclasses. Always returns self.



84
85
86
# File 'lib/corefoundation/base.rb', line 84

def to_cf
  self
end

#to_rubyObject

Raises:

  • (CF::Exceptions::MethodNotImplemented)


77
78
79
# File 'lib/corefoundation/base.rb', line 77

def to_ruby
  raise CF::Exceptions::MethodNotImplemented, "#{self.class} should implement the to_ruby method."
end