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