Class: RubyBindgen::Generators::Rice::IteratorCollector

Inherits:
Object
  • Object
show all
Defined in:
lib/ruby-bindgen/generators/rice/iterator_collector.rb

Overview

Tracks iterator-related state across one translation-unit visit:

* Which Ruby iterator names a class exposes (each, each_const,
  each_reverse, each_reverse_const). Drives the each_const-only
  `alias each each_const` emission.
* Which custom iterator types still need a generated
  `std::iterator_traits` specialization (for iterators that don't
  specialize std::iterator_traits themselves), de-duplicated
  across the multiple begin/rbegin callsites that hit the same
  iterator type.

‘record(cursor)` is the single entry point per iterator method. `clear` resets between translation units.

Defined Under Namespace

Classes: Inference

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeIteratorCollector

Returns a new instance of IteratorCollector.



29
30
31
32
# File 'lib/ruby-bindgen/generators/rice/iterator_collector.rb', line 29

def initialize
  @incomplete_iterators = {}
  @iterator_names_by_class = Hash.new { |h, k| h[k] = Set.new }
end

Instance Attribute Details

#incomplete_iteratorsObject (readonly)

Returns the qualified-name → Inference map of iterators that need a generated std::iterator_traits specialization.



27
28
29
# File 'lib/ruby-bindgen/generators/rice/iterator_collector.rb', line 27

def incomplete_iterators
  @incomplete_iterators
end

Instance Method Details

#clearObject



34
35
36
37
# File 'lib/ruby-bindgen/generators/rice/iterator_collector.rb', line 34

def clear
  @incomplete_iterators.clear
  @iterator_names_by_class.clear
end

#each_const_only?(cruby_name) ⇒ Boolean

True when ‘cruby_name` exposes only const iteration. The caller emits an `alias each each_const` so Ruby code can iterate without spelling the const variant.

Returns:

  • (Boolean)


60
61
62
63
# File 'lib/ruby-bindgen/generators/rice/iterator_collector.rb', line 60

def each_const_only?(cruby_name)
  names = @iterator_names_by_class[cruby_name]
  names.include?("each_const") && !names.include?("each")
end

#record(cursor) ⇒ Object

Record an iterator method on its parent class. Returns the Ruby iterator name to render (e.g. “each”, “each_const”) or nil for variants we deliberately skip:

* cbegin/crbegin — would emit a duplicate each_const /
  each_reverse_const since C++ allows them on non-const
  receivers but Ruby has no const distinction.
* end/cend/rend/crend — handled implicitly by Rice's
  define_iterator.


48
49
50
51
52
53
54
55
# File 'lib/ruby-bindgen/generators/rice/iterator_collector.rb', line 48

def record(cursor)
  ruby_name = ruby_iterator_name(cursor)
  return nil unless ruby_name
  
  @iterator_names_by_class[cursor.semantic_parent.cruby_name] << ruby_name
  record_traits_if_needed(cursor.result_type)
  ruby_name
end