8#ifndef ARP_USE_ARP_SYSTEM_CORE
10#include "Arp/Base/Core/Enumerator.hxx"
15#include "Arp/System/Core/IEnumerator.hxx"
16#include "Arp/System/Core/Exception.hpp"
29class Enumerator :
public IEnumerator<T>
38 Empty(
void) =
default;
47 virtual ~Empty(
void) =
default;
93 using Stack = std::stack<typename IEnumerator<T>::Ptr>;
127 template<
class Iterator>
128 class IteratorAdapters
134 typename Iterator::reference operator()(Iterator i)
const
141 const typename Iterator::value_type::first_type& operator()(Iterator i)
const
148 typename Iterator::value_type::second_type& operator()(Iterator i)
const
155 const typename Iterator::value_type::second_type& operator()(Iterator i)
const
167 template<class Iterator, class IteratorAdapter = typename IteratorAdapters<Iterator>::Value>
194 bool firstMove =
true;
195 IteratorAdapter iteratorAdapter;
201 template<
class Predicate>
224 template<
typename SourceEnumerator,
typename TransformOperation>
229 using Source = SourceEnumerator;
234 TransformEnumerator(std::shared_ptr<SourceEnumerator> source, TransformOperation transformOp);
240 std::shared_ptr<SourceEnumerator> source;
241 TransformOperation transformOp;
285 template<
class TIterator>
292 template<
class TContainer>
299 template<
class TContainer>
306 template<
class TContainer>
313 template<
class TContainer>
320 template<
class TContainer>
328 template<
class Predicate>
337 template<
class SourceEnumerator,
class TransformOperation>
338 static typename IEnumerator<T>::Ptr CreateTransform(std::shared_ptr<SourceEnumerator> source, TransformOperation transformOp);
348 using TCurrent =
typename std::remove_const<typename std::remove_reference<T>::type>::type;
365 throw Exception(
"IEnumerator::GetCurrent() is not supported by empty enumerators");
378inline bool Enumerator<T>::Composite::MoveNext()
382 if (this->first->MoveNext())
389 return this->second->MoveNext();
393inline T Enumerator<T>::Composite::GetCurrent()
397 return this->first->GetCurrent();
400 return this->second->GetCurrent();
406inline void Enumerator<T>::StackComposite::Push(
typename IEnumerator<T>::Ptr e)
408 this->enumerators.push(e);
412inline bool Enumerator<T>::StackComposite::MoveNext()
414 while (!this->enumerators.empty())
416 if (this->enumerators.top()->MoveNext())
421 this->enumerators.pop();
428inline T Enumerator<T>::StackComposite::GetCurrent()
430 return this->enumerators.top()->GetCurrent();
434inline size_t Enumerator<T>::StackComposite::GetSize()
const
436 return this->enumerators.size();
442template<
class Iterator,
class IteratorAdapter>
443inline Enumerator<T>::StlAdapter<Iterator, IteratorAdapter>::StlAdapter(Iterator begin, Iterator end)
451template<
class Iterator,
class IteratorAdapter>
452inline bool Enumerator<T>::StlAdapter<Iterator, IteratorAdapter>::MoveNext()
456 this->firstMove =
false;
457 return this->begin != this->end;
460 return ++this->begin != this->end;
464template<
class Iterator,
class IteratorAdapter>
465inline T Enumerator<T>::StlAdapter<Iterator, IteratorAdapter>::GetCurrent()
467 return this->iteratorAdapter(this->begin);
473template<
class Predicate>
474inline Enumerator<T>::FilterEnumerator<Predicate>::FilterEnumerator(
475 typename IEnumerator<T>::Ptr source, Predicate predicate)
476 : source(
std::move(source)), predicate(
std::move(predicate))
482template<
class Predicate>
483inline bool Enumerator<T>::FilterEnumerator<Predicate>::MoveNext()
485 while (this->source->MoveNext())
487 if (this->predicate(this->source->GetCurrent()))
497template<
class Predicate>
498inline T Enumerator<T>::FilterEnumerator<Predicate>::GetCurrent()
500 return this->source->GetCurrent();
506template<
class SourceEnumerator,
class TransformOperation>
507Enumerator<T>::TransformEnumerator<SourceEnumerator, TransformOperation>::TransformEnumerator(
508 std::shared_ptr<SourceEnumerator> source, TransformOperation transformOp)
509 : source(
std::move(source)), transformOp(
std::move(transformOp))
515template<
class SourceEnumerator,
class TransformOperation>
516bool Enumerator<T>::TransformEnumerator<SourceEnumerator, TransformOperation>::MoveNext()
518 return this->source->MoveNext();
523template<
class SourceEnumerator,
class TransformOperation>
524T Enumerator<T>::TransformEnumerator<SourceEnumerator, TransformOperation>::GetCurrent()
526 return this->transformOp(this->source->GetCurrent());
533inline T Enumerator<T>::GetCurrent()
535 return this->current;
539inline typename IEnumerator<T>::Ptr Enumerator<T>::CreateEmpty()
541 return std::make_shared<Empty>();
545inline typename IEnumerator<T>::Ptr Enumerator<T>::CreateComposite(
typename IEnumerator<T>::Ptr first,
typename IEnumerator<T>::Ptr second)
547 return std::make_shared<Composite>(first, second);
551template<
class TIterator>
552inline typename IEnumerator<T>::Ptr Enumerator<T>::CreateStlAdapter(TIterator begin, TIterator end)
554 return std::make_shared<StlAdapter<TIterator>>(begin, end);
558template<
class TContainer>
559inline typename IEnumerator<T>::Ptr Enumerator<T>::CreateStlAdapter(TContainer& c)
561 return CreateStlAdapter(c.begin(), c.end());
565template<
class TContainer>
566inline typename IEnumerator<T>::Ptr Enumerator<T>::CreateKeysAdapter(TContainer& c)
568 return std::make_shared<StlAdapter<typename TContainer::iterator, typename IteratorAdapters<typename TContainer::iterator>::ConstKey>>(c.begin(), c.end());
572template<
class TContainer>
573inline typename IEnumerator<T>::Ptr Enumerator<T>::CreateKeysAdapter(
const TContainer& c)
575 return std::make_shared<StlAdapter<typename TContainer::const_iterator, typename IteratorAdapters<typename TContainer::const_iterator>::ConstKey>>(c.begin(), c.end());
579template<
class TContainer>
580inline typename IEnumerator<T>::Ptr Enumerator<T>::CreateMappedAdapter(TContainer& c)
582 return std::make_shared<StlAdapter<typename TContainer::iterator, typename IteratorAdapters<typename TContainer::iterator>::Mapped>>(c.begin(), c.end());
586template<
class TContainer>
587inline typename IEnumerator<T>::Ptr Enumerator<T>::CreateMappedAdapter(
const TContainer& c)
589 return std::make_shared<StlAdapter<typename TContainer::const_iterator, typename IteratorAdapters<typename TContainer::const_iterator>::ConstMapped>>(c.begin(), c.end());
593template<
class Predicate>
594inline typename IEnumerator<T>::Ptr Enumerator<T>::CreateFilter(
595 typename IEnumerator<T>::Ptr source, Predicate predicate)
597 return std::make_shared<Enumerator<T>::FilterEnumerator<Predicate>>(std::move(source), predicate);
601template<
class SourceEnumerator,
class TransformOperation>
602inline typename IEnumerator<T>::Ptr Enumerator<T>::CreateTransform(std::shared_ptr<SourceEnumerator> source, TransformOperation transformOp)
604 return std::make_shared<Enumerator<T>::TransformEnumerator<SourceEnumerator, TransformOperation>>(
605 std::move(source), transformOp);
611typename Enumerator<T>::Empty Enumerator<T>::Null;
bool MoveNext(void) override
Moves this enumerator to the next position.
Definition: Enumerator.Composite.hxx:68
T GetCurrent(void) override
Gets the element at the current position.
Definition: Enumerator.Composite.hxx:93
Composite(typename IEnumerator< T >::Ptr first, typename IEnumerator< T >::Ptr second)
Constructs an Enumerator<T>::Composite instance.
Definition: Enumerator.Composite.hxx:60
~Composite(void) override
Destructs this instance and frees all resources.
bool MoveNext(void) override
Moves this enumerator to the next position.
Definition: Enumerator.Empty.hxx:60
~Empty(void) override
Destructs this instance and frees all resources.
T GetCurrent(void) override
Gets the element at the current position.
Definition: Enumerator.Empty.hxx:67
Empty(void)
Constructs an Enumerator<T>::Empty instance.
void Push(typename IEnumerator< T >::Ptr enumerator)
Adds the as argument passed enumerator to this stack.
Definition: Enumerator.StackComposite.hxx:100
T GetCurrent(void) override
Gets the element at the current position.
Definition: Enumerator.StackComposite.hxx:88
StackComposite(void)
Constructs an StackComposite instance.
size_t GetSize(void) const
Get the size of this stack.
Definition: Enumerator.StackComposite.hxx:112
bool MoveNext(void) override
Moves this enumerator to the next position.
Definition: Enumerator.StackComposite.hxx:71
~StackComposite(void) override
Destructs this instance and frees all resources.
T GetCurrent(void) override
Gets the element at the current position.
Definition: Enumerator.StlAdapter.hxx:98
StlAdapter(Iterator begin, Iterator end)
Constructs an StlAdapter instance.
Definition: Enumerator.StlAdapter.hxx:69
~StlAdapter(void) override
Destructs this instance and frees all resources.
bool MoveNext(void) override
Moves this enumerator to the next position.
Definition: Enumerator.StlAdapter.hxx:78
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 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.Composite.hxx:122
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
TCurrent current
The current field of this enumerator.
Definition: Enumerator.hxx:86
static IEnumerator< T >::Ptr CreateEmpty(void)
Creates an empty enumerator.
Definition: Enumerator.Empty.hxx:77
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.Filter.hxx:104
Enumerator(void)
Constructs a default Enumerator instance.
~Enumerator(void) override
Destructs this instance and frees all resources.
T GetCurrent(void) override
Gets the element at the current position.
Definition: Enumerator.hxx:113
Declares the interface of the enumerator pattern, which is leaned on .NET enumerator idiom.
Definition: IEnumerator.hxx:49
IEnumerator(void)=default
Constructs an IEnumerator instance.
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
@ Empty
No sink assigned to session yet.
Root namespace for the PLCnext API
Namespace of the C++ standard library