PoDoFo 1.0.0-dev
Loading...
Searching...
No Matches
StateStack.h
1
7#ifndef AUX_STATE_STACK_H
8#define AUX_STATE_STACK_H
9
10#include <stack>
11#include <stdexcept>
12
13namespace PoDoFo
14{
15 template <typename StateT>
16 class StateStack final
17 {
18 struct Accessor final
19 {
20 friend class StateStack;
21 private:
22 Accessor() : m_state(nullptr) { }
23 void Set(StateT& state) { m_state = &state; }
24 public:
25 StateT* operator->() { return m_state; }
26 const StateT* operator->() const { return m_state; }
27 StateT& operator*() { return *m_state; }
28 const StateT& operator*() const { return *m_state; }
29 private:
30 StateT* m_state;
31 };
32
33 public:
34 Accessor Current;
35 public:
36 StateStack();
37 void Push();
38 bool PopLenient(unsigned popCount = 1);
39 void Pop(unsigned popCount = 1);
40 void Clear();
41 unsigned GetSize() const;
42 private:
43 void push(const StateT& state);
44 private:
45 std::stack<StateT> m_states;
46 };
47
48 template <typename StateT>
49 StateStack<StateT>::StateStack()
50 {
51 push({ });
52 }
53
54 template <typename StateT>
55 void StateStack<StateT>::Push()
56 {
57 push(m_states.top());
58 }
59
60 template <typename StateT>
61 bool StateStack<StateT>::PopLenient(unsigned popCount)
62 {
63 if (popCount >= m_states.size())
64 return false;
65
66 for (unsigned i = 0; i < popCount; i++)
67 m_states.pop();
68
69 Current.Set(m_states.top());
70 return true;
71 }
72
73 template<typename StateT>
74 inline void StateStack<StateT>::Pop(unsigned popCount)
75 {
76 if (popCount >= m_states.size())
77 throw std::runtime_error("Can't pop out all the states in the stack");
78
79 for (unsigned i = 0; i < popCount; i++)
80 m_states.pop();
81
82 Current.Set(m_states.top());
83 }
84
85 template <typename StateT>
86 void StateStack<StateT>::Clear()
87 {
88 for (unsigned i = 0; i < m_states.size(); i++)
89 m_states.pop();
90
91 push({ });
92 }
93
94 template <typename StateT>
95 unsigned StateStack<StateT>::GetSize() const
96 {
97 return (unsigned)m_states.size();
98 }
99
100 template <typename StateT>
101 void StateStack<StateT>::push(const StateT& state)
102 {
103 m_states.push(state);
104 Current.Set(m_states.top());
105 }
106}
107
108#endif // AUX_STATE_STACK_H
SPDX-FileCopyrightText: (C) 2022 Francesco Pretto ceztko@gmail.com SPDX-License-Identifier: LGPL-2....
Definition basetypes.h:16
@ Push
Display the fields down appearance (requires an additional appearance stream to be set)