ScummVM API documentation
dgStack.h
1 /* Copyright (c) <2003-2011> <Julio Jerez, Newton Game Dynamics>
2 *
3 * This software is provided 'as-is', without any express or implied
4 * warranty. In no event will the authors be held liable for any damages
5 * arising from the use of this software.
6 *
7 * Permission is granted to anyone to use this software for any purpose,
8 * including commercial applications, and to alter it and redistribute it
9 * freely, subject to the following restrictions:
10 *
11 * 1. The origin of this software must not be misrepresented; you must not
12 * claim that you wrote the original software. If you use this software
13 * in a product, an acknowledgment in the product documentation would be
14 * appreciated but is not required.
15 *
16 * 2. Altered source versions must be plainly marked as such, and must not be
17 * misrepresented as being the original software.
18 *
19 * 3. This notice may not be removed or altered from any source distribution.
20 */
21 
22 #ifndef __dgStack__
23 #define __dgStack__
24 
25 #include "dgStdafx.h"
26 #include "dgDebug.h"
27 #include "dgMemory.h"
28 
29 class dgStackBase {
30 protected:
31  dgStackBase(dgInt32 size);
32  ~dgStackBase();
33 
34  void *m_ptr;
35 };
36 
37 inline dgStackBase::dgStackBase(dgInt32 size) {
38  m_ptr = dgMallocStack(size_t (size));
39 }
40 
41 inline dgStackBase::~dgStackBase() {
42  dgFreeStack(m_ptr);
43 }
44 
45 
46 
47 
48 template<class T>
49 class dgStack: public dgStackBase {
50 public:
51  dgStack(dgInt32 size);
52  ~dgStack();
53  dgInt32 GetSizeInBytes() const;
54  dgInt32 GetElementsCount() const;
55 
56  T &operator[](dgInt32 entry);
57  const T &operator[](dgInt32 entry) const;
58 
59 private:
60  dgInt32 m_size;
61 };
62 
63 template<class T>
64 dgStack<T>::dgStack(dgInt32 size)
65  : dgStackBase(dgInt32(size * sizeof(T))) {
66  m_size = size;
67 }
68 
69 template<class T>
71 }
72 
73 template<class T>
74 dgInt32 dgStack<T>::GetElementsCount() const {
75  return m_size;
76 }
77 
78 template<class T>
79 dgInt32 dgStack<T>::GetSizeInBytes() const {
80  return dgInt32(m_size * sizeof(T));
81 }
82 
83 
84 template<class T>
85 T &dgStack<T>::operator[](dgInt32 entry) {
86  T *mem;
87 
88  NEWTON_ASSERT(entry >= 0);
89  NEWTON_ASSERT((entry < m_size) || ((m_size == 0) && (entry == 0)));
90 
91  mem = (T *) m_ptr;
92  return mem[entry];
93 }
94 
95 template<class T>
96 const T &dgStack<T>::operator[](dgInt32 entry) const {
97  T *mem;
98 
99  NEWTON_ASSERT(0);
100  NEWTON_ASSERT(entry >= 0);
101  NEWTON_ASSERT((entry < m_size) || ((m_size == 0) && (entry == 0)));
102 
103  mem = (T *) m_ptr;
104  return mem[entry];
105 }
106 
107 
108 #endif
Definition: dgStack.h:49
Definition: dgStack.h:29