VR-Forces Developer's Guide
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
DtSTLUtilities.h
Go to the documentation of this file.
1 /******************************************************************************
2 ** Copyright (c) 2025 MAK Technologies, Inc.
3 ** All rights reserved.
4 ******************************************************************************/
5 
9 #pragma once
10 
11 #include <boost/unordered_map.hpp>
12 #include <boost/unordered_set.hpp>
13 
14 #include <utility>
15 #include <boost/bind/bind.hpp>
16 #include <map>
17 #include <vector>
18 #include <list>
19 
20 namespace makVrv
21 {
22  template<typename P>
24  {
25  const typename P::first_type&
26  operator()(const P& p) const
27  {
28  return p.first;
29  }
30  };
31 
32  template<typename P>
34  {
35  const typename P::second_type&
36  operator()(const P& p) const
37  {
38  return p.second;
39  }
40  };
41 
42  template <class T> struct DeletePtr
43  {
44  void operator()(T* obj)
45  {
46  delete obj;
47  }
48  };
49 
50  template <class T>
51  void verify_index_for_container(size_t index, const T& t)
52  {
53 #ifndef _DEBUG
54  static_cast<void>(t);
55  static_cast<void>(index);
56 #endif
57  Assert((index==0&&t.size()==1)
58  || (t.size()>1 && index<=t.size()-1));
59  }
60 
61  template <class C, class T> void destroy_from_container(C& _container, T* pT)
62  {
63  typename C::iterator it = std::find(_container.begin(), _container.end(), pT);
64  if (it!=_container.end())
65  {
66  delete pT;pT=0;
67  _container.erase(it);
68  }
69  }
70 
71  template <class C, class T> void remove_from_container(C& _container, T* pT)
72  {
73  typename C::iterator it = std::find(_container.begin(), _container.end(), pT);
74  if (it!=_container.end())
75  {
76  _container.erase(it);
77  }
78  }
79 
80  template <class K, class T> T* get_from_map(std::map<K, T*>& _map, const K& key)
81  {
82  typename std::map<K, T*>::const_iterator it = _map.find(key);
83  if (it==_map.end())
84  return 0;
85  return it->second;
86  }
87 
88  template <class K, class T> void destroy_from_map(std::map<K, T*>& _map, const K& key)
89  {
90  typename std::map<K, T*>::iterator it = _map.find(key);
91  Assert(it!=_map.end());
92 
93  T* pT= it->second;
94 
95  _map.erase(key);
96 
97  delete pT; pT = 0;
98  }
99 
100  template <class T> void destroy_all_from_vector(std::vector<T*>& _vector)
101  {
102  while(_vector.empty()==false)
103  {
104  makVrv::destroy_from_container(_vector, *_vector.begin());
105  }
106  };
107 
108  template <class T> void destroy_all_from_list(std::list<T*>& _list)
109  {
110  while(_list.empty()==false)
111  {
112  makVrv::destroy_from_container(_list, *_list.begin());
113  }
114  }
115 
116  template <class T> void destroy_all_from_unordered_set(boost::unordered_set<T*>& _set)
117  {
118  while(_set.empty()==false)
119  {
120  makVrv::destroy_from_container(_set, *_set.begin());
121  }
122  }
123 
124  template <class K, class T> void destroy_all_from_map(std::map<K, T*>& _map)
125  {
126  while(_map.empty()==false)
127  {
128  typename std::map<K, T*>::iterator it = _map.begin();
129 
130  T* pT = it->second;
131  delete pT; pT = 0;
132 
133  _map.erase(it);
134  }
135  _map.clear();
136  }
137 
138  template <class T> T* get_nth_item(boost::unordered_set<T*>& _set, size_t index)
139  {
140  if (_set.empty())
141  return 0;
142 
144 
145  typename std::list<T*>::const_iterator it = _set.begin();
146  std::advance(it, index);
147  return *it;
148  }
149 
150  template <class T> T* get_nth_item(std::list<T*>& _list, size_t index)
151  {
152  if (_list.empty())
153  return 0;
154 
156 
157  typename std::list<T*>::const_iterator it = _list.begin();
158  std::advance(it, index);
159  return *it;
160  }
161 
162  template <class T> const T* get_nth_item(const std::list<T*>& _list, size_t index)
163  {
164  if (_list.empty())
165  return 0;
166 
168 
169  typename std::list<T*>::const_iterator it = _list.begin();
170  std::advance(it, index);
171  return *it;
172  }
173 
174  template <class T> T* get_nth_item(std::vector<T*>& _vector, size_t index)
175  {
176  if (_vector.empty())
177  return 0;
178 
179  makVrv::verify_index_for_container(index, _vector);
180 
181  return _vector[index];
182  }
183 
184  template <class T> T* get_nth_item(const std::vector<T*>& _vector, size_t index)
185  {
186  if (_vector.empty())
187  return 0;
188 
189  makVrv::verify_index_for_container(index, _vector);
190 
191  return _vector[index];
192  }
193 
194  template <class K, class T> T* get_nth_item(std::map<K, T*>& _map, size_t index)
195  {
196  if (_map.empty())
197  return 0;
198 
200 
201  typename std::map<K, T*>::const_iterator it = _map.begin();
202  std::advance(it, index);
203  return it->second;
204  }
205 
206  template <class K, class T> T* get_nth_item(const std::map<K, T*>& _map, size_t index)
207  {
208  if (_map.empty())
209  return 0;
210 
212 
213  typename std::map<K, T*>::const_iterator it = _map.begin();
214  std::advance(it, index);
215  return it->second;
216  }
217 }
Definition: DtSTLUtilities.h:23
const P::first_type & operator()(const P &p) const
Definition: DtSTLUtilities.h:26
const P::second_type & operator()(const P &p) const
Definition: DtSTLUtilities.h:36
T * get_nth_item(boost::unordered_set< T * > &_set, size_t index)
Definition: DtSTLUtilities.h:138
void verify_index_for_container(size_t index, const T &t)
Definition: DtSTLUtilities.h:51
void destroy_all_from_vector(std::vector< T * > &_vector)
Definition: DtSTLUtilities.h:100
void destroy_from_map(std::map< K, T * > &_map, const K &key)
Definition: DtSTLUtilities.h:88
void remove_from_container(C &_container, T *pT)
Definition: DtSTLUtilities.h:71
void destroy_from_container(C &_container, T *pT)
Definition: DtSTLUtilities.h:61
T * get_from_map(std::map< K, T * > &_map, const K &key)
Definition: DtSTLUtilities.h:80
void destroy_all_from_unordered_set(boost::unordered_set< T * > &_set)
Definition: DtSTLUtilities.h:116
Definition: DtSTLUtilities.h:33
Definition: DtSTLUtilities.h:42
void destroy_all_from_map(std::map< K, T * > &_map)
Definition: DtSTLUtilities.h:124
void operator()(T *obj)
Definition: DtSTLUtilities.h:44
void destroy_all_from_list(std::list< T * > &_list)
Definition: DtSTLUtilities.h:108
#define Assert(exp)
Definition: DtAssert.h:25

Document ID: Generated on Thu Oct 23 22:29:17 EDT 2025 from SVN revision 280951
Copyright © 2005-2024 MAK Technologies. All Rights Reserved (www.mak.com)