cppx-core
Enumerated_.hpp
Go to the documentation of this file.
1 #pragma once // Source encoding: UTF-8 with BOM (π is a lowercase Greek "pi").
2 
3 #include <cppx-core/collections/dynamic-size-checking.hpp> // cppx::n_items_of
4 #include <cppx-core/iterators/Forward_iterator_impl_.hpp> // cppx::Forward_iterator_impl_
6 #include <cppx-core/language/syntax/macro-define_tag.hpp> // CPPX_DEFINE_TAG
7 #include <cppx-core/language/types/Truth.hpp> // cppx::Truth
8 #include <cppx-core/meta-type/type-traits.hpp> // cppx::Iterator_for_
9 
10 #include <initializer_list> // std::initializer_list
11 #include <iterator> // std::begin
12 
13 CPPX_DEFINE_TAG( Temporary );
14 
15 namespace cppx
16 {
17  CPPX_USE_STD( declval, initializer_list );
18 
19  template< class Collection >
21  {
22  using Collection_iterator = Iterator_for_<Collection>;
23 
24  P_<Collection> m_p_collection;
25 
26  public:
27  using Item = decltype( *std::begin( declval<Collection&>() ) ); // Usually a ref.
29 
31  {
34  };
35 
36  class Iterator:
37  public Forward_iterator_impl_<Iterator, Item_and_index>
38  {
39  Collection_iterator m_current;
40  Index m_index;
41 
42  public:
43  void advance() { ++m_current; ++m_index; }
44 
45  auto operator*() const
47  { return {*m_current, m_index}; }
48 
49  friend auto operator==( const Iterator& a, const Iterator& b )
50  -> Truth
51  { return a.m_current == b.m_current; }
52 
53  explicit Iterator( const Collection_iterator it, const Index i ):
54  m_current( it),
55  m_index( i )
56  {}
57  };
58 
59  auto begin() const
60  -> Iterator
61  { return Iterator( std::begin( *m_p_collection ), 0 ); }
62 
63  auto end() const
64  -> Iterator
65  { return Iterator( std::end( *m_p_collection ), n_items_of( *m_p_collection ) ); }
66 
67  explicit Enumerated_( Collection& c ):
68  m_p_collection( &c )
69  {}
70 
71  explicit Enumerated_( tag::Temporary, Collection&& c ):
72  m_p_collection( &c )
73  {}
74  };
75 
76  template< class Collection >
77  auto enumerated( Collection& c )
79  { return Enumerated_<Collection>( c ); }
80 
81  template< class Item >
82  auto enumerated( const initializer_list<Item>& list )
84  { return Enumerated_<const initializer_list<Item>>( list ); }
85 } // namespace cppx
Unref_< Item > Item_value
Definition: Enumerated_.hpp:28
Some_type * P_
Creates a raw pointer type.
constexpr auto n_items_of(const Collection &c) noexcept -> Size
A drop-in replacement for bool without implicit conversion from/to types other than bool.
Definition: Truth.hpp:34
friend auto operator==(const Iterator &a, const Iterator &b) -> Truth
Definition: Enumerated_.hpp:49
std::remove_reference_t< Some_reference_type > Unref_
Reduces a type T& or T&& to just T.
Definition: type-makers.hpp:41
CPPX_DEFINE_TAG(Temporary)
Truth is a drop-in replacement for bool without implicit conversion from/to types other than bool.
CPPX_USE_STD(basic_string, basic_string_view, bitset, char_traits, size)
Enumerated_(tag::Temporary, Collection &&c)
Definition: Enumerated_.hpp:71
$define_tag(NAME) defines NAME as a ~unique pointer type in namespace tag.
Size Index
Same as Size but with name signifying use as index.
auto operator *() const -> Item_and_index
Definition: Enumerated_.hpp:45
Simple type builders Type_, P_, R_, Raw_array_ and Raw_array_of_.
Iterator(const Collection_iterator it, const Index i)
Definition: Enumerated_.hpp:53
auto begin() const -> Iterator
Definition: Enumerated_.hpp:59
decltype(begin(declval< Collection & >())) Iterator_for_
Definition: type-traits.hpp:56
auto enumerated(Collection &c) -> Enumerated_< Collection >
Definition: Enumerated_.hpp:77
auto end() const -> Iterator
Definition: Enumerated_.hpp:63
Enumerated_(Collection &c)
Definition: Enumerated_.hpp:67
decltype(*std::begin(declval< Collection & >())) Item
Definition: Enumerated_.hpp:27