Class: AutoC::Collection

Inherits:
Type show all
Defined in:
lib/autoc/collection.rb

Overview

Implemented types

Implemented collections

Ruby side operation

Complete example for generation of a list of integers collection:
require "autoc"
AutoC::Module.generate!(:Test) do |c|
  c << AutoC::List.new(:IntList, :int)
end

In the above example a C module Test represented by the C header test_auto.h and the C source test_auto.c is generated. The C++ counterpart of the generated collection is std::forward_list<int>.

C interface

Element types: values, references

Collections may contain both value and reference types, including other collections.

Thread safety

Warning
In its current state the implemented collections are not thread-safe.

Iteration

At the moment a fairly simple iteration functionality is implemented. The iterators are modeled after the C# language. All implemented iterators do not require destruction after use.

Basic iterator usage example:
MyVector c;
MyVectorIt it;
...
MyVectorItCtor(&it, &c);
while(MyVectorItMove(&it)) {
  Element e = MyVectorItGet(&it);
  ...
  ElementDtor(e);
}
Warning
the collection being iterated must not be modified in any way otherwise the iterator behavior is undefined.

Direct Known Subclasses

HashMap, HashSet, List, Queue, Vector

Instance Attribute Summary collapse

Attributes inherited from Type

#type, #type_ref

Instance Method Summary collapse

Methods inherited from Type

#abort, #assert, #calloc, coerce, #constructible?, #destructible?, #extern, #free, #inline, #malloc, #method_missing, #orderable?, #prefix, #private?, #public?, #static, #static?, #write_decls, #write_defs, #write_intf

Methods inherited from Code

#attach, #priority, #source_size, #write_decls, #write_defs, #write_intf

Constructor Details

#initialize(type_name, element_type, visibility = :public) ⇒ Collection

Returns a new instance of Collection.



85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/autoc/collection.rb', line 85

def initialize(type_name, element_type, visibility = :public)
  super(type_name, visibility)
  @it_ref = "#{it}*"
  @element = Type.coerce(element_type)
  @ctor = define_function(:ctor, Function::Signature.new([type_ref^:self]))
  @dtor = define_function(:dtor, Function::Signature.new([type_ref^:self]))
  @copy = define_function(:copy, Function::Signature.new([type_ref^:dst, type_ref^:src]))
  @equal = define_function(:equal, Function::Signature.new([type_ref^:lt, type_ref^:rt], :int))
  @identify = define_function(:identify, Function::Signature.new([type_ref^:self], :size_t))
  @less = define_function(:less, Function::Signature.new([type_ref^:lt, type_ref^:rt], :int))
  element_type_check(element)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class AutoC::Type

Instance Attribute Details

#elementObject (readonly)

Returns the value of attribute element.



75
76
77
# File 'lib/autoc/collection.rb', line 75

def element
  @element
end

#it_refObject (readonly)

Returns the value of attribute it_ref.



75
76
77
# File 'lib/autoc/collection.rb', line 75

def it_ref
  @it_ref
end

Instance Method Details

#==(other) ⇒ Object Also known as: eql?



79
# File 'lib/autoc/collection.rb', line 79

def ==(other) super && element == other.element end

#comparable?Boolean

Returns:

  • (Boolean)


117
# File 'lib/autoc/collection.rb', line 117

def comparable?; super && element.comparable? end

#copyable?Boolean

Returns:

  • (Boolean)


115
# File 'lib/autoc/collection.rb', line 115

def copyable?; super && element.copyable? end

#entitiesObject



83
# File 'lib/autoc/collection.rb', line 83

def entities; super << element end

#hashObject



77
# File 'lib/autoc/collection.rb', line 77

def hash; super ^ element.hash end

#hashable?Boolean

Returns:

  • (Boolean)


119
# File 'lib/autoc/collection.rb', line 119

def hashable?; super && element.hashable? end

#write_intf_decls(stream, declare, define) ⇒ Object



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/autoc/collection.rb', line 98

def write_intf_decls(stream, declare, define)
  # Emit default redirection macros
  # Unlike other special methods the constructors may have extra arguments
  # Assume the constructor's first parameter is always a target
  ctor_ex = ctor.parameters.names[1..-1]
  ctor_lt = ["self"].concat(ctor_ex).join(',')
  ctor_rt = ["&self"].concat(ctor_ex).join(',')
  stream << %$
    #define _#{ctor}(#{ctor_lt}) #{ctor}(#{ctor_rt})
    #define _#{dtor}(self) #{dtor}(&self)
    #define _#{identify}(self) #{identify}(&self)
    #define _#{copy}(dst,src) #{copy}(&dst,&src)
    #define _#{equal}(lt,rt) #{equal}(&lt,&rt)
    #define _#{less}(lt,rt) #{less}(&lt,&rt)
  $
end