PLCnext API Documentation 25.0.2.69
Enumerator.Composite.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"
10namespace Arp { namespace Base { namespace Core
11{
12
18template<class T>
19class Enumerator<T>::Composite : public IEnumerator<T>
20{
21public: // construction/destruction
22 Composite(typename IEnumerator<T>::Ptr first, typename IEnumerator<T>::Ptr second);
23 Composite(const Composite& arg) = delete;
24 Composite(Composite&& arg)noexcept;
25 Composite& operator=(const Composite& arg) = delete;
26 Composite& operator=(Composite&& arg)noexcept;
27 ~Composite(void)override;
28
29public: // overridden operations
30 bool MoveNext(void)override;
31 T GetCurrent(void)override;
32
33private: // fields
34 typename IEnumerator<T>::Ptr first;
35 typename IEnumerator<T>::Ptr second;
36};
37
39// inline methods of class Enumerator<T>::Composite
40
43template<class T>
44inline Enumerator<T>::Composite::Composite(Composite&& arg)noexcept = default;
45
49template<class T>
50inline typename Enumerator<T>::Composite& Enumerator<T>::Composite::operator=(Composite&& arg)noexcept = default;
51
53template<class T>
55
59template<class T>
61 : first(std::move(first))
62 , second(std::move(second))
63{
64}
65
66// inherited doc
67template<class T>
69{
70 if (this->first)
71 {
72 if (this->first->MoveNext())
73 {
74 return true;
75 }
76 // else
77 this->first.reset();
78 }
79 if (this->second)
80 {
81 if (this->second->MoveNext())
82 {
83 return true;
84 }
85 // else
86 this->second.reset();
87 }
88 return false;
89}
90
91// inherited doc
92template<class T>
94{
95 if (this->first)
96 {
97 return this->first->GetCurrent();
98 }
99 // else
100 if (this->second)
101 {
102 return this->second->GetCurrent();
103 }
104 // else
105 throw Exception("Enumerator<T>::Composite::GetCurrent() fails: Composite enumerator is empty or enumerating has ended yet.");
106}
107
108// Implementation of factory operation of class Enumerator<T>
109
121template<class T>
123{
124 return std::make_shared<Composite>(first, second);
125}
126
127}}} // end of namespace Arp::Base::Core
Use this class to build a single enumerator by two given enumerator, e.g. to enumerate multiple conta...
Definition: Enumerator.Composite.hxx:20
Composite & operator=(Composite &&arg) noexcept
Move assignment operator.
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(Composite &&arg) noexcept
Move constructor.
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.
This class defines a base class for all enumerator implementations and some predefined enumerators as...
Definition: Enumerator.hxx:24
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
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