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

Returns a new instance of Base.

Parameters:

  • pointer (FFI::Pointer)

    The pointer to wrap



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

def initialize(pointer)
  @ptr = FFI::Pointer.new(pointer)
  ObjectSpace.define_finalizer(self, self.class.finalize(ptr))
  CF.retain(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

Parameters:

  • cftyperef

    object to check

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

Parameters:

  • pointer (FFI::Pointer)

    to the address of the object



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

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

Parameters:

  • cftyperef (FFI::Pointer)

    Object to wrap

Returns:

  • A wrapper object inheriting from CF::Base



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

Returns:



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

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:



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

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:

  • (Integer)


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

def hash
  CF.CFHash(self)
end

#null?Boolean

Whether the instance is the CFNull singleton

Returns:



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

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

#to_cfObject

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

Returns:

  • Returns self



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

def to_cf
  self
end

#to_rubyObject

Raises:

  • (CF::Exceptions::MethodNotImplemented)


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

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