PLCnext API Documentation 25.0.2.69
Enumerator.Filter.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
11namespace Arp { namespace Base { namespace Core
12{
13
16template<class T>
17template<class Predicate>
18class Enumerator<T>::Filter : public IEnumerator<T>
19{
20public: // construction/destruction
21 Filter(typename IEnumerator<T>::Ptr source, Predicate filter);
22 Filter(const Filter& arg) = delete;
23 Filter(Filter&& arg)noexcept;
24 Filter& operator=(const Filter& arg) = delete;
25 Filter& operator=(Filter&& arg)noexcept;
26 ~Filter(void)override;
27
28public: // overridden operations
29 bool MoveNext(void)override;
30 T GetCurrent(void)override;
31
32private: // fields
33 typename IEnumerator<T>::Ptr source;
34 Predicate filter;
35};
36
38// inline methods of class Enumerator<T>::Filter
39
42template<class T>
43template<class Predicate>
44inline Enumerator<T>::Filter<Predicate>::Filter(Filter&& arg)noexcept = default;
45
49template<class T>
50template<class Predicate>
51inline typename Enumerator<T>::Filter<Predicate>& Enumerator<T>::Filter<Predicate>::operator=(Filter&& arg)noexcept = default;
52
54template<class T>
55template<class Predicate>
57
61template<class T>
62template<class Predicate>
63inline Enumerator<T>::Filter<Predicate>::Filter(typename IEnumerator<T>::Ptr source, Predicate filter)
64 : source(std::move(source)), filter(std::move(filter))
65{
66 if (!this->source)
67 {
68 throw Exception("Enumerator<T>::Filter<Predicate>::Filter(): Argument 'source' is null.");
69 }
70}
71
72// inherited doc
73template<class T>
74template<class Predicate>
76{
77 while (this->source->MoveNext())
78 {
79 if (this->filter(this->source->GetCurrent()))
80 {
81 return true;
82 }
83 }
84 return false;
85}
86
87// inherited doc
88template<class T>
89template<class Predicate>
91{
92 return this->source->GetCurrent();
93}
94
95// Implementation of factory operation of class Enumerator<T>
96
102template<class T>
103template<class Predicate>
104inline typename IEnumerator<T>::Ptr Enumerator<T>::CreateFilter(typename IEnumerator<T>::Ptr source, Predicate filter)
105{
106 return std::make_shared<Enumerator<T>::Filter<Predicate>>(std::move(source), std::move(filter));
107}
108
109
110}}} // end of namespace Arp::Base::Core
This class implements a filtering enumerator. It enumerates all nodes of the source enumerator matchi...
Definition: Enumerator.Filter.hxx:19
Filter(Filter &&arg) noexcept
Move constructor.
~Filter(void) override
Destructs this instance and frees all resources.
Filter & operator=(Filter &&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 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
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
Namespace of the C++ standard library