8 #include "Arp/System/Core/IEnumerator.hxx" 9 #include "Arp/System/Core/Exception.hpp" 30 Empty(
void) =
default;
39 virtual ~Empty(
void) =
default;
85 using Stack = std::stack<typename IEnumerator<T>::Ptr>;
112 size_t GetSize()
const;
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();
434 template<
class Iterator,
class IteratorAdapter>
443 template<
class Iterator,
class IteratorAdapter>
448 this->firstMove =
false;
449 return this->begin != this->end;
452 return ++this->begin != this->end;
456 template<
class Iterator,
class IteratorAdapter>
459 return this->iteratorAdapter(this->begin);
465 template<
class Predicate>
468 : source(std::move(source)), predicate(std::move(predicate))
474 template<
class Predicate>
479 if (this->predicate(this->source->
GetCurrent()))
489 template<
class Predicate>
498 template<
class SourceEnumerator,
class TransformOperation>
500 std::shared_ptr<SourceEnumerator> source, TransformOperation transformOp)
501 : source(std::move(source)), transformOp(std::move(transformOp))
507 template<
class SourceEnumerator,
class TransformOperation>
510 return this->source->MoveNext();
515 template<
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);
543 template<
class TIterator>
546 return std::make_shared<StlAdapter<TIterator>>(begin, end);
550 template<
class TContainer>
557 template<
class TContainer>
560 return std::make_shared<StlAdapter<typename TContainer::iterator, typename IteratorAdapters<typename TContainer::iterator>::ConstKey>>(c.begin(), c.end());
564 template<
class TContainer>
567 return std::make_shared<StlAdapter<typename TContainer::const_iterator, typename IteratorAdapters<typename TContainer::const_iterator>::ConstKey>>(c.begin(), c.end());
571 template<
class TContainer>
574 return std::make_shared<StlAdapter<typename TContainer::iterator, typename IteratorAdapters<typename TContainer::iterator>::Mapped>>(c.begin(), c.end());
578 template<
class TContainer>
581 return std::make_shared<StlAdapter<typename TContainer::const_iterator, typename IteratorAdapters<typename TContainer::const_iterator>::ConstMapped>>(c.begin(), c.end());
585 template<
class Predicate>
589 return std::make_shared<Enumerator<T>::FilterEnumerator<Predicate>>(source, predicate);
593 template<
class SourceEnumerator,
class TransformOperation>
596 return std::make_shared<Enumerator<T>::TransformEnumerator<SourceEnumerator, TransformOperation>>(
597 source, transformOp);
Declares the interface of the enumerator pattern, which is leaned on .NET enumerator idiom...
Definition: IEnumerator.hxx:47
static IEnumerator< T >::Ptr CreateStlAdapter(TIterator begin, TIterator end)
Creates an enumerator adapter from the given STL iterators.
Definition: Enumerator.hxx:544
std::shared_ptr< IEnumerator > Ptr
The smart pointer tpye of this interface.
Definition: IEnumerator.hxx:51
Enumerator(void)=default
Constructs an Enumerator instance.
virtual bool MoveNext(void)=0
Moves this enumerator to the next position.
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
Composite(typename IEnumerator< T >::Ptr first, typename IEnumerator< T >::Ptr second)
Constructs an Composite instance.
Definition: Enumerator.hxx:363
Implements an empty enumerator, that is, the first call of MoveNext() will return false...
Definition: Enumerator.hxx:26
size_t GetSize() const
Get the size of this stack.
Definition: Enumerator.hxx:426
static IEnumerator< T >::Ptr CreateMappedAdapter(TContainer &c)
Creates an enumerator adapter from the given STL map enumerating the mapped items.
Definition: Enumerator.hxx:572
Use this class to build a single enumerator by two given enumerator, e.g. to enumerate multiple conta...
Definition: Enumerator.hxx:82
T GetCurrent(void) override
Gets the element at the current position.
Definition: Enumerator.hxx:385
void Push(typename IEnumerator< T >::Ptr e)
Adds the as argument passed enumerator to this stack.
Definition: Enumerator.hxx:398
Empty(void)=default
Constructs an Empty instance.
static IEnumerator< T >::Ptr CreateEmpty(void)
Creates an empty enumerator.
Definition: Enumerator.hxx:531
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
static Empty Null
A static empty enumerator instance.
Definition: Enumerator.hxx:252
Root namespace for the PLCnext API
Empty & operator=(const Empty &arg)=default
Assignment operator.
virtual T GetCurrent(void) override
Gets the element at the current position.
Definition: Enumerator.hxx:525
This class implements an enumerator adapter for STL container based on iterators.
Definition: Enumerator.hxx:160
virtual ~Enumerator(void)=default
Destructs this instance and frees all resources.
bool MoveNext(void) override
Moves this enumerator to the next position.
Definition: Enumerator.hxx:370
virtual ~Empty(void)=default
Destructs this instance and frees all resources.
virtual bool MoveNext(void)=0
Moves this enumerator to the next position.
This is the base class of all Arp exception classes.
Definition: Exception.hpp:15
T GetCurrent(void) override
Gets the element at the current position.
Definition: Enumerator.hxx:420
This class defines a base class for all enumerator implementations and some predefined enumerators as...
Definition: Enumerator.hxx:21
static IEnumerator< T >::Ptr CreateKeysAdapter(TContainer &c)
Creates an enumerator adapter from the given STL map enumerating the keys.
Definition: Enumerator.hxx:558
bool MoveNext(void) override
Moves this enumerator to the next position.
Definition: Enumerator.hxx:404
virtual T GetCurrent(void)=0
Gets the element at the current position.
Use this class to build a single enumerator by two given enumerator, e.g. to enumerate multiple conta...
Definition: Enumerator.hxx:52
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
bool MoveNext(void) override
Moves this enumerator to the next position.
Definition: Enumerator.hxx:349
TCurrent current
The current field of this enumerator.
Definition: Enumerator.hxx:343
T GetCurrent(void) override
Gets the element at the current position.
Definition: Enumerator.hxx:355