PLCnext API Documentation 25.0.2.69
Enumerator.StackComposite.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#include <stack>
11
12namespace Arp { namespace Base { namespace Core
13{
14
21template<class T>
23{
24private: // usings
25 using Stack = std::stack<typename IEnumerator<T>::Ptr>;
26
27public: // construction/destruction
29 StackComposite(const StackComposite& arg) = delete;
31 StackComposite& operator=(const StackComposite& arg) = delete;
33 ~StackComposite(void)override;
34
35public: // overridden operations
36 bool MoveNext(void)override;
37 T GetCurrent(void)override;
38
39public: // operations
40 void Push(typename IEnumerator<T>::Ptr enumerator);
41 size_t GetSize(void)const;
42
43private: // fields
44 Stack enumerators;
45};
46
48// inline methods of class Enumerator<T>::StackComposite
49
51template<class T>
53
56template<class T>
57inline Enumerator<T>::StackComposite::StackComposite(StackComposite&& arg)noexcept = default;
58
62template<class T>
63inline typename Enumerator<T>::StackComposite& Enumerator<T>::StackComposite::operator=(StackComposite&& arg) noexcept = default;
64
66template<class T>
68
69// inherited doc
70template<class T>
72{
73 while (!this->enumerators.empty())
74 {
75 if (this->enumerators.top()->MoveNext())
76 {
77 return true;
78 }
79 // else must pop last enumerator
80 this->enumerators.pop();
81 }
82 // reaching this point means stack is empty
83 return false;
84}
85
86// inherited doc
87template<class T>
89{
90 if (this->enumerators.empty())
91 {
92 throw Exception("Enumerator<T>::StackComposite::GetCurrent(): StackComposite enumerators is empty or enumerating has ended yet");
93 }
94 return this->enumerators.top()->GetCurrent();
95}
96
99template<class T>
101{
102 if (!enumerator)
103 {
104 return;
105 }
106 this->enumerators.emplace(std::move(enumerator));
107}
108
111template<class T>
113{
114 return this->enumerators.size();
115}
116
117}}} // end of namespace Arp::Base::Core
Use this class to build a single enumerator by multiple other enumerators using a stack
Definition: Enumerator.StackComposite.hxx:23
void Push(typename IEnumerator< T >::Ptr enumerator)
Adds the as argument passed enumerator to this stack.
Definition: Enumerator.StackComposite.hxx:100
StackComposite & operator=(StackComposite &&arg) noexcept
Move assignment operator.
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(StackComposite &&arg) noexcept
Move constructor.
~StackComposite(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
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