Class: AutoC::Collection

Inherits:
Type show all
Includes:
Redirecting
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, TreeMap, TreeSet, Vector

Instance Attribute Summary collapse

Attributes inherited from Type

#type, #type_ref

Instance Method Summary collapse

Methods inherited from Type

#abort, #assert, #calloc, coerce, #extern, #free, #inline, #malloc, #method_missing, #orderable?, #prefix, #private?, #public?, #sortable?, #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.



93
94
95
96
97
98
99
# File 'lib/autoc/collection.rb', line 93

def initialize(type_name, element_type, visibility = :public)
  super(type_name, visibility)
  @it_ref = "#{it}*"
  @element = Type.coerce(element_type)
  initialize_redirectors
  element_requirement(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.



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

def element
  @element
end

#it_refObject (readonly)

Returns the value of attribute it_ref.



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

def it_ref
  @it_ref
end

Instance Method Details

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



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

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

#comparable?Boolean

Collection always has equality tester but the element is required to be comparable on its own because collection comparison incurs comparison of all contained elements

Returns:

  • (Boolean)


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

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

#constructible?Boolean

Collection always has default constructor

Returns:

  • (Boolean)


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

def constructible?; true end

#copyable?Boolean

Collection always has copy constructor but the element is required to be copyable on its own because collection copying incurs copying of all contained elements

Returns:

  • (Boolean)


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

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

#destructible?Boolean

Collection always has destructor but the element is required to be destructible on its own because collection destruction incurs destruction of all contained elements

Returns:

  • (Boolean)


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

def destructible?; true && element.destructible? end

#entitiesObject



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

def entities; super << element end

#hashObject



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

def hash; super ^ element.hash end

#hashable?Boolean

Collection always has hash calculation function but the element is required to be hashable on its own because collection comparison incurs hash calculation for all contained elements

Returns:

  • (Boolean)


126
127
128
129
# File 'lib/autoc/collection.rb', line 126

def hashable?
  # Since using collection as an element of a hash-based container also requires it to be comparable as well 
  comparable? && element.hashable?
end

#initializable?Boolean

Collection always has constructor

Returns:

  • (Boolean)


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

def initializable?; true end

#write_intf_decls(stream, declare, define) ⇒ Object



131
132
133
# File 'lib/autoc/collection.rb', line 131

def write_intf_decls(stream, declare, define)
  write_redirectors(stream, declare, define)
end