My Project: dynamics.mod/NewtonDynamics/source/core/dgStack.h Source File

My Project

dgStack.h
Go to the documentation of this file.
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 {
31 protected:
32 dgStackBase (dgInt32 size);
33 ~dgStackBase ();
34
35 const void *m_ptr;
36 };
37
38 inline dgStackBase::dgStackBase (dgInt32 size)
39 {
40 m_ptr = dgMallocStack (size_t (size));
41 }
42
43 inline dgStackBase::~dgStackBase ()
44 {
45 dgFreeStack ((void*)m_ptr);
46 }
47
48
49
50
51 template<class T>
52 class dgStack: public dgStackBase
53 {
54 public:
55 dgStack (dgInt32 size);
56 ~dgStack ();
57 dgInt32 GetSizeInBytes() const;
58 dgInt32 GetElementsCount() const;
59
60 T& operator[] (dgInt32 entry);
61 const T& operator[] (dgInt32 entry) const;
62
63 private:
64 dgInt32 m_size;
65 };
66
67 template<class T>
68 dgStack<T>::dgStack (dgInt32 size)
69 :dgStackBase (dgInt32 (size * sizeof(T)))
70 {
71 m_size = size;
72 }
73
74 template<class T>
75 dgStack<T>::~dgStack ()
76 {
77 }
78
79 template<class T>
80 dgInt32 dgStack<T>::GetElementsCount() const
81 {
82 return m_size;
83 }
84
85 template<class T>
86 dgInt32 dgStack<T>::GetSizeInBytes() const
87 {
88 return dgInt32 (m_size * sizeof(T));
89 }
90
91
92 template<class T>
93 T& dgStack<T>::operator[] (dgInt32 entry)
94 {
95 T *mem;
96
97 dgAssert (entry >= 0);
98 dgAssert ((entry < m_size) || ((m_size == 0) && (entry == 0)));
99
100 mem = (T*) m_ptr;
101 return mem[entry];
102 }
103
104 template<class T>
105 const T& dgStack<T>::operator[] (dgInt32 entry) const
106 {
107 T *mem;
108
109 dgAssert (0);
110 dgAssert (entry >= 0);
111 dgAssert ((entry < m_size) || ((m_size == 0) && (entry == 0)));
112
113 mem = (T*) m_ptr;
114 return mem[entry];
115 }
116
117
118 #endif
119
dgFreeStack
void dgApi dgFreeStack(void *const ptr)
Definition: dgMemory.cpp:430
dgMemory.h
dgStack::dgStack
dgStack(dgInt32 size)
Definition: dgStack.h:68
dgDebug.h
dgStack::operator[]
T & operator[](dgInt32 entry)
Definition: dgStack.h:93
dgAssert
#define dgAssert(x)
Definition: dgTypes.h:158
dgInt32
int dgInt32
Definition: dgTypes.h:218
dgStack::GetSizeInBytes
dgInt32 GetSizeInBytes() const
Definition: dgStack.h:86
dgStackBase::dgStackBase
dgStackBase(dgInt32 size)
Definition: dgStack.h:38
entry
void * entry(void *arg)
Definition: self2.c:53
dgStack::~dgStack
~dgStack()
Definition: dgStack.h:75
dgStdafx.h
dgStackBase::m_ptr
const void * m_ptr
Definition: dgStack.h:35
dgStack::GetElementsCount
dgInt32 GetElementsCount() const
Definition: dgStack.h:80
dgStack
Definition: dgStack.h:52
dgStackBase::~dgStackBase
~dgStackBase()
Definition: dgStack.h:43
dgMallocStack
void *dgApi dgMallocStack(size_t size)
Definition: dgMemory.cpp:409
dgStackBase
Definition: dgStack.h:29