VR-Vantage 2.7 API Documentation
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
templateUtil.h
Go to the documentation of this file.
1 /*****************************************************************************
2  * Copyright (c) 2020 MAK Technologies, Inc.
3  * All rights reserved.
4  *****************************************************************************/
5 
9 
10 #pragma once
11 
12 #include <vlutil/vlExceptions.h>
13 #include <functional>
14 #include <vector>
15 #include <map>
16 
17 // Expand the provide macro.
18 #define DO_PP(MACRO) MACRO
19 
20 // Theses macros make it easy to define VAR-ARG template classes.
21 
22 // Make stream puts an 'word' with the index prepended separated by "<<"
23 // Example MAKE_STREAM_1(A) output A1
24 // Example MAKE_STREAM_2(A) output A1 << A2
25 #define MAKE_STREAM_1(word) word##1
26 #define MAKE_STREAM_2(word) MAKE_STREAM_1(word)<<word##2
27 #define MAKE_STREAM_3(word) MAKE_STREAM_2(word)<<word##3
28 #define MAKE_STREAM_4(word) MAKE_STREAM_3(word)<<word##4
29 #define MAKE_STREAM_5(word) MAKE_STREAM_4(word)<<word##5
30 #define MAKE_STREAM_6(word) MAKE_STREAM_5(word)<<word##6
31 #define MAKE_STREAM_7(word) MAKE_STREAM_6(word)<<word##7
32 #define MAKE_STREAM_8(word) MAKE_STREAM_7(word)<<word##8
33 #define MAKE_STREAM_9(word) MAKE_STREAM_8(word)<<word##9
34 #define MAKE_STREAM_10(word) MAKE_STREAM_9(word)<<word##10
35 
36 #define MAKE_STREAM(word,count) DO_PP(MAKE_STREAM_##count(word))
37 
38 
39 // Make stream puts an 'word' with the index prepended separated by ">>"
40 // Example MAKE_STREAMOUT_1(A) output A1
41 // Example MAKE_STREAMOUT_2(A) output A1 >> A2
42 #define MAKE_STREAMOUT_1(word) word##1
43 #define MAKE_STREAMOUT_2(word) MAKE_STREAMOUT_1(word)>>word##2
44 #define MAKE_STREAMOUT_3(word) MAKE_STREAMOUT_2(word)>>word##3
45 #define MAKE_STREAMOUT_4(word) MAKE_STREAMOUT_3(word)>>word##4
46 #define MAKE_STREAMOUT_5(word) MAKE_STREAMOUT_4(word)>>word##5
47 #define MAKE_STREAMOUT_6(word) MAKE_STREAMOUT_5(word)>>word##6
48 #define MAKE_STREAMOUT_7(word) MAKE_STREAMOUT_6(word)>>word##7
49 #define MAKE_STREAMOUT_8(word) MAKE_STREAMOUT_7(word)>>word##8
50 #define MAKE_STREAMOUT_9(word) MAKE_STREAMOUT_8(word)>>word##9
51 #define MAKE_STREAMOUT_10(word) MAKE_STREAMOUT_9(word)>>word##10
52 
53 #define MAKE_STREAMOUT(word,count) DO_PP(MAKE_STREAMOUT_##count(word))
54 
55 
56 
57 //Make list puts an 'word' with the index prepended separated by ","
58 //Example MAKE_LIST_1(A) output A1
59 //Example MAKE_LIST_2(A) output A1,A2
60 #define MAKE_LIST_1(word) word##1
61 #define MAKE_LIST_2(word) MAKE_LIST_1(word),word##2
62 #define MAKE_LIST_3(word) MAKE_LIST_2(word),word##3
63 #define MAKE_LIST_4(word) MAKE_LIST_3(word),word##4
64 #define MAKE_LIST_5(word) MAKE_LIST_4(word),word##5
65 #define MAKE_LIST_6(word) MAKE_LIST_5(word),word##6
66 #define MAKE_LIST_7(word) MAKE_LIST_6(word),word##7
67 #define MAKE_LIST_8(word) MAKE_LIST_7(word),word##8
68 #define MAKE_LIST_9(word) MAKE_LIST_8(word),word##9
69 #define MAKE_LIST_10(word) MAKE_LIST_9(word),word##10
70 
71 #define MAKE_LIST(word,count) DO_PP(MAKE_LIST_##count(word))
72 
73 //Make multilist puts two words 'first' then 'second' each with the index of the
74 //pair, prepended separated by ","
75 //Example MAKE_MULTILIST_1(A,::B) output A1::B1
76 //Example MAKE_MULTILIST_2(A,::B) output A1::B1,A2::B2
77 #define MAKE_MULTILIST_1(first,second) first##1 second##1
78 #define MAKE_MULTILIST_2(first,second) MAKE_MULTILIST_1(first,second),first##2 second##2
79 #define MAKE_MULTILIST_3(first,second) MAKE_MULTILIST_2(first,second),first##3 second##3
80 #define MAKE_MULTILIST_4(first,second) MAKE_MULTILIST_3(first,second),first##4 second##4
81 #define MAKE_MULTILIST_5(first,second) MAKE_MULTILIST_4(first,second),first##5 second##5
82 #define MAKE_MULTILIST_6(first,second) MAKE_MULTILIST_5(first,second),first##6 second##6
83 #define MAKE_MULTILIST_7(first,second) MAKE_MULTILIST_6(first,second),first##7 second##7
84 #define MAKE_MULTILIST_8(first,second) MAKE_MULTILIST_7(first,second),first##8 second##8
85 #define MAKE_MULTILIST_9(first,second) MAKE_MULTILIST_8(first,second),first##9 second##9
86 #define MAKE_MULTILIST_10(first,second) MAKE_MULTILIST_9(first,second),first##10 second##10
87 
88 #define MAKE_MULTILIST(first,second,count) DO_PP(MAKE_MULTILIST_##count(first,second))
89 
90 
91 //A slightly odd macro, needed to for a series of inline functions.
92 //pair, prepended separated by ","
93 //Example MAKE_CAST_CALL_1(A,B,C,D) output A1B(C,D1)
94 //Example MAKE_CAST_CALL_2(A,::B,C)output A1B(C,D1),A2B(C,D2)
95 #define MAKE_CAST_CALL_1(first,second,third,forth) first##1 second(third,forth##1)
96 #define MAKE_CAST_CALL_2(first,second,third,forth) MAKE_CAST_CALL_1(first,second,third,forth),first##2 second(third,forth##2)
97 #define MAKE_CAST_CALL_3(first,second,third,forth) MAKE_CAST_CALL_2(first,second,third,forth),first##3 second(third,forth##3)
98 #define MAKE_CAST_CALL_4(first,second,third,forth) MAKE_CAST_CALL_3(first,second,third,forth),first##4 second(third,forth##4)
99 #define MAKE_CAST_CALL_5(first,second,third,forth) MAKE_CAST_CALL_4(first,second,third,forth),first##5 second(third,forth##5)
100 #define MAKE_CAST_CALL_6(first,second,third,forth) MAKE_CAST_CALL_5(first,second,third,forth),first##6 second(third,forth##6)
101 #define MAKE_CAST_CALL_7(first,second,third,forth) MAKE_CAST_CALL_6(first,second,third,forth),first##7 second(third,forth##7)
102 #define MAKE_CAST_CALL_8(first,second,third,forth) MAKE_CAST_CALL_7(first,second,third,forth),first##8 second(third,forth##8)
103 #define MAKE_CAST_CALL_9(first,second,third,forth) MAKE_CAST_CALL_8(first,second,third,forth),first##9 second(third,forth##9)
104 #define MAKE_CAST_CALL_10(first,second,third,forth) MAKE_CAST_CALL_9(first,second,third,forth),first##10 second(third,forth##10)
105 
106 #define MAKE_CAST_CALL(first,second,third,forth,count) DO_PP(MAKE_CAST_CALL_##count(first,second,third,forth))
107 
108 
109 //Make list puts an 'word' with the index prepended separated by ";"
110 //Example MAKE_DECL_LIST_1(int A) output int A
111 //Example MAKE_DECL_LIST_1(int A) output int A1; int A2
112 #define MAKE_DECL_LIST_1(word) word##1
113 #define MAKE_DECL_LIST_2(word) MAKE_DECL_LIST_1(word);word##2
114 #define MAKE_DECL_LIST_3(word) MAKE_DECL_LIST_2(word);word##3
115 #define MAKE_DECL_LIST_4(word) MAKE_DECL_LIST_3(word);word##4
116 #define MAKE_DECL_LIST_5(word) MAKE_DECL_LIST_4(word);word##5
117 #define MAKE_DECL_LIST_6(word) MAKE_DECL_LIST_5(word);word##6
118 #define MAKE_DECL_LIST_7(word) MAKE_DECL_LIST_6(word);word##7
119 #define MAKE_DECL_LIST_8(word) MAKE_DECL_LIST_7(word);word##8
120 #define MAKE_DECL_LIST_9(word) MAKE_DECL_LIST_8(word);word##9
121 #define MAKE_DECL_LIST_10(word) MAKE_DECL_LIST_9(word);word##10
122 
123 #define MAKE_DECL_LIST(word,count) DO_PP(MAKE_DECL_LIST_##count(word))
124 
125 //Make list puts an 'word' with the index prepended separated by ";"
126 //Example MAKE_DECL_LIST_1(int A) output int A
127 //Example MAKE_DECL_LIST_1(int A) output int A1; int A2
128 #define MAKE_DECL_MULTILIST_1(first,second) first##1 second##1
129 #define MAKE_DECL_MULTILIST_2(first,second) MAKE_DECL_MULTILIST_1(first,second);first##2 second##2
130 #define MAKE_DECL_MULTILIST_3(first,second) MAKE_DECL_MULTILIST_2(first,second);first##3 second##3
131 #define MAKE_DECL_MULTILIST_4(first,second) MAKE_DECL_MULTILIST_3(first,second);first##4 second##4
132 #define MAKE_DECL_MULTILIST_5(first,second) MAKE_DECL_MULTILIST_4(first,second);first##5 second##5
133 #define MAKE_DECL_MULTILIST_6(first,second) MAKE_DECL_MULTILIST_5(first,second);first##6 second##6
134 #define MAKE_DECL_MULTILIST_7(first,second) MAKE_DECL_MULTILIST_6(first,second);first##7 second##7
135 #define MAKE_DECL_MULTILIST_8(first,second) MAKE_DECL_MULTILIST_7(first,second);first##8 second##8
136 #define MAKE_DECL_MULTILIST_9(first,second) MAKE_DECL_MULTILIST_8(first,second);first##9 second##9
137 #define MAKE_DECL_MULTILIST_10(first,second) MAKE_DECL_MULTILIST_9(first,second);first##10 second##10
138 
139 #define MAKE_DECL_MULTILIST(first,second,count) DO_PP(MAKE_DECL_MULTILIST_##count(first,second))
140 
141 #ifdef _WIN32
142 #define EXPORT_TEMPLATE_TYPEDEF(C,TD)\
143  typedef C TD;
144 #else
145 #define EXPORT_TEMPLATE_TYPEDEF(C,TD)\
146  typedef C TD;\
147  template class C;
148 #endif
149 
150 // FAST_REPEATED_CALL implements/mimics Duff's Device.
151 // A Google search will provide examples and explanations.
152 #define FAST_REPEATED_CALL(count,x) \
153 { \
154  if(count > 0) \
155  { \
156  size_t __FAST_ITERATE_count = count;\
157  size_t __FAST_ITERATE_n = (__FAST_ITERATE_count + 7) >> 3; \
158  switch (__FAST_ITERATE_count & 7) \
159  { \
160  case 0: do { x;\
161  case 7: x; \
162  case 6: x; \
163  case 5: x; \
164  case 4: x; \
165  case 3: x; \
166  case 2: x; \
167  case 1: x; \
168  } while (--__FAST_ITERATE_n > 0);\
169  }\
170  }\
171 }
172 
173 
174 namespace makVrv
175 {
176 
177 //Used to search a key in a map, and return NULL if it is not found.
178 template <typename K,typename V, typename Pr,typename Alloc>
179 V* DtFindPointerInMap(const std::map<K,V,Pr,Alloc>& map,K key)
180 {
181  typename std::map<K,V,Pr,Alloc>::const_iterator iter = map.find(key);
182  if(iter == map.end())
183  {
184  return 0;
185  }
186  else
187  {
188  return const_cast<V*>(&iter->second);
189  }
190 }
191 
197 template <typename M>
199 {
200  typename M::mapped_type ptr;
201  typename M::iterator curIter = map.begin();
202  typename M::const_iterator endIter = map.end();
203  for ( ; curIter != endIter; ++curIter )
204  {
205  ptr = curIter->second;
206  curIter->second = 0;
207  delete ptr;
208  }
209  map.clear();
210 }
211 
217 template <typename V>
219 {
220  typename V::value_type ptr;
221  typename V::iterator curIter = vec.begin();
222  typename V::iterator endIter = vec.end();
223  for ( ; curIter != endIter; ++curIter )
224  {
225  ptr = *curIter;
226  (*curIter) = 0;
227  delete ptr;
228  }
229  vec.clear();
230 }
231 
232 template <typename Iter>
233 void DtDeletePointersInIterators(Iter begin, Iter end)
234 {
235  for(Iter cur = begin;cur != end;++cur)
236  {
237  delete *cur;
238  }
239 }
240 
241 template <typename Key, typename Value>
243 {
244  inline bool operator()(const std::pair<Key,Value>& a, const std::pair<Key,Value>& b) const
245  {
246  return a.first < b.first;
247  }
248 };
249 
250 
260 template <typename T,typename R,typename A>
261 std::binder1st<std::mem_fun1_t<R,T,A> > callMemberFunction(R (T::*memfun)(A),T* ptr)
262 {
263  return std::bind1st(std::mem_fun(memfun),ptr);
264 }
265 
266 template <typename T,typename R,typename A>
267 std::binder1st<std::mem_fun1_t<R,T,A> > callMemberRefFunction(R (T::*memfun)(A&),T* ptr)
268 {
269  return std::bind1st(std::mem_fun(memfun),ptr);
270 }
271 
272 template <typename T,typename R,typename A>
273 std::binder1st<std::mem_fun1_t<R,T,A> > callMemberConstRefFunction(R (T::*memfun)(const A&),T* ptr)
274 {
275  return std::bind1st(std::mem_fun(memfun),ptr);
276 }
277 
278 template <typename T,typename R,typename A,typename Col>
279 void forEachCallMember( T* _this, R (T::*memfun)(A),Col& col)
280 {
281  typename Col::iterator curIter = col.begin();
282  typename Col::iterator endIter = col.end();
283  for(;curIter != endIter;++curIter)
284  {
285  (_this->*memfun)(*curIter);
286  }
287 }
288 
289 template <typename Ret,typename AT,typename Class>
290 Ret callConstFunction1(const Class* _this,
291  const Ret (Class::* memberPtr)(AT) const,
292  AT param)
293 {
294  return const_cast<Ret>(_this->*(memberPtr)(param));
295 }
296 
297 class DtDe;
298 
299 template <typename T, void(T::*MF)()>
301 {
302  (((T*)data)->*MF)();
303 }
304 
305 template <class InputIterator1, class InputIterator2, class OutputIterator1,class OutputIterator2>
306 void set_split_difference ( InputIterator1 first1, InputIterator1 last1,
307  InputIterator2 first2, InputIterator2 last2,
308  OutputIterator1 result1,
309  OutputIterator2 result2 )
310 {
311  while (first1!=last1 && first2!=last2)
312  {
313  if (*first1<*first2)
314  {
315  *result1++ = *first1++;
316  }
317  else if (*first2<*first1)
318  {
319  *result2++ = *first2++;
320  }
321  else
322  {
323  first1++;
324  first2++;
325  }
326 
327  }
328  copy(first1,last1,result1);
329  copy(first2,last2,result2);
330 }
331 
332 //#include <boost/bind.hpp>
333 //#include <vrvUtil/signalslib.h>
334 //
335 //template <typename Argument>
336 //void forwardSignal(boost::signalslib::signal<void (Argument)>& emitter,
338 //{
344 //}
345 //
346 //template <typename Argument,typename Argument2>
347 //void forwardSignal(boost::signalslib::signal<void (Argument,Argument2)>& emitter,
349 //{
355 //}
356 
357 template <typename iter, typename T>
358 iter fasterFind(size_t size, iter begin, iter end,const T& value)
359 {
360  FAST_REPEATED_CALL(size, {if(*begin == value) return begin; else ++begin;} )
361  return end;
362 }
363 
364 template <typename iter, typename F>
365 iter fastFindPred(size_t size, iter begin, iter end,const F& pred)
366 {
367  FAST_REPEATED_CALL(size, {if(pred(*begin)) return begin; else ++begin;} )
368  return end;
369 }
370 
371 template <typename T, typename Ax >
372 void fastEraseFromUnsortedVector(std::vector<T,Ax>& list, const T& value)
373 {
374  size_t size = list.size();
375  if(size == 0)
376  {
377  return;
378  }
379  else
380  {
381  typename std::vector<T,Ax>::iterator i = fasterFind(size,list.begin(),list.end(),value);
382  if(i != list.end())
383  {
384  if(size == 1)
385  {
386  list.resize(0);
387  }
388  else
389  {
390  if(&(*i) != &list.back())
391  {
392  *i = list.back();
393  }
394  list.resize(size - 1);
395  }
396  }
397  }
398 }
399 
400 template <typename T, typename Ax >
401 void fastEraseFromUnsortedVector(std::vector<T*,Ax>& list, const T* value)
402 {
403  size_t size = list.size();
404  if(size == 0)
405  {
406  return;
407  }
408  else if(size == 1)
409  {
410  list.resize(0);
411  }
412  else
413  {
414  typename std::vector<T*,Ax>::iterator i = fasterFind(size,list.begin(),list.end(),value);
415  if(i != list.end())
416  {
417  if(&(*i) != &list.back())
418  {
419  *i = list.back();
420  }
421  list.resize(size - 1);
422  }
423  }
424 }
425 
426 template <typename T, typename Ax, typename Function >
427 void fastEraseFromUnsortedVectorPred(std::vector<T,Ax>& list, const Function& f)
428 {
429  size_t size = list.size();
430  if(size == 0)
431  {
432  return;
433  }
434  else if(size == 1)
435  {
436  list.resize(0);
437  }
438  else
439  {
440  typename std::vector<T,Ax>::iterator i = fastFindPred(size,list.begin(),list.end(),f);
441  if(i != list.end())
442  {
443  if(&(*i) != &list.back())
444  {
445  *i = list.back();
446  }
447  list.resize(size - 1);
448  }
449  }
450 }
451 
452 template <typename T, typename Ax, typename Function>
453 void fastEraseFromUnsortedVectorPred(std::vector<T*,Ax>& list, const Function& f)
454 {
455  size_t size = list.size();
456  if(size == 0)
457  {
458  return;
459  }
460  else if(size == 1)
461  {
462  list.resize(0);
463  }
464  else
465  {
466  typename std::vector<T*,Ax>::iterator i =fastFindPred(size,list.begin(),list.end(), f );
467  if(i != list.end())
468  {
469  if(&(*i) != &list.back())
470  {
471  *i = list.back();
472  }
473  list.resize(size - 1);
474  }
475  }
476 }
477 
478 template <typename Ret, typename T>
480 {
481  typedef Ret (T::*MemberFnPtr)() const;
483 
485  : myPtr(memFun)
486  {
487  }
488 
490  : myPtr(second.myPtr)
491  {
492  }
493 
494  bool operator()(const T* a, const T* b) const
495  {
496  return (a->*myPtr)() < (b->*myPtr)();
497  }
498 };
499 
500 template <typename Ret, typename T>
502 {
504 }
505 
506 template <typename Ret, typename T>
508 {
509  typedef Ret (T::*MemberFnPtr)() const;
511  Ret myValue;
512 
514  : myPtr(memFun)
515  , myValue(value)
516  {
517  }
518 
520  : myPtr(second.myPtr)
521  , myValue(second.myValue)
522  {
523  }
524 
525  bool operator()(const T* a) const
526  {
527  return (a->*myPtr)() == myValue;
528  }
529 };
530 
531 template <typename Ret, typename T>
533 {
535 }
536 
538 template <typename K,typename V>
539 const V& findOrThrow(const std::map<K,V,std::less<K>, std::allocator<std::pair<const K, V> > >& map, K key)
540 {
541  typename std::map<K,V>::const_iterator iter = map.find(key);
542  if(iter == map.end())
543  {
544  DtTHROW_NEW(DtInvalidInput,"Unable to find key");
545  }
546  return iter->second;
547 }
548 
550 template <typename K,typename V>
551 const V& findByIndexOrThrow(const std::map<K,V,std::less<K>, std::allocator<std::pair<const K, V> > >& map, int index)
552 {
553  if(index >= (int)map.size())
554  {
555  DtTHROW_NEW(DtInvalidInput,"Index out of range.");
556  }
557  typename std::map<K,V>::const_iterator i = map.begin();
558  std::advance(i,index);
559  return i->second;
560 }
561 
563 template <typename V>
564 const V& findByIndexOrThrow(const std::vector<V>& vec, int index)
565 {
566  if(index >= (int)vec.size())
567  {
568  DtTHROW_NEW(DtInvalidInput,"Index out of range.");
569  }
570  typename std::vector<V>::const_iterator i = vec.begin();
571  std::advance(i,index);
572  return *i;
573 }
574 
575 
576 }


Copyright © 2005-2021 MAK Technologies. All Rights Reserved (www.mak.com)