CFx SDK Documentation  2020SP3
OdStack.h
Go to the documentation of this file.
1 // Copyright (C) 2002-2017, Open Design Alliance (the "Alliance").
3 // All rights reserved.
4 //
5 // This software and its documentation and related materials are owned by
6 // the Alliance. The software may only be incorporated into application
7 // programs owned by members of the Alliance, subject to a signed
8 // Membership Agreement and Supplemental Software License Agreement with the
9 // Alliance. The structure and organization of this software are the valuable
10 // trade secrets of the Alliance and its suppliers. The software is also
11 // protected by copyright law and international treaty provisions. Application
12 // programs incorporating this software must include the following statement
13 // with their copyright notices:
14 //
15 // This application incorporates Teigha(R) software pursuant to a license
16 // agreement with Open Design Alliance.
17 // Teigha(R) Copyright (C) 2002-2017 by Open Design Alliance.
18 // All rights reserved.
19 //
20 // By use of this software, its documentation or related materials, you
21 // acknowledge and accept the above terms.
23 
24 
25 
26 
27 #ifndef _ODSTACK_H_
28 #define _ODSTACK_H_
29 
30 #include "TD_PackPush.h"
31 
32 template <class T> class OdStackItem;
33 template <class T> class OdStack;
34 
39 template <class T>
40 class OdStackItem : public T
41 {
42  friend class OdStack<T>;
43 protected:
45  inline OdStackItem(OdStackItem* pUnder, const T& val)
46  : T(val), m_pUnder(pUnder) { }
47  inline OdStackItem(OdStackItem* pUnder) : m_pUnder(pUnder) { }
48 };
49 
54 template <class T>
55 class OdStack
56 {
57  typedef OdStackItem<T> TItem;
58 public:
60  inline OdStack() : m_pTop(0) { }
61  inline void push(const T& inVal)
62  {
63  m_pTop = new TItem(m_pTop, inVal);
64  }
65  inline T* push()
66  {
67  m_pTop = new TItem(m_pTop);
68  return top();
69  }
70  inline void pop(T& outVal)
71  {
72  ODA_ASSERT(m_pTop); // pop from empty stack
73  outVal = *m_pTop;
74  pop();
75  }
76 
77  inline const T* top() const { return m_pTop; }
78  inline T* top() { return m_pTop; }
79 
80  inline void pop()
81  {
82  TItem* pTop = m_pTop;
83  ODA_ASSERT(pTop); // pop from empty stack
84  m_pTop = pTop->m_pUnder;
85  delete pTop;
86  }
87 
88  inline ~OdStack()
89  {
90  while(m_pTop)
91  {
92  pop();
93  }
94  }
95 
96  inline T* beforeTop() const
97  {
99  return m_pTop->m_pUnder;
100  }
101 
102  bool empty()const { return m_pTop == 0; }
103 
104  size_t size() const
105  {
106  size_t n = 0;
107  for ( TItem* p = m_pTop; p; p = p->m_pUnder)
108  ++n;
109  return n;
110  }
111 };
112 
113 #include "TD_PackPop.h"
114 
115 #endif //#ifndef _ODSTACK_H_
OdStack::beforeTop
T * beforeTop() const
Definition: OdStack.h:96
OdStack::push
T * push()
Definition: OdStack.h:65
TD_PackPop.h
OdStack::top
T * top()
Definition: OdStack.h:78
OdStackItem::OdStackItem
OdStackItem(OdStackItem *pUnder)
Definition: OdStack.h:47
OdStackItem::m_pUnder
OdStackItem * m_pUnder
Definition: OdStack.h:44
OdStack::empty
bool empty() const
Definition: OdStack.h:102
OdStackItem::OdStackItem
OdStackItem(OdStackItem *pUnder, const T &val)
Definition: OdStack.h:45
OdStack::~OdStack
~OdStack()
Definition: OdStack.h:88
OdStack::pop
void pop()
Definition: OdStack.h:80
TD_PackPush.h
OdStack
Definition: OdStack.h:56
OdStackItem
Definition: OdStack.h:41
OdStack::pop
void pop(T &outVal)
Definition: OdStack.h:70
ODA_ASSERT
#define ODA_ASSERT(exp)
Definition: DebugStuff.h:49
OdStack::m_pTop
TItem * m_pTop
Definition: OdStack.h:59
OdStack::top
const T * top() const
Definition: OdStack.h:77
OdStack::push
void push(const T &inVal)
Definition: OdStack.h:61
OdStack::OdStack
OdStack()
Definition: OdStack.h:60
OdStack::size
size_t size() const
Definition: OdStack.h:104