VR-Forces 5.0.3 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) 2017 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 <functional>
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 _Fn>
44  : public std::unary_function<typename _Fn::argument_type, typename _Fn::result_type>
45  { // functor adapter _Func(stored, right)
46  public:
47  typedef std::unary_function<typename _Fn::argument_type,
48  typename _Fn::result_type> _Base;
49  typedef typename _Base::argument_type argument_type;
50  typedef typename _Base::result_type result_type;
51 
52  binderNoArgs(const _Fn& _Func,
53  const typename _Fn::argument_type& _Left)
54  : op(_Func), value(_Left)
55  { // construct from functor and left operand
56  }
57 
58  result_type operator()(void) const
59  { // apply functor to operands
60  return (op(value));
61  }
62 
64  { // apply functor to operands
65  return (op(value));
66  }
67 
68  protected:
69  _Fn op; // the functor to apply
70  typename _Fn::argument_type value; // the left operand
71  };
72 
73  // TEMPLATE FUNCTION bind (based upon std::bind1st)
74  template<class _Fn,
75  class _Ty> inline
76  binderNoArgs<_Fn> bind(const _Fn& _Func, const _Ty& _Left)
77  { // return a binderNoArgs functor adapter
78  typename _Fn::argument_type _Val(_Left);
79  return (binderNoArgs<_Fn>(_Func, _Val));
80  }
81 
82 
83  template <class T> struct DeletePtr
84  {
85  void operator()(T* obj)
86  {
87  delete obj;
88  }
89  };
90 
91  template <class T>
92  void verify_index_for_container(size_t index, const T& t)
93  {
94 #ifndef _DEBUG
95  static_cast<void>(t);
96  static_cast<void>(index);
97 #endif
98  Assert((index==0&&t.size()==1)
99  || (t.size()>1 && index<=t.size()-1));
100  }
101 
102  template <class C, class T> void destroy_from_container(C& _container, T* pT)
103  {
104  typename C::iterator it = std::find(_container.begin(), _container.end(), pT);
105  if (it!=_container.end())
106  {
107  delete pT;pT=0;
108  _container.erase(it);
109  }
110  }
111 
112  template <class C, class T> void remove_from_container(C& _container, T* pT)
113  {
114  typename C::iterator it = std::find(_container.begin(), _container.end(), pT);
115  if (it!=_container.end())
116  {
117  _container.erase(it);
118  }
119  }
120 
121  template <class K, class T> T* get_from_map(std::map<K, T*>& _map, const K& key)
122  {
123  typename std::map<K, T*>::const_iterator it = _map.find(key);
124  if (it==_map.end())
125  return 0;
126  return it->second;
127  }
128 
129  template <class K, class T> void destroy_from_map(std::map<K, T*>& _map, const K& key)
130  {
131  typename std::map<K, T*>::iterator it = _map.find(key);
132  Assert(it!=_map.end());
133 
134  T* pT= it->second;
135 
136  _map.erase(key);
137 
138  delete pT; pT = 0;
139  }
140 
141  template <class T> void destroy_all_from_vector(std::vector<T*>& _vector)
142  {
143  while(_vector.empty()==false)
144  {
145  makVrv::destroy_from_container(_vector, *_vector.begin());
146  }
147  };
148 
149  template <class T> void destroy_all_from_list(std::list<T*>& _list)
150  {
151  while(_list.empty()==false)
152  {
153  makVrv::destroy_from_container(_list, *_list.begin());
154  }
155  }
156 
157  template <class T> void destroy_all_from_unordered_set(boost::unordered_set<T*>& _set)
158  {
159  while(_set.empty()==false)
160  {
161  makVrv::destroy_from_container(_set, *_set.begin());
162  }
163  }
164 
165  template <class K, class T> void destroy_all_from_map(std::map<K, T*>& _map)
166  {
167  while(_map.empty()==false)
168  {
169  typename std::map<K, T*>::iterator it = _map.begin();
170 
171  T* pT = it->second;
172  delete pT; pT = 0;
173 
174  _map.erase(it);
175  }
176  _map.clear();
177  }
178 
179  template <class T> T* get_nth_item(boost::unordered_set<T*>& _set, size_t index)
180  {
181  if (_set.empty())
182  return 0;
183 
185 
186  typename std::list<T*>::const_iterator it = _set.begin();
187  std::advance(it, index);
188  return *it;
189  }
190 
191  template <class T> T* get_nth_item(std::list<T*>& _list, size_t index)
192  {
193  if (_list.empty())
194  return 0;
195 
197 
198  typename std::list<T*>::const_iterator it = _list.begin();
199  std::advance(it, index);
200  return *it;
201  }
202 
203  template <class T> const T* get_nth_item(const std::list<T*>& _list, size_t index)
204  {
205  if (_list.empty())
206  return 0;
207 
209 
210  typename std::list<T*>::const_iterator it = _list.begin();
211  std::advance(it, index);
212  return *it;
213  }
214 
215  template <class T> T* get_nth_item(std::vector<T*>& _vector, size_t index)
216  {
217  if (_vector.empty())
218  return 0;
219 
220  makVrv::verify_index_for_container(index, _vector);
221 
222  return _vector[index];
223  }
224 
225  template <class T> T* get_nth_item(const std::vector<T*>& _vector, size_t index)
226  {
227  if (_vector.empty())
228  return 0;
229 
230  makVrv::verify_index_for_container(index, _vector);
231 
232  return _vector[index];
233  }
234 
235  template <class K, class T> T* get_nth_item(std::map<K, T*>& _map, size_t index)
236  {
237  if (_map.empty())
238  return 0;
239 
241 
242  typename std::map<K, T*>::const_iterator it = _map.begin();
243  std::advance(it, index);
244  return it->second;
245  }
246 
247  template <class K, class T> T* get_nth_item(const std::map<K, T*>& _map, size_t index)
248  {
249  if (_map.empty())
250  return 0;
251 
253 
254  typename std::map<K, T*>::const_iterator it = _map.begin();
255  std::advance(it, index);
256  return it->second;
257  }
258 }
Definition: DtSTLUtilities.h:23
const P::first_type & operator()(const P &p) const
Definition: DtSTLUtilities.h:26
Definition: DtSTLUtilities.h:43
_Base::result_type result_type
Definition: DtSTLUtilities.h:50
_Fn::argument_type value
Definition: DtSTLUtilities.h:70
const P::second_type & operator()(const P &p) const
Definition: DtSTLUtilities.h:36
result_type operator()(void)
Definition: DtSTLUtilities.h:63
T * get_nth_item(boost::unordered_set< T * > &_set, size_t index)
Definition: DtSTLUtilities.h:179
void verify_index_for_container(size_t index, const T &t)
Definition: DtSTLUtilities.h:92
binderNoArgs< _Fn > bind(const _Fn &_Func, const _Ty &_Left)
Definition: DtSTLUtilities.h:76
void destroy_all_from_vector(std::vector< T * > &_vector)
Definition: DtSTLUtilities.h:141
binderNoArgs(const _Fn &_Func, const typename _Fn::argument_type &_Left)
Definition: DtSTLUtilities.h:52
void destroy_from_map(std::map< K, T * > &_map, const K &key)
Definition: DtSTLUtilities.h:129
void remove_from_container(C &_container, T *pT)
Definition: DtSTLUtilities.h:112
_Fn op
Definition: DtSTLUtilities.h:69
void destroy_from_container(C &_container, T *pT)
Definition: DtSTLUtilities.h:102
T * get_from_map(std::map< K, T * > &_map, const K &key)
Definition: DtSTLUtilities.h:121
result_type operator()(void) const
Definition: DtSTLUtilities.h:58
void destroy_all_from_unordered_set(boost::unordered_set< T * > &_set)
Definition: DtSTLUtilities.h:157
_Base::argument_type argument_type
Definition: DtSTLUtilities.h:49
Definition: DtSTLUtilities.h:33
Definition: DtSTLUtilities.h:83
void destroy_all_from_map(std::map< K, T * > &_map)
Definition: DtSTLUtilities.h:165
std::unary_function< typename _Fn::argument_type, typename _Fn::result_type > _Base
Definition: DtSTLUtilities.h:48
void operator()(T *obj)
Definition: DtSTLUtilities.h:85
void destroy_all_from_list(std::list< T * > &_list)
Definition: DtSTLUtilities.h:149
#define Assert(exp)
Definition: DtAssert.h:25

Document ID: Generated on Thu Jun 1 17:58:13 EDT 2023 from SVN revision 255404
Copyright © 2005-2021 MAK Technologies. All Rights Reserved (www.mak.com)