VR-Forces 4.3.1 Class Documentation
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
DtSTLUtilities.h
Go to the documentation of this file.
1 /******************************************************************************
2 ** Copyright (c) 2014 MAK Technologies, Inc.
3 ** All rights reserved.
4 ******************************************************************************/
5 
9 #pragma once
10 
11 #include <utility>
12 #include <functional>
13 #include <map>
14 #include <vector>
15 #include <list>
16 #include <boost/unordered_map.hpp>
17 #include <boost/unordered_set.hpp>
18 
19 namespace makVrv
20 {
21  template<typename P>
23  {
24  const typename P::first_type&
25  operator()(const P& p) const
26  {
27  return p.first;
28  }
29  };
30 
31  template<typename P>
33  {
34  const typename P::second_type&
35  operator()(const P& p) const
36  {
37  return p.second;
38  }
39  };
40 
41  template<class _Fn>
43  : public std::unary_function<typename _Fn::argument_type, typename _Fn::result_type>
44  { // functor adapter _Func(stored, right)
45  public:
46  typedef std::unary_function<typename _Fn::argument_type,
47  typename _Fn::result_type> _Base;
48  typedef typename _Base::argument_type argument_type;
49  typedef typename _Base::result_type result_type;
50 
51  binderNoArgs(const _Fn& _Func,
52  const typename _Fn::argument_type& _Left)
53  : op(_Func), value(_Left)
54  { // construct from functor and left operand
55  }
56 
57  result_type operator()(void) const
58  { // apply functor to operands
59  return (op(value));
60  }
61 
63  { // apply functor to operands
64  return (op(value));
65  }
66 
67  protected:
68  _Fn op; // the functor to apply
69  typename _Fn::argument_type value; // the left operand
70  };
71 
72  // TEMPLATE FUNCTION bind (based upon std::bind1st)
73  template<class _Fn,
74  class _Ty> inline
75  binderNoArgs<_Fn> bind(const _Fn& _Func, const _Ty& _Left)
76  { // return a binderNoArgs functor adapter
77  typename _Fn::argument_type _Val(_Left);
78  return (binderNoArgs<_Fn>(_Func, _Val));
79  }
80 
81 
82  template <class T> struct DeletePtr
83  {
84  void operator()(T* obj)
85  {
86  delete obj;
87  }
88  };
89 
90  template <class T>
91  void verify_index_for_container(size_t index, const T& t)
92  {
93 #ifndef _DEBUG
94  static_cast<void>(t);
95  static_cast<void>(index);
96 #endif
97  Assert((index==0&&t.size()==1)
98  || (t.size()>1 && index<=t.size()-1));
99  }
100 
101  template <class C, class T> void destroy_from_container(C& _container, T* pT)
102  {
103  typename C::iterator it = std::find(_container.begin(), _container.end(), pT);
104  if (it!=_container.end())
105  {
106  delete pT;pT=0;
107  _container.erase(it);
108  }
109  }
110 
111  template <class C, class T> void remove_from_container(C& _container, T* pT)
112  {
113  typename C::iterator it = std::find(_container.begin(), _container.end(), pT);
114  if (it!=_container.end())
115  {
116  delete pT;pT=0;
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 }

Document ID: Generated on Wed Jul 29 00:55:00 EDT 2015 from SVN revision 155257
Copyright © 2005-2015 VT MÄK. All Rights Reserved (www.mak.com)