8#include "Arp/System/Core/IEnumerator.hxx"
9#include "Arp/System/Core/Exception.hpp"
85 using Stack = std::stack<typename IEnumerator<T>::Ptr>;
119 template<
class Iterator>
120 class IteratorAdapters
126 typename Iterator::reference operator()(Iterator i)
const
133 const typename Iterator::value_type::first_type& operator()(Iterator i)
const
140 typename Iterator::value_type::second_type& operator()(Iterator i)
const
147 const typename Iterator::value_type::second_type& operator()(Iterator i)
const
159 template<class Iterator, class IteratorAdapter = typename IteratorAdapters<Iterator>::Value>
186 bool firstMove =
true;
187 IteratorAdapter iteratorAdapter;
193 template<
class Predicate>
216 template<
typename SourceEnumerator,
typename TransformOperation>
221 using Source = SourceEnumerator;
226 TransformEnumerator(std::shared_ptr<SourceEnumerator> source, TransformOperation transformOp);
232 std::shared_ptr<SourceEnumerator> source;
233 TransformOperation transformOp;
277 template<
class TIterator>
284 template<
class TContainer>
291 template<
class TContainer>
298 template<
class TContainer>
305 template<
class TContainer>
312 template<
class TContainer>
320 template<
class Predicate>
329 template<
class SourceEnumerator,
class TransformOperation>
340 using TCurrent =
typename std::remove_const<typename std::remove_reference<T>::type>::type;
357 throw Exception(
"IEnumerator::GetCurrent() is not supported by empty enumerators");
374 if (this->first->MoveNext())
381 return this->second->MoveNext();
389 return this->first->GetCurrent();
392 return this->second->GetCurrent();
400 this->enumerators.push(e);
406 while (!this->enumerators.empty())
408 if (this->enumerators.top()->MoveNext())
413 this->enumerators.pop();
422 return this->enumerators.top()->GetCurrent();
428 return this->enumerators.size();
434template<
class Iterator,
class IteratorAdapter>
443template<
class Iterator,
class IteratorAdapter>
448 this->firstMove =
false;
449 return this->begin != this->end;
452 return ++this->begin != this->end;
456template<
class Iterator,
class IteratorAdapter>
459 return this->iteratorAdapter(this->begin);
465template<
class Predicate>
468 : source(
std::move(source)), predicate(
std::move(predicate))
474template<
class Predicate>
477 while (this->source->MoveNext())
479 if (this->predicate(this->source->GetCurrent()))
489template<
class Predicate>
492 return this->source->GetCurrent();
498template<
class SourceEnumerator,
class TransformOperation>
499Enumerator<T>::TransformEnumerator<SourceEnumerator, TransformOperation>::TransformEnumerator(
500 std::shared_ptr<SourceEnumerator> source, TransformOperation transformOp)
501 : source(
std::move(source)), transformOp(
std::move(transformOp))
507template<
class SourceEnumerator,
class TransformOperation>
510 return this->source->MoveNext();
515template<
class SourceEnumerator,
class TransformOperation>
518 return this->transformOp(this->source->GetCurrent());
533 return std::make_shared<Empty>();
539 return std::make_shared<Composite>(first, second);
543template<
class TIterator>
546 return std::make_shared<StlAdapter<TIterator>>(begin, end);
550template<
class TContainer>
557template<
class TContainer>
560 return std::make_shared<StlAdapter<typename TContainer::iterator, typename IteratorAdapters<typename TContainer::iterator>::ConstKey>>(c.begin(), c.end());
564template<
class TContainer>
567 return std::make_shared<StlAdapter<typename TContainer::const_iterator, typename IteratorAdapters<typename TContainer::const_iterator>::ConstKey>>(c.begin(), c.end());
571template<
class TContainer>
574 return std::make_shared<StlAdapter<typename TContainer::iterator, typename IteratorAdapters<typename TContainer::iterator>::Mapped>>(c.begin(), c.end());
578template<
class TContainer>
581 return std::make_shared<StlAdapter<typename TContainer::const_iterator, typename IteratorAdapters<typename TContainer::const_iterator>::ConstMapped>>(c.begin(), c.end());
585template<
class Predicate>
589 return std::make_shared<Enumerator<T>::FilterEnumerator<Predicate>>(std::move(source), predicate);
593template<
class SourceEnumerator,
class TransformOperation>
596 return std::make_shared<Enumerator<T>::TransformEnumerator<SourceEnumerator, TransformOperation>>(
597 std::move(source), transformOp);
Use this class to build a single enumerator by two given enumerator, e.g. to enumerate multiple conta...
Definition: Enumerator.hxx:53
Composite(typename IEnumerator< T >::Ptr first, typename IEnumerator< T >::Ptr second)
Constructs an Composite instance.
Definition: Enumerator.hxx:363
Composite(const Composite &arg)=default
Copy constructor.
bool MoveNext(void) override
Moves this enumerator to the next position.
Definition: Enumerator.hxx:370
virtual ~Composite(void)=default
Destructs this instance and frees all resources.
T GetCurrent(void) override
Gets the element at the current position.
Definition: Enumerator.hxx:385
Composite & operator=(const Composite &arg)=default
Assignment operator.
Implements an empty enumerator, that is, the first call of MoveNext() will return false.
Definition: Enumerator.hxx:27
bool MoveNext(void) override
Moves this enumerator to the next position.
Definition: Enumerator.hxx:349
Empty & operator=(const Empty &arg)=default
Assignment operator.
virtual ~Empty(void)=default
Destructs this instance and frees all resources.
T GetCurrent(void) override
Gets the element at the current position.
Definition: Enumerator.hxx:355
Empty(const Empty &arg)=default
Copy constructor.
Empty(void)=default
Constructs an Empty instance.
Use this class to build a single enumerator by two given enumerator, e.g. to enumerate multiple conta...
Definition: Enumerator.hxx:83
void Push(typename IEnumerator< T >::Ptr e)
Adds the as argument passed enumerator to this stack.
Definition: Enumerator.hxx:398
bool MoveNext(void) override
Moves this enumerator to the next position.
Definition: Enumerator.hxx:404
StackComposite(void)=default
Constructs an StackComposite instance.
virtual ~StackComposite(void)=default
Destructs this instance and frees all resources.
size_t GetSize() const
Get the size of this stack.
Definition: Enumerator.hxx:426
StackComposite(const StackComposite &arg)=default
Copy constructor.
StackComposite & operator=(const StackComposite &arg)=default
Assignment operator.
T GetCurrent(void) override
Gets the element at the current position.
Definition: Enumerator.hxx:420
This class implements an enumerator adapter for STL container based on iterators.
Definition: Enumerator.hxx:161
bool MoveNext(void) override
Moves this enumerator to the next position.
Definition: Enumerator.hxx:444
StlAdapter & operator=(const StlAdapter &arg)=default
Assignment operator.
StlAdapter(Iterator begin, Iterator end)
Constructs an StlAdapter instance.
Definition: Enumerator.hxx:435
virtual ~StlAdapter(void)=default
Destructs this instance and frees all resources.
StlAdapter(const StlAdapter &arg)=default
Copy constructor.
T GetCurrent(void) override
Gets the element at the current position.
Definition: Enumerator.hxx:457
This class defines a base class for all enumerator implementations and some predefined enumerators as...
Definition: Enumerator.hxx:22
virtual bool MoveNext(void)=0
Moves this enumerator to the next position.
TCurrent current
The current field of this enumerator.
Definition: Enumerator.hxx:343
static IEnumerator< T >::Ptr CreateTransform(std::shared_ptr< SourceEnumerator > source, TransformOperation transformOp)
Creates a transforming adapter enumerating applying a transform operation to each element.
Definition: Enumerator.hxx:594
static IEnumerator< T >::Ptr CreateKeysAdapter(const TContainer &c)
Creates an enumerator adapter from the given const STL map enumerating the keys.
Definition: Enumerator.hxx:565
static IEnumerator< T >::Ptr CreateMappedAdapter(const TContainer &c)
Creates an enumerator adapter from the given const STL map enumerating the mapped items.
Definition: Enumerator.hxx:579
Enumerator(void)=default
Constructs an Enumerator instance.
static IEnumerator< T >::Ptr CreateKeysAdapter(TContainer &c)
Creates an enumerator adapter from the given STL map enumerating the keys.
Definition: Enumerator.hxx:558
virtual ~Enumerator(void)=default
Destructs this instance and frees all resources.
static IEnumerator< T >::Ptr CreateEmpty(void)
Creates an empty enumerator.
Definition: Enumerator.hxx:531
static Empty Null
A static empty enumerator instance.
Definition: Enumerator.hxx:252
static IEnumerator< T >::Ptr CreateMappedAdapter(TContainer &c)
Creates an enumerator adapter from the given STL map enumerating the mapped items.
Definition: Enumerator.hxx:572
static IEnumerator< T >::Ptr CreateStlAdapter(TIterator begin, TIterator end)
Creates an enumerator adapter from the given STL iterators.
Definition: Enumerator.hxx:544
T GetCurrent(void) override
Gets the element at the current position.
Definition: Enumerator.hxx:525
Enumerator & operator=(const Enumerator &arg)=default
Assignment operator.
static IEnumerator< T >::Ptr CreateComposite(typename IEnumerator< T >::Ptr first, typename IEnumerator< T >::Ptr second)
Creates a composite enumerator from the two given enumerators, e.g. to enumerate trees easily.
Definition: Enumerator.hxx:537
static IEnumerator< T >::Ptr CreateStlAdapter(TContainer &c)
Creates an enumerator adapter from the given STL container.
Definition: Enumerator.hxx:551
static IEnumerator< T >::Ptr CreateFilter(typename IEnumerator< T >::Ptr source, Predicate predicate)
Creates a filtering adapter enumerating only the nodes matching a given predicate.
Definition: Enumerator.hxx:586
Enumerator(const Enumerator &arg)=default
Copy constructor.
This is the base class of all Arp exception classes.
Definition: Exception.hpp:16
Declares the interface of the enumerator pattern, which is leaned on .NET enumerator idiom.
Definition: IEnumerator.hxx:48
std::shared_ptr< IEnumerator > Ptr
The smart pointer tpye of this interface.
Definition: IEnumerator.hxx:51
Root namespace for the PLCnext API
Namespace of the C++ standard library