Newton Dynamics  4.00
dStack.h
1 /* Copyright (c) <2003-2019> <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 __dStack__
23 #define __dStack__
24 
25 #include "dCoreStdafx.h"
26 #include "dTypes.h"
27 #include "dDebug.h"
28 #include "dMemory.h"
29 
31 {
32  protected:
33  dStackBase (size_t size);
34  ~dStackBase ();
35 
36  const void *m_ptr;
37 };
38 
39 inline dStackBase::dStackBase (size_t size)
40  :m_ptr (dMemory::Malloc (size_t (size)))
41 {
42 }
43 
44 inline dStackBase::~dStackBase ()
45 {
46  dMemory::Free ((void*)m_ptr);
47 }
48 
49 template<class T>
50 class dStack: public dStackBase
51 {
52  public:
53  dStack (size_t size);
54  ~dStack ();
55  dInt32 GetSizeInBytes() const;
56  dInt32 GetElementsCount() const;
57 
58  D_INLINE T& operator[] (dInt32 entry);
59  D_INLINE const T& operator[] (dInt32 entry) const;
60 
61  private:
62  size_t m_size;
63 };
64 
65 template<class T>
66 dStack<T>::dStack (size_t size)
67  :dStackBase (size * sizeof(T))
68  ,m_size(size)
69 {
70 }
71 
72 template<class T>
74 {
75 }
76 
77 template<class T>
78 dInt32 dStack<T>::GetElementsCount() const
79 {
80  return dInt32 (m_size);
81 }
82 
83 template<class T>
84 dInt32 dStack<T>::GetSizeInBytes() const
85 {
86  return dInt32 (m_size * sizeof(T));
87 }
88 
89 template<class T>
90 D_INLINE T& dStack<T>::operator[] (dInt32 entry)
91 {
92  dAssert (entry >= 0);
93  dAssert ((size_t(entry) < m_size) || ((m_size == 0) && (entry == 0)));
94 
95  T* const mem = (T*) m_ptr;
96  return mem[entry];
97 }
98 
99 template<class T>
100 D_INLINE const T& dStack<T>::operator[] (dInt32 entry) const
101 {
102  dAssert (entry >= 0);
103  dAssert ((entry < m_size) || ((m_size == 0) && (entry == 0)));
104 
105  const T* const mem = (T*) m_ptr;
106  return mem[entry];
107 }
108 
109 #endif
110 
dStackBase
Definition: dStack.h:31
dMemory::Free
static D_CORE_API void Free(void *const ptr)
Destroy a memory buffer previously allocated by Malloc.
Definition: dMemory.cpp:58
dStack
Definition: dStack.h:51
dMemory
Definition: dMemory.h:31