Newton Dynamics  4.00
ndListView.h
1 /* Copyright (c) <2003-2021> <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 __ND_LIST_VIEW_H__
23 #define __ND_LIST_VIEW_H__
24 
25 #include "ndCollisionStdafx.h"
26 
27 class ndBodyKinematic;
28 
29 template<class T>
30 class ndListView : public ndList<T*, ndContainersFreeListAlloc<T*>>
31 {
32  public:
33  ndListView();
34  ndListView(const ndListView& src);
35 
36  typename ndListView<T>::ndNode* AddItem(T* const item);
37  void RemoveItem(typename ndListView<T>::ndNode* const node);
38 
39  bool UpdateView();
40  const bool IsListDirty() const;
41 
42  ndArray<T*>& GetView();
43  const ndArray<T*>& GetView() const;
44 
45  protected:
46  ndArray<T*> m_view;
47  ndUnsigned8 m_listIsDirty;
48 };
49 
50 class ndBodyList: public ndListView<ndBodyKinematic>
51 {
52  public:
53  ndBodyList()
55  {
56  }
57 
58  ndBodyList(const ndBodyList& src)
60  {
61  }
62 };
63 
64 template<class T>
67  ,m_view(1024)
68  ,m_listIsDirty(1)
69 {
70 }
71 
72 template<class T>
75  ,m_view()
76  ,m_listIsDirty(1)
77 {
78  typename ndListView<T>::ndNode* nextNode;
79  ndListView* const stealData = (ndListView*)&src;
80  for (typename ndListView<T>::ndNode* node = stealData->GetFirst(); node; node = nextNode)
81  {
82  nextNode = node->GetNext();
83  stealData->Unlink(node);
85  }
86  m_view.Swap(stealData->m_view);
87 }
88 
89 template<class T>
91 {
92  return m_view;
93 }
94 
95 template<class T>
97 {
98  return m_view;
99 }
100 
101 template<class T>
102 typename ndListView<T>::ndNode* ndListView<T>::AddItem(T* const item)
103 {
104  m_listIsDirty = 1;
105  return ndListView<T>::Append(item);
106 }
107 
108 template<class T>
109 void ndListView<T>::RemoveItem(typename ndListView<T>::ndNode* const node)
110 {
111  m_listIsDirty = 1;
112  ndListView<T>::Remove(node);
113 }
114 
115 template<class T>
116 const bool ndListView<T>::IsListDirty() const
117 {
118  return m_listIsDirty ? true : false;
119 }
120 
121 template<class T>
123 {
124  bool ret = false;
125  if (m_listIsDirty)
126  {
127  D_TRACKTIME();
128  ret = true;
129  m_listIsDirty = 0;
130  m_view.SetCount(ndListView<T>::GetCount());
131  ndInt32 index = 0;
132  for (typename ndListView<T>::ndNode* node = ndListView<T>::GetFirst(); node; node = node->GetNext())
133  {
134  m_view[index] = node->GetInfo();
135  index++;
136  }
137  }
138  return ret;
139 }
140 
141 #endif
ndArray< T * >
ndArray::Swap
void Swap(ndArray &other)
Interchange all the information with other.
Definition: ndArray.h:257
ndBodyList
Definition: ndListView.h:51
ndBodyKinematic
Definition: ndBodyKinematic.h:40
ndList
Definition: ndList.h:33
ndContainersFreeListAlloc
Definition: ndContainersAlloc.h:60
ndListView
Definition: ndListView.h:31