Class: CF::Array
- Includes:
- Enumerable
- Defined in:
- lib/corefoundation/array.rb
Overview
Wrapper class for CFArrayRef. It implements enumberable so you can use a lot of your favourite ruby methods on it.
Values returned by the accessor methods or yielded by the block are retained and marked as releasable on garbage collection This means you can safely use the returned values even if the CFArray itself has been destroyed
Unlike ruby arrays you cannot set arbitary array indexes - You can only set indexes in the range 0..length
Instance Attribute Summary
Attributes inherited from Base
Class Method Summary collapse
-
.immutable(array) ⇒ CF::Array
Creates a new, immutable CFArray from a ruby array of cf objects.
-
.mutable ⇒ CF::Array
Creates a new, empty mutable CFArray.
Instance Method Summary collapse
-
#<<(value) ⇒ CF::Array
(also: #push)
Appends a value to the array.
-
#[](index) ⇒ CF::Base
Returns the object at the index.
-
#[]=(index, value) ⇒ CF::Base
Sets object at the index.
-
#each ⇒ Object
Iterates over the array yielding the value to the block The value is wrapped in the appropriate CF::Base subclass and retained (but marked for releasing upon garbage collection).
-
#length ⇒ Integer
(also: #size)
Returns the number of elements the array contains.
-
#mutable? ⇒ Boolean
Whether the array is mutable.
-
#to_ruby ⇒ Object
Returns a ruby array containing the result of calling to_ruby on each of the array's elements.
Methods inherited from Base
check_cftype, #eql?, #equals?, finalize, #hash, #initialize, #null?, #to_cf, typecast
Methods included from Memory
#inspect, #release, #retain, #to_ptr
Methods included from Register
Constructor Details
This class inherits a constructor from CF::Base
Class Method Details
.immutable(array) ⇒ CF::Array
Creates a new, immutable CFArray from a ruby array of cf objects
61 62 63 64 65 66 67 68 |
# File 'lib/corefoundation/array.rb', line 61 def self.immutable(array) if bad_element = array.detect {|value| !value.is_a?(CF::Base)} raise TypeError, "Array contains non cftype #{bad_element.inspect}" end m = FFI::MemoryPointer.new(:pointer, array.length) m.write_array_of_pointer(array) new(CF.CFArrayCreate(nil,m,array.length,CF::kCFTypeArrayCallBacks.to_ptr)) end |
Instance Method Details
#<<(value) ⇒ CF::Array Also known as: push
Appends a value to the array
100 101 102 103 104 105 |
# File 'lib/corefoundation/array.rb', line 100 def <<(value) raise TypeError, "instance is not mutable" unless mutable? self.class.check_cftype(value) CF.CFArrayAppendValue(self, value) self end |
#[](index) ⇒ CF::Base
Returns the object at the index
81 82 83 |
# File 'lib/corefoundation/array.rb', line 81 def [](index) Base.typecast(CF.CFArrayGetValueAtIndex(self, index)) end |
#[]=(index, value) ⇒ CF::Base
Sets object at the index
91 92 93 94 95 |
# File 'lib/corefoundation/array.rb', line 91 def []=(index, value) raise TypeError, "instance is not mutable" unless mutable? self.class.check_cftype(value) CF.CFArraySetValueAtIndex(self, index, value) end |
#each ⇒ Object
Iterates over the array yielding the value to the block The value is wrapped in the appropriate CF::Base subclass and retained (but marked for releasing upon garbage collection)
47 48 49 50 51 52 53 54 55 56 |
# File 'lib/corefoundation/array.rb', line 47 def each range = CF::Range.new range[:location] = 0 range[:length] = length callback = lambda do |value, _| yield Base.typecast(value) end CF.CFArrayApplyFunction(self, range, callback, nil) self end |
#length ⇒ Integer Also known as: size
Returns the number of elements the array contains
117 118 119 |
# File 'lib/corefoundation/array.rb', line 117 def length CF.CFArrayGetCount(self) end |
#mutable? ⇒ Boolean
Whether the array is mutable
WARNING: this only works for arrays created by CF::Array, there is no public api for telling whether an arbitrary CFTypeRef is a mutable array or not
40 41 42 |
# File 'lib/corefoundation/array.rb', line 40 def mutable? @mutable end |
#to_ruby ⇒ Object
Returns a ruby array containing the result of calling to_ruby on each of the array's elements
109 110 111 |
# File 'lib/corefoundation/array.rb', line 109 def to_ruby collect(&:to_ruby) end |