PLCnext API Documentation 25.0.2.69
Enumerator.StlAdapter.hxx
1
2//
3// Copyright Phoenix Contact GmbH & Co. KG
4//
6#pragma once
8#include "Arp/Base/Core/Exception.hpp"
9#include "Arp/Base/Core/Detail/Enumerator.hxx"
10#include "Arp/Base/Core/Detail/Enumerator.IteratorAdapters.hxx"
11#include <stack>
12
13namespace Arp { namespace Base { namespace Core
14{
15
20template<class T>
21template<class Iterator, class IteratorAdapter>
22class Enumerator<T>::StlAdapter : public IEnumerator<T>
23{
24public: // construction/destruction
25 StlAdapter(Iterator begin, Iterator end);
26 StlAdapter(const StlAdapter& arg) = delete;
27 StlAdapter(StlAdapter&& arg)noexcept;
28 StlAdapter& operator=(const StlAdapter& arg) = delete;
30 ~StlAdapter(void)override;
31
32public: // overridden operations
33 bool MoveNext(void)override;
34 T GetCurrent(void)override;
35
36private: // fields
37 Iterator begin;
38 Iterator end;
39 bool wasMoved = false;
40 IteratorAdapter iteratorAdapter;
41};
42
44// inline methods of class Enumerator<T>::StlAdapter
45
48template<class T>
49template<class Iterator, class IteratorAdapter>
50inline Enumerator<T>::StlAdapter<Iterator, IteratorAdapter>::StlAdapter(StlAdapter&& arg)noexcept = default;
51
55template<class T>
56template<class Iterator, class IteratorAdapter>
57inline Enumerator<T>::StlAdapter<Iterator, IteratorAdapter>& Enumerator<T>::StlAdapter<Iterator, IteratorAdapter>::operator=(StlAdapter&& arg)noexcept = default;
60template<class T>
61template<class Iterator, class IteratorAdapter>
63
67template<class T>
68template<class Iterator, class IteratorAdapter>
70 : begin(begin)
71 , end(end)
72{
73}
74
75// inherited doc
76template<class T>
77template<class Iterator, class IteratorAdapter>
79{
80 if (this->begin == this->end)
81 {
82 return false;
83 }
84 if (!this->wasMoved)
85 {
86 this->wasMoved = true;
87 }
88 else
89 {
90 ++this->begin;
91 }
92 return this->begin != this->end;
93}
94
95// inherited doc
96template<class T>
97template<class Iterator, class IteratorAdapter>
99{
100 if ((!this->wasMoved) || (this->begin == this->end))
101 {
102 throw Exception("Enumerator<T>::StlAdapter::GetCurrent(): StlAdapter enumerator is empty or enumerating has ended yet");
103 }
104 return this->iteratorAdapter(this->begin);
105}
106
107// Implementation of factory operation of class Enumerator<T>
108
114template<class T>
115template<class TIterator>
116inline typename IEnumerator<T>::Ptr Enumerator<T>::CreateStlAdapter(TIterator begin, TIterator end)
117{
118 return std::make_shared<StlAdapter<TIterator, typename IteratorAdapters<TIterator>::Value>>(begin, end);
119}
120
125template<class T>
126template<class TContainer>
128{
129 return CreateStlAdapter(c.begin(), c.end());
130}
131
136template<class T>
137template<class TContainer>
138inline typename IEnumerator<T>::Ptr Enumerator<T>::CreateStlAdapter(const TContainer& c)
139{
140 return CreateStlAdapter(c.begin(), c.end());
141}
142
147template<class T>
148template<class TContainer>
150{
151 return std::make_shared<StlAdapter<typename TContainer::iterator, typename IteratorAdapters<typename TContainer::iterator>::ConstKey>>(c.begin(), c.end());
152}
153
158template<class T>
159template<class TContainer>
160inline typename IEnumerator<T>::Ptr Enumerator<T>::CreateKeysAdapter(const TContainer& c)
161{
162 return std::make_shared<StlAdapter<typename TContainer::const_iterator, typename IteratorAdapters<typename TContainer::const_iterator>::ConstKey>>(c.begin(), c.end());
163}
164
169template<class T>
170template<class TContainer>
172{
173 return std::make_shared<StlAdapter<typename TContainer::iterator, typename IteratorAdapters<typename TContainer::iterator>::Mapped>>(c.begin(), c.end());
174}
175
180template<class T>
181template<class TContainer>
182inline typename IEnumerator<T>::Ptr Enumerator<T>::CreateMappedAdapter(const TContainer& c)
183{
184 return std::make_shared<StlAdapter<typename TContainer::const_iterator, typename IteratorAdapters<typename TContainer::const_iterator>::ConstMapped>>(c.begin(), c.end());
185}
186
187
188
189}}} // end of namespace Arp::Base::Core
This class implements an enumerator adapter for STL container based on iterators.
Definition: Enumerator.StlAdapter.hxx:23
StlAdapter(StlAdapter &&arg) noexcept
Move constructor.
~StlAdapter(void) override
Destructs this instance and frees all resources.
StlAdapter & operator=(StlAdapter &&arg) noexcept
Move assignment operator.
This class defines a base class for all enumerator implementations and some predefined enumerators as...
Definition: Enumerator.hxx:24
static IEnumerator< T >::Ptr CreateMappedAdapter(TContainer &c)
Creates an enumerator adapter from the given STL map enumerating the mapped items.
Definition: Enumerator.StlAdapter.hxx:171
static IEnumerator< T >::Ptr CreateKeysAdapter(TContainer &c)
Creates an enumerator adapter from the given STL map enumerating the keys.
Definition: Enumerator.StlAdapter.hxx:149
static IEnumerator< T >::Ptr CreateStlAdapter(TIterator begin, TIterator end)
Creates an enumerator adapter from the given STL iterators.
Definition: Enumerator.StlAdapter.hxx:116
T GetCurrent(void) override
Gets the element at the current position.
Definition: Enumerator.hxx:113
This is the base class of all Arp exception classes.
Definition: Exception.hpp:21
Declares the interface of the enumerator pattern, which is leaned on .NET enumerator idiom.
Definition: IEnumerator.hxx:49
virtual bool MoveNext(void)=0
Moves this enumerator to the next position.
std::shared_ptr< IEnumerator > Ptr
The smart pointer type of this interface.
Definition: IEnumerator.hxx:52
Root namespace for the PLCnext API