TurtleBrains  0.3.5
High quality, portable, C++ framework for rapid 2D game development.
tb_typed_range.hpp
1 
9 #ifndef TurtleBrains_TypedRange_hpp
10 #define TurtleBrains_TypedRange_hpp
11 
12 #include <turtle_brains/core/tb_types.hpp>
13 
14 #include <functional>
15 #include <type_traits>
16 
17 namespace TurtleBrains::Core
18 {
19 
20  template<typename IndexType, typename ValueType, size_t kMaximumIndex, ValueType& (*AccessFunction)(const IndexType index)> struct TypedRange
21  {
22  class Iterator
23  {
24  public:
25  explicit Iterator(const IndexType index) :
26  mIndex(index)
27  {
28  }
29 
30  Iterator operator++(void) { ++mIndex; return *this; }
31  Iterator operator++(int) { Iterator value = *this; ++mIndex; return value; }
32  bool operator==(const Iterator& other) const { return mIndex == other.mIndex; }
33  bool operator!=(const Iterator& other) const { return mIndex != other.mIndex; }
34  ValueType& operator*(void) const { return AccessFunction(mIndex); }
35 
36  private:
37  IndexType mIndex;
38  };
39 
40  Iterator begin(void) const { return Iterator{ 0 }; }
41  Iterator end(void) const { return Iterator{ kMaximumIndex }; }
42  };
43 
44 
45  template<typename IndexType, typename ValueType, IndexType (*SizeFunction)(void), ValueType& (*AccessFunction)(const IndexType index)> struct DynamicTypedRange
46  {
47  class Iterator
48  {
49  public:
50  explicit Iterator(const IndexType index) :
51  mIndex(index)
52  {
53  }
54 
55  Iterator operator++(void) { ++mIndex; return *this; }
56  Iterator operator++(int) { Iterator value = *this; ++mIndex; return value; }
57  bool operator==(const Iterator& other) const { return mIndex == other.mIndex; }
58  bool operator!=(const Iterator& other) const { return mIndex != other.mIndex; }
59  ValueType& operator*(void) const { return AccessFunction(mIndex); }
60 
61  private:
62  IndexType mIndex;
63  };
64 
65  Iterator begin(void) const { return Iterator{ 0 }; }
66  Iterator end(void) const { return Iterator{ SizeFunction() }; }
67  };
68 
69 
70  template<typename IndexType, typename ValueType> struct OtherTypedRange
71  {
72  OtherTypedRange(std::function<IndexType()> sizeFunction, std::function<ValueType(IndexType)> accessFunction) :
73  mSizeFunction(sizeFunction),
74  mAccessFunction(accessFunction)
75  {
76  }
77 
78  class Iterator
79  {
80  public:
81  explicit Iterator(const IndexType index, std::function<ValueType(IndexType)> accessFunction) :
82  mIndex(index),
83  mAccessFunction(accessFunction)
84  {
85  }
86 
87  Iterator operator++(void) { ++mIndex; return *this; }
88  Iterator operator++(int) { Iterator value = *this; ++mIndex; return value; }
89  bool operator==(const Iterator& other) const { return mIndex == other.mIndex; }
90  bool operator!=(const Iterator& other) const { return mIndex != other.mIndex; }
91  ValueType operator*(void) const { return mAccessFunction(mIndex); }
92 
93  private:
94  IndexType mIndex;
95  std::function<ValueType(IndexType)> mAccessFunction;
96  };
97 
98  Iterator begin(void) const { return Iterator{ 0, mAccessFunction }; }
99  Iterator end(void) const { return Iterator{ mSizeFunction(), mAccessFunction }; }
100 
101  std::function<IndexType()> mSizeFunction;
102  std::function<ValueType(IndexType)> mAccessFunction;
103  };
104 
105 
106  namespace Internals
107  {
108  template<typename MapContainerType> struct ValuesOfAccessor
109  {
110  MapContainerType& mContainer;
111 
112  ValuesOfAccessor(MapContainerType& container) :
113  mContainer(container)
114  {
115  }
116 
117  using BaseIterator = typename std::conditional<std::is_const<MapContainerType>::value,
118  typename MapContainerType::const_iterator,
119  typename MapContainerType::iterator>::type;
120 
121  using ValueType = typename std::conditional<std::is_const<MapContainerType>::value,
122  const typename MapContainerType::mapped_type, typename MapContainerType::mapped_type>::type;
123 
124  class Iterator : public BaseIterator
125  {
126  public:
127  explicit Iterator(BaseIterator iterator) :
128  BaseIterator(iterator) { }
129 
130  ValueType& operator*(void) const { return (*this)->second; }
131  };
132 
133  Iterator begin(void) const { return Iterator(mContainer.begin()); }
134  Iterator end(void) const { return Iterator(mContainer.end()); }
135  Iterator begin(void) { return Iterator(mContainer.begin()); }
136  Iterator end(void) { return Iterator(mContainer.end()); }
137  };
138  };
139 
148  template<typename MapContainerType> Internals::ValuesOfAccessor<MapContainerType> ValuesOf(MapContainerType& container)
149  {
151  }
152 
153 }; //namespace TurtleBrains::Core
154 
155 namespace tbCore = TurtleBrains::Core;
156 
157 #endif /* TurtleBrains_TypedRange_hpp */
Definition: tb_typed_range.hpp:48
Definition: tb_typed_range.hpp:79
Definition: tb_typed_range.hpp:23
Contains core functionality for each component of the API.
Definition: tb_debug_logger.hpp:125
Internals::ValuesOfAccessor< MapContainerType > ValuesOf(MapContainerType &container)
Definition: tb_typed_range.hpp:148
Definition: tb_typed_range.hpp:46
Definition: tb_typed_range.hpp:109
Definition: tb_typed_range.hpp:71
Definition: tb_typed_range.hpp:21