VR-Forces Developer's Guide
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
templateUtil.h
Go to the documentation of this file.
1 /*****************************************************************************
2  * Copyright (c) 2023 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 
183 template <typename M>
185 {
186  for (auto& keyVal : map)
187  {
188  auto ptr = keyVal.second;
189  keyVal.second = 0;
190  delete ptr;
191  }
192  map.clear();
193 }
194 
200 template <typename V>
202 {
203  typename V::value_type ptr;
204  typename V::iterator curIter = vec.begin();
205  typename V::iterator endIter = vec.end();
206  for ( ; curIter != endIter; ++curIter )
207  {
208  ptr = *curIter;
209  (*curIter) = 0;
210  delete ptr;
211  }
212  vec.clear();
213 }
214 
215 template <typename Iter>
216 void DtDeletePointersInIterators(Iter begin, Iter end)
217 {
218  for(Iter cur = begin;cur != end;++cur)
219  {
220  delete *cur;
221  }
222 }
223 
224 template <typename Key, typename Value>
226 {
227  inline bool operator()(const std::pair<Key,Value>& a, const std::pair<Key,Value>& b) const
228  {
229  return a.first < b.first;
230  }
231 };
232 
233 
234 template <typename T,typename R,typename A,typename Col>
235 void forEachCallMember( T* _this, R (T::*memfun)(A),Col& col)
236 {
237  typename Col::iterator curIter = col.begin();
238  typename Col::iterator endIter = col.end();
239  for(;curIter != endIter;++curIter)
240  {
241  (_this->*memfun)(*curIter);
242  }
243 }
244 
245 template <typename Ret,typename AT,typename Class>
246 Ret callConstFunction1(const Class* _this,
247  const Ret (Class::* memberPtr)(AT) const,
248  AT param)
249 {
250  return const_cast<Ret>(_this->*(memberPtr)(param));
251 }
252 
253 class DtDe;
254 
255 template <typename T, void(T::*MF)()>
256 void DtMemberFunctionCallback(DtDe* de,void* data)
257 {
258  (((T*)data)->*MF)();
259 }
260 
261 template <class InputIterator1, class InputIterator2, class OutputIterator1,class OutputIterator2>
262 void set_split_difference ( InputIterator1 first1, InputIterator1 last1,
263  InputIterator2 first2, InputIterator2 last2,
264  OutputIterator1 result1,
265  OutputIterator2 result2 )
266 {
267  while (first1!=last1 && first2!=last2)
268  {
269  if (*first1<*first2)
270  {
271  *result1++ = *first1++;
272  }
273  else if (*first2<*first1)
274  {
275  *result2++ = *first2++;
276  }
277  else
278  {
279  first1++;
280  first2++;
281  }
282 
283  }
284  copy(first1,last1,result1);
285  copy(first2,last2,result2);
286 }
287 
288 //#include <boost/bind.hpp>
289 //#include <vrvUtil/signalslib.h>
290 //
291 //template <typename Argument>
292 //void forwardSignal(boost::signalslib::signal<void (Argument)>& emitter,
294 //{
300 //}
301 //
302 //template <typename Argument,typename Argument2>
303 //void forwardSignal(boost::signalslib::signal<void (Argument,Argument2)>& emitter,
305 //{
311 //}
312 
313 template <typename iter, typename T>
314 iter fasterFind(size_t size, iter begin, iter end,const T& value)
315 {
316  FAST_REPEATED_CALL(size, {if(*begin == value) return begin; else ++begin;} )
317  return end;
318 }
319 
320 template <typename iter, typename F>
321 iter fastFindPred(size_t size, iter begin, iter end,const F& pred)
322 {
323  FAST_REPEATED_CALL(size, {if(pred(*begin)) return begin; else ++begin;} )
324  return end;
325 }
326 
327 template <typename T, typename Ax >
328 void fastEraseFromUnsortedVector(std::vector<T,Ax>& list, const T& value)
329 {
330  size_t size = list.size();
331  if(size == 0)
332  {
333  return;
334  }
335  else
336  {
337  typename std::vector<T,Ax>::iterator i = fasterFind(size,list.begin(),list.end(),value);
338  if(i != list.end())
339  {
340  if(size == 1)
341  {
342  list.resize(0);
343  }
344  else
345  {
346  if(&(*i) != &list.back())
347  {
348  *i = list.back();
349  }
350  list.resize(size - 1);
351  }
352  }
353  }
354 }
355 
356 template <typename T, typename Ax >
357 void fastEraseFromUnsortedVector(std::vector<T*,Ax>& list, const T* value)
358 {
359  size_t size = list.size();
360  if(size == 0)
361  {
362  return;
363  }
364  else if(size == 1)
365  {
366  list.resize(0);
367  }
368  else
369  {
370  typename std::vector<T*,Ax>::iterator i = fasterFind(size,list.begin(),list.end(),value);
371  if(i != list.end())
372  {
373  if(&(*i) != &list.back())
374  {
375  *i = list.back();
376  }
377  list.resize(size - 1);
378  }
379  }
380 }
381 
382 template <typename T, typename Ax, typename Function >
383 void fastEraseFromUnsortedVectorPred(std::vector<T,Ax>& list, const Function& f)
384 {
385  size_t size = list.size();
386  if(size == 0)
387  {
388  return;
389  }
390  else
391  {
392  typename std::vector<T,Ax>::iterator i = fastFindPred(size,list.begin(),list.end(),f);
393  if(i != list.end())
394  {
395  if(&(*i) != &list.back())
396  {
397  *i = list.back();
398  }
399  list.resize(size - 1);
400  }
401  }
402 }
403 
404 template <typename T, typename Ax, typename Function>
405 void fastEraseFromUnsortedVectorPred(std::vector<T*,Ax>& list, const Function& f)
406 {
407  size_t size = list.size();
408  if(size == 0)
409  {
410  return;
411  }
412  else
413  {
414  typename std::vector<T*,Ax>::iterator i =fastFindPred(size,list.begin(),list.end(), f );
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 Ret, typename T>
428 {
429  typedef Ret (T::*MemberFnPtr)() const;
431 
433  : myPtr(memFun)
434  {
435  }
436 
438  : myPtr(second.myPtr)
439  {
440  }
441 
442  bool operator()(const T* a, const T* b) const
443  {
444  return (a->*myPtr)() < (b->*myPtr)();
445  }
446 };
447 
448 template <typename Ret, typename T>
450 {
452 }
453 
454 template <typename Ret, typename T>
456 {
457  typedef Ret (T::*MemberFnPtr)() const;
459  Ret myValue;
460 
462  : myPtr(memFun)
463  , myValue(value)
464  {
465  }
466 
468  : myPtr(second.myPtr)
469  , myValue(second.myValue)
470  {
471  }
472 
473  bool operator()(const T* a) const
474  {
475  return (a->*myPtr)() == myValue;
476  }
477 };
478 
479 template <typename Ret, typename T>
480 inline EqualPointerByFunction_T<Ret,T> EqualPointerByFunction(Ret (T::*ptr)() const,Ret value)
481 {
482  return EqualPointerByFunction_T<Ret,T>(ptr,value);
483 }
484 
486 template <typename K,typename V>
487 const V& findOrThrow(const std::map<K,V,std::less<K>, std::allocator<std::pair<const K, V> > >& map, K key)
488 {
489  typename std::map<K,V>::const_iterator iter = map.find(key);
490  if(iter == map.end())
491  {
492  DtTHROW_NEW(DtInvalidInput,"Unable to find key");
493  }
494  return iter->second;
495 }
496 
498 template <typename K,typename V>
499 const V& findByIndexOrThrow(const std::map<K,V,std::less<K>, std::allocator<std::pair<const K, V> > >& map, int index)
500 {
501  if(index >= (int)map.size())
502  {
503  DtTHROW_NEW(DtInvalidInput,"Index out of range.");
504  }
505  typename std::map<K,V>::const_iterator i = map.begin();
506  std::advance(i,index);
507  return i->second;
508 }
509 
511 template <typename V>
512 const V& findByIndexOrThrow(const std::vector<V>& vec, int index)
513 {
514  if(index >= (int)vec.size())
515  {
516  DtTHROW_NEW(DtInvalidInput,"Index out of range.");
517  }
518  typename std::vector<V>::const_iterator i = vec.begin();
519  std::advance(i,index);
520  return *i;
521 }
522 
523 
524 }
Ret myValue
Definition: templateUtil.h:459
bool operator()(const T *a, const T *b) const
Definition: templateUtil.h:442
voidpf void uLong size
Definition: ioapi.h:39
void DtMemberFunctionCallback(DtDe *de, void *data)
Definition: templateUtil.h:256
EqualPointerByFunction_T< Ret, T > EqualPointerByFunction(Ret(T::*ptr)() const, Ret value)
Definition: templateUtil.h:480
const V & findByIndexOrThrow(const std::map< K, V, std::less< K >, std::allocator< std::pair< const K, V > > > &map, int index)
Find a value by index in a map, or throw an DtInvalidInput exception.
Definition: templateUtil.h:499
Ret callConstFunction1(const Class *_this, const Ret(Class::*memberPtr)(AT) const, AT param)
Definition: templateUtil.h:246
const V & findOrThrow(const std::map< K, V, std::less< K >, std::allocator< std::pair< const K, V > > > &map, K key)
Find a value in a map, or throw an DtInvalidInput exception.
Definition: templateUtil.h:487
void DtClearAndDeletePointersInMap(M &map)
Deletes all pointers in a map then clears it.
Definition: templateUtil.h:184
Definition: templateUtil.h:455
bool operator()(const T *a) const
Definition: templateUtil.h:473
SortPointersByFunction_T< Ret, T > SortPointersByFunction(Ret(T::*ptr)() const)
Definition: templateUtil.h:449
bool operator()(const std::pair< Key, Value > &a, const std::pair< Key, Value > &b) const
Definition: templateUtil.h:227
Definition: templateUtil.h:225
#define FAST_REPEATED_CALL(count, x)
Definition: templateUtil.h:152
SortPointersByFunction_T(MemberFnPtr memFun)
Definition: templateUtil.h:432
EqualPointerByFunction_T(MemberFnPtr memFun, Ret value)
Definition: templateUtil.h:461
MemberFnPtr myPtr
Definition: templateUtil.h:430
Definition: templateUtil.h:427
void DtDeletePointersInIterators(Iter begin, Iter end)
Definition: templateUtil.h:216
void DtClearAndDeletePointersInVector(V &vec)
Deletes all pointers in a vector then clears it.
Definition: templateUtil.h:201
SortPointersByFunction_T(const SortPointersByFunction_T< Ret, T > &second)
Definition: templateUtil.h:437
iter fastFindPred(size_t size, iter begin, iter end, const F &pred)
Definition: templateUtil.h:321
MemberFnPtr myPtr
Definition: templateUtil.h:458
void fastEraseFromUnsortedVectorPred(std::vector< T, Ax > &list, const Function &f)
Definition: templateUtil.h:383
EqualPointerByFunction_T(const SortPointersByFunction_T< Ret, T > &second)
Definition: templateUtil.h:467
Ret(T::* MemberFnPtr)() const
Definition: templateUtil.h:429
void forEachCallMember(T *_this, R(T::*memfun)(A), Col &col)
Definition: templateUtil.h:235
The DtDe is the core component of any VR-Vantage application. It owns a DtRenderer, DtDeCommunicator, as well as a DtDisplay and other things.
Definition: DtDe.h:72
iter fasterFind(size_t size, iter begin, iter end, const T &value)
boost::signalslib::signal&lt;void (Argument)&gt;&amp; forwarder)
Definition: templateUtil.h:314
void fastEraseFromUnsortedVector(std::vector< T, Ax > &list, const T &value)
Definition: templateUtil.h:328
Ret(T::* MemberFnPtr)() const
Definition: templateUtil.h:457
void set_split_difference(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, InputIterator2 last2, OutputIterator1 result1, OutputIterator2 result2)
Definition: templateUtil.h:262

Document ID: Generated on Tue Sep 24 19:28:17 EDT 2024 from SVN revision 269799
Copyright © 2005-2024 MAK Technologies. All Rights Reserved (www.mak.com)