VR-Forces 4.6.1 Class Documentation
 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  delete pT;pT=0;
118  _container.erase(it);
119  }
120  }
121 
122  template <class K, class T> T* get_from_map(std::map<K, T*>& _map, const K& key)
123  {
124  typename std::map<K, T*>::const_iterator it = _map.find(key);
125  if (it==_map.end())
126  return 0;
127  return it->second;
128  }
129 
130  template <class K, class T> void destroy_from_map(std::map<K, T*>& _map, const K& key)
131  {
132  typename std::map<K, T*>::iterator it = _map.find(key);
133  Assert(it!=_map.end());
134 
135  T* pT= it->second;
136 
137  _map.erase(key);
138 
139  delete pT; pT = 0;
140  }
141 
142  template <class T> void destroy_all_from_vector(std::vector<T*>& _vector)
143  {
144  while(_vector.empty()==false)
145  {
146  makVrv::destroy_from_container(_vector, *_vector.begin());
147  }
148  };
149 
150  template <class T> void destroy_all_from_list(std::list<T*>& _list)
151  {
152  while(_list.empty()==false)
153  {
154  makVrv::destroy_from_container(_list, *_list.begin());
155  }
156  }
157 
158  template <class T> void destroy_all_from_unordered_set(boost::unordered_set<T*>& _set)
159  {
160  while(_set.empty()==false)
161  {
162  makVrv::destroy_from_container(_set, *_set.begin());
163  }
164  }
165 
166  template <class K, class T> void destroy_all_from_map(std::map<K, T*>& _map)
167  {
168  while(_map.empty()==false)
169  {
170  typename std::map<K, T*>::iterator it = _map.begin();
171 
172  T* pT = it->second;
173  delete pT; pT = 0;
174 
175  _map.erase(it);
176  }
177  _map.clear();
178  }
179 
180  template <class T> T* get_nth_item(boost::unordered_set<T*>& _set, size_t index)
181  {
182  if (_set.empty())
183  return 0;
184 
186 
187  typename std::list<T*>::const_iterator it = _set.begin();
188  std::advance(it, index);
189  return *it;
190  }
191 
192  template <class T> T* get_nth_item(std::list<T*>& _list, size_t index)
193  {
194  if (_list.empty())
195  return 0;
196 
198 
199  typename std::list<T*>::const_iterator it = _list.begin();
200  std::advance(it, index);
201  return *it;
202  }
203 
204  template <class T> const T* get_nth_item(const std::list<T*>& _list, size_t index)
205  {
206  if (_list.empty())
207  return 0;
208 
210 
211  typename std::list<T*>::const_iterator it = _list.begin();
212  std::advance(it, index);
213  return *it;
214  }
215 
216  template <class T> T* get_nth_item(std::vector<T*>& _vector, size_t index)
217  {
218  if (_vector.empty())
219  return 0;
220 
221  makVrv::verify_index_for_container(index, _vector);
222 
223  return _vector[index];
224  }
225 
226  template <class T> T* get_nth_item(const std::vector<T*>& _vector, size_t index)
227  {
228  if (_vector.empty())
229  return 0;
230 
231  makVrv::verify_index_for_container(index, _vector);
232 
233  return _vector[index];
234  }
235 
236  template <class K, class T> T* get_nth_item(std::map<K, T*>& _map, size_t index)
237  {
238  if (_map.empty())
239  return 0;
240 
242 
243  typename std::map<K, T*>::const_iterator it = _map.begin();
244  std::advance(it, index);
245  return it->second;
246  }
247 
248  template <class K, class T> T* get_nth_item(const std::map<K, T*>& _map, size_t index)
249  {
250  if (_map.empty())
251  return 0;
252 
254 
255  typename std::map<K, T*>::const_iterator it = _map.begin();
256  std::advance(it, index);
257  return it->second;
258  }
259 }

Document ID: Generated on Wed Jul 25 16:57:45 EDT 2018 from SVN revision 190790
Copyright © 2005-2018 VT MÄK. All Rights Reserved (www.mak.com)