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