RefleX
Build DDS Applications in Modern C++ without IDL
 All Classes Namespaces Files Functions Variables Typedefs Enumerator Friends Macros Pages
enable_if.h
Go to the documentation of this file.
1 /*********************************************************************************************
2 (c) 2005-2014 Copyright, Real-Time Innovations, Inc. All rights reserved.
3 RTI grants Licensee a license to use, modify, compile, and create derivative works
4 of the Software. Licensee has the right to distribute object form only for use with RTI
5 products. The Software is provided "as is", with no warranty of any type, including
6 any warranty for fitness for any purpose. RTI is under no obligation to maintain or
7 support the Software. RTI shall not be liable for any incidental or consequential
8 damages arising out of the use or inability to use the software.
9 **********************************************************************************************/
10 
11 #ifndef RTIREFLEX_ENABLE_IF_H
12 #define RTIREFLEX_ENABLE_IF_H
13 
14 #include <tuple>
15 #include <vector>
16 #include <list>
17 #include <set>
18 #include <map>
19 #include <string>
20 #include <array>
21 #include <type_traits>
22 #include <boost/range.hpp>
23 #include <boost/range/any_range.hpp>
24 
25 #include <boost/fusion/sequence/intrinsic/at.hpp>
26 #include <boost/fusion/include/at.hpp>
27 #include <boost/fusion/sequence/intrinsic/size.hpp>
28 #include <boost/fusion/include/size.hpp>
29 #include <boost/optional.hpp>
30 
31 #ifdef RTI_WIN32
32 #include <stdint.h>
33 #endif
34 
35 #if ((RTI_DDS_VERSION_MAJOR==5) && (RTI_DDS_VERSION_MINOR==1))
36 
37 #ifndef REFLEX_STATIC_STRING_BOUND
38  #define REFLEX_STATIC_STRING_BOUND 256
39 #endif
40 
41 #ifndef REFLEX_STATIC_SEQUENCE_BOUND
42  #define REFLEX_STATIC_SEQUENCE_BOUND 256
43 #endif
44 
45 #elif ((RTI_DDS_VERSION_MAJOR==5) && (RTI_DDS_VERSION_MINOR==2))
46 
47 #ifndef REFLEX_STATIC_STRING_BOUND
48  #define REFLEX_STATIC_STRING_BOUND 256
49 #endif
50 
51 #ifndef REFLEX_STATIC_SEQUENCE_BOUND
52  #define REFLEX_STATIC_SEQUENCE_BOUND 256
53 #endif
54 
55 #endif // RTI_DDS_VERSION_MAJOR
56 
57 
58 
59 namespace reflex {
60 
61  namespace match {
62 
63  typedef unsigned char octet_t;
64 
65  template <class TagType, class... Cases>
66  struct Union;
67 
68  template <class... T>
69  struct Sparse;
70 
71 #ifdef RTI_WIN32
72 
73  template <class T>
74  struct Range
75  : boost::any_range <
76  T,
77  boost::forward_traversal_tag,
78  T, std::ptrdiff_t >
79  {
80  template <class... U>
81  Range(U && ... u)
82  : boost::any_range <
83  T,
84  boost::forward_traversal_tag,
85  T, std::ptrdiff_t >(std::forward<U>(u)...)
86  {}
87  };
88 
89 #else
90 
91  template <class T>
92  using Range =
93  boost::any_range<T,
94  boost::forward_traversal_tag,
95  T, std::ptrdiff_t>;
96 
97 #endif
98 
99  template <class T, size_t Bound>
100  struct BoundedRange : Range<T>
101  {
102  template <class... U>
103  BoundedRange(U&&... u)
104  : Range<T>(std::forward<U>(u)...)
105  {}
106  };
107 
108  } // namespace match
109 
113  namespace meta {
114 
115  struct true_type {
116  enum { value = true };
117  };
118 
119  struct false_type {
120  enum { value = false };
121  };
122 
123  } // namespace meta
124 
129  namespace codegen {
130 
131  template <class T>
132  struct EnumDef
133  {
134  enum { is_enum = 0 };
135  };
136 
154  template <class T>
155  void enum_cast(T & dst, DDS_Long src)
156  {
157  dst = static_cast<T>(src); // cast required for enums
158  }
159 
160 
161  } // namespace codegen
162 
163 
203  namespace type_traits {
204 
207 
208  template <class T>
209  struct is_bool : false_type {};
210 
211  template <>
212  struct is_bool<bool> : true_type{};
213 
214  template <class T>
215  struct is_primitive : false_type {}; // enum
216 
217  template <> struct is_primitive<match::octet_t> : true_type{};
218  template <> struct is_primitive<bool> : true_type{};
219  template <> struct is_primitive<char> : true_type{};
220  template <> struct is_primitive<signed char> : true_type{};
221  template <> struct is_primitive<int16_t> : true_type{};
222  template <> struct is_primitive<uint16_t> : true_type{};
223  template <> struct is_primitive<int32_t> : true_type{};
224  template <> struct is_primitive<uint32_t> : true_type{};
225  template <> struct is_primitive<int64_t> : true_type{};
226  template <> struct is_primitive<uint64_t> : true_type{};
227  template <> struct is_primitive<float> : true_type{};
228  template <> struct is_primitive<double> : true_type{};
229  template <> struct is_primitive<long double> : true_type{};
230 #ifndef RTI_WIN32
231  template <> struct is_primitive<char32_t> : true_type{};
232 #endif
233 #if __x86_64__
234  template <> struct is_primitive<long long> : true_type{};
235  template <> struct is_primitive<unsigned long long> : true_type{};
236 #endif
237 
238  template <class... Args>
239  struct is_primitive<std::tuple<Args...>> : false_type{};
240 
241  template <>
242  struct is_primitive<std::string> : false_type{};
243 
244  template <class T>
245  struct is_primitive<std::vector<T>> : false_type{};
246 
247  template <class TagType, class... Cases>
248  struct is_primitive<reflex::match::Union<TagType, Cases...>> : false_type{};
249 
250  template <class T>
251  struct is_enum
252  {
254  };
255 
256  template <class T>
258  {
260  };
261 
262  template <class T>
264  {
266  };
267 
268  template <class T>
270  {
272  };
273 
274  template <class T>
275  struct is_vector : false_type {};
276 
277  template <class T>
278  struct is_vector<std::vector<T>> : true_type{};
279 
280  template <class T>
281  struct is_vector<const std::vector<T>> : true_type{};
282 
283  template <class T>
284  struct is_string : false_type {};
285 
286  template <>
287  struct is_string<std::string> : true_type{};
288 
289  template <>
290  struct is_string<const std::string> : true_type{};
291 
292  template <class T>
293  struct is_tuple : false_type {};
294 
295  template <typename... Args>
296  struct is_tuple<std::tuple<Args...>> : true_type{};
297 
298  template <typename... Args>
299  struct is_tuple<const std::tuple<Args...>> : true_type{};
300 
301  template <typename T>
302  struct is_stdarray : false_type {};
303 
304  template <typename T, size_t N>
305  struct is_stdarray<std::array<T, N>> : true_type{};
306 
307  template <typename T, size_t N>
308  struct is_stdarray<const std::array<T, N>> : true_type{};
309 
310  template <class C>
311  struct is_stdset : false_type {};
312 
313  template <class T, class Comp, class Alloc>
314  struct is_stdset<std::set<T, Comp, Alloc>> : true_type{};
315 
316  template <class T, class Comp, class Alloc>
317  struct is_stdset<const std::set<T, Comp, Alloc>> : true_type{};
318 
319  template <class C>
320  struct is_stdmap : false_type {};
321 
322  template <class Key, class T, class Comp, class Alloc>
323  struct is_stdmap<std::map<Key, T, Comp, Alloc>> : true_type{};
324 
325  template <class Key, class T, class Comp, class Alloc>
326  struct is_stdmap<const std::map<Key, T, Comp, Alloc>> : true_type{};
327 
328  template <class T>
330 
331  template <class T, size_t Dim>
332  struct is_builtin_array<T[Dim]> : true_type{};
333 
334  template <class T, size_t Dim>
335  struct is_builtin_array<const T[Dim]> : true_type{};
336 
337  template <class T>
338  struct is_builtin_array<T []> : true_type{};
339 
340  template <class T>
341  struct is_builtin_array<const T []> : true_type{};
342 
343  template <class T>
344  struct is_pointer : false_type {};
345 
346  template <class T>
347  struct is_pointer<T *> : true_type{};
348 
349  template <class T>
350  struct is_pointer<const T *> : true_type{};
351 
352  template <class T>
354 
355  template <class T, class Alloc>
356  struct is_container<std::vector<T, Alloc>> : true_type{};
357 
358  template <class T, class Alloc>
359  struct is_container<const std::vector<T, Alloc>> : true_type{};
360 
361  template <class T, class Alloc>
362  struct is_container<std::list<T, Alloc>> : true_type{};
363 
364  template <class T, class Alloc>
365  struct is_container<const std::list<T, Alloc>> : true_type{};
366 
367  template <class Key, class Comp, class Alloc>
368  struct is_container<std::set<Key, Comp, Alloc>> : true_type{};
369 
370  template <class Key, class Comp, class Alloc>
371  struct is_container<const std::set<Key, Comp, Alloc>> : true_type{};
372 
373  template <class Key, class T, class Comp, class Alloc>
374  struct is_container<std::map<Key, T, Comp, Alloc>> : true_type{};
375 
376  template <class Key, class T, class Comp, class Alloc>
377  struct is_container<const std::map<Key, T, Comp, Alloc>> : true_type{};
378 
379  template <class T>
380  struct is_union : false_type {};
381 
382  template <class TagType, class... Args>
383  struct is_union<reflex::match::Union<TagType, Args...>> : true_type{};
384 
385  template <class TagType, class... Args>
386  struct is_union<const reflex::match::Union<TagType, Args...>> : true_type{};
387 
388  template <class T>
389  struct is_sparse : false_type {};
390 
391  template <class... Args>
392  struct is_sparse<reflex::match::Sparse<Args...>> : true_type{};
393 
394  template <class... Args>
395  struct is_sparse<const reflex::match::Sparse<Args...>> : true_type{};
396 
397  template <class T>
398  struct is_range : false_type {};
399 
400  template <class T>
401  struct is_range<reflex::match::Range<T>> : true_type{};
402 
403  template <class T>
404  struct is_range<const reflex::match::Range<T>> : true_type{};
405 
406  template <class T, size_t N>
407  struct is_range<reflex::match::BoundedRange<T, N>> : true_type{};
408 
409  template <class T, size_t N>
410  struct is_range<const reflex::match::BoundedRange<T, N>> : true_type{};
411 
412  template <class T>
413  struct is_optional : false_type {};
414 
415 #ifdef RTI_WIN32
416  template <class... T>
417  struct is_optional<boost::optional<T...>> : true_type{};
418 #else
419  template <class T>
420  struct is_optional<boost::optional<T>> : true_type{};
421 #endif
422 
423  template <class C, bool>
425 
426  template <class C>
427  struct container_traits_impl<C, true>
428  {
429  typedef typename C::value_type value_type;
430  typedef typename C::iterator iterator;
431  };
432 
433  template <class C>
434  struct container_traits_impl<C, false>
435  {
436  typedef void value_type;
437  typedef void iterator;
438  };
439 
440  template <class C>
442  {
443  typedef typename
445  C,
448 
449  typedef typename
451  C,
454  };
455 
456  template <class T>
458 
459  template <class T> struct is_char_ptr : false_type {};
460  template <> struct is_char_ptr<char *> : true_type{};
461  template <> struct is_char_ptr<const char *> : true_type{};
462  template <> struct is_char_ptr<char * const> : true_type{};
463  template <> struct is_char_ptr<const char * const> : true_type{};
464 
465  template <class T>
468  };
469 
470  struct default_bound { };
471 
472  template <class T, class Parent = void, int Index = -1>
474  {
475  static const unsigned int value = REFLEX_STATIC_STRING_BOUND;
476  };
477 
478  template <class T, class Parent = void, int Index = -1>
480  {
481  static const unsigned int value = REFLEX_STATIC_SEQUENCE_BOUND;
482  };
483 
484  /*template <class T>
485  struct is_default_member_names {
486  enum { value = std::is_base_of<default_bound, T>::value };
487  };*/
488  } // namespace type_traits
489 
490  namespace meta {
491 
492  template <bool, class T = void>
493  struct enable_if {
494  typedef T type;
495  };
496 
497  template <class T>
498  struct enable_if<false, T> {
499  };
500 
501  template <bool, class T = void>
502  struct disable_if {
503  typedef T type;
504  };
505 
506  template <class T>
507  struct disable_if<true, T> {};
508 
509  template <bool, class T = void>
511  typedef typename T::type type;
512  };
513 
514  template <class T>
515  struct disable_if_lazy<true, T> {};
516 
517  template <size_t... Dims>
518  struct dim_list {
519  enum { size = sizeof...(Dims) };
520  };
521 
522  template <size_t Arg, class DimList>
523  struct dim_cat;
524 
525  template <size_t Head, size_t... Args>
526  struct dim_cat<Head, dim_list<Args...>> {
527  typedef dim_list<Head, Args...> type;
528  };
529 
530  template <size_t Head>
531  struct dim_cat<Head, dim_list<0>> {
533  };
534 
535  template <class>
536  struct make_dim_list {
537  typedef dim_list<0> type;
538  };
539 
540  template <class T>
541  struct make_dim_list<T []>;
542 
544 
545  template <class T>
546  struct make_dim_list<T[0]> {
548  };
549 
550  template <class T>
551  struct make_dim_list<std::array<T, 0>> {
553  };
554 
555  template <class T, size_t Dim>
556  struct make_dim_list<T[Dim]> {
557  typedef typename
558  dim_cat<Dim,
560  };
561 
562  template <class T, size_t Dim>
563  struct make_dim_list<std::array<T, Dim>> {
564  typedef typename
565  dim_cat<Dim,
567  };
568 
569  template <class T>
571  typedef T type;
572  };
573 
574  template <class T>
575  struct remove_reference<T &> {
576  typedef T type;
577  };
578 
579  template <class T>
580  struct remove_const {
581  typedef T type;
582  };
583 
584  template <class T>
585  struct remove_const<const T> {
586  typedef T type;
587  };
588 
589  template<class T>
591  typedef T type;
592  };
593 
594  template<class T>
595  struct remove_all_extents<T []> {
597  };
598 
599  template<class T, size_t N>
600  struct remove_all_extents<T[N]> {
602  };
603 
604  template <class T, size_t N>
605  struct remove_all_extents<std::array<T, N>> {
607  };
608 
609  template <class T, class... U>
610  struct packhead {
611  typedef T type;
612  };
613 
614  template <class T>
615  struct packhead<T> {
616  typedef T type;
617  };
618 
619  template <size_t Head, size_t... Tail>
620  struct multiply {
621  static const size_t value =
622  Head * multiply<Tail...>::value;
623  };
624 
625  template <size_t N>
626  struct multiply<N> {
627  static const uint64_t value = N;
628  };
629 
630  template <class>
632 
633  template <size_t... Dims>
634  struct dim_list_multiply<dim_list<Dims...>> {
635  static const uint64_t value = multiply<Dims...>::value;
636  };
637 
638  template <class FusionSeq, size_t I>
639  struct at
640  {
641  typedef typename
642  boost::fusion::result_of::at_c<FusionSeq, I>::type type;
643  };
644 
645  template <class... Args, size_t I>
646  struct at<std::tuple<Args...>, I>
647  {
648  typedef typename std::tuple_element<I, std::tuple<Args...>>::type type;
649  };
650 
651  template <class... Args, size_t I>
652  struct at<const std::tuple<Args...>, I>
653  {
654  // This is crazy!: I can't pass const tuple to std::tuple_element.
655  // I've to attach const to the result of tuple_element.
656  // This is unlike Boost Fusion Sequences up above.
657  //
658  // I'm not sure if this will even work for references
659  // because const references is redundant because references never change.
660  typedef typename
661  std::tuple_element<I, std::tuple<Args...>>::type const type;
662  };
663 
664  template <class First, class Second, size_t I>
665  struct at<std::pair<First, Second>, I>
666  {
667  typedef typename
668  std::tuple_element<I, std::pair<First, Second>>::type type;
669  };
670 
671  template <class First, class Second, size_t I>
672  struct at<const std::pair<First, Second>, I>
673  {
674  typedef typename
675  std::tuple_element<I, std::pair<First, Second>>::type const type;
676  };
677 
678  template <class FusionSeq>
679  struct size
680  {
681  enum {
682  value =
683  boost::fusion::result_of::size<FusionSeq>::type::value
684  };
685  };
686 
687  template <class First, class Second>
688  struct size<std::pair<First, Second>>
689  {
690  enum { value = 2 };
691  };
692 
693  template <class First, class Second>
694  struct size<const std::pair<First, Second>>
695  {
696  enum { value = 2 };
697  };
698 
699  template <class... Args>
700  struct size<std::tuple<Args...>>
701  {
702  enum { value = std::tuple_size<std::tuple<Args...>>::value };
703  };
704 
705  template <class... Args>
706  struct size<const std::tuple<Args...>>
707  {
708  enum { value = std::tuple_size<const std::tuple<Args...>>::value };
709  };
710 
711  template <size_t I, class FusionSeq>
713  at<FusionSeq, I >> ::type
714  get(FusionSeq & seq)
715  {
716  return boost::fusion::at_c<I>(seq);
717  }
718 
719  template <size_t I, class FusionSeq>
720  typename disable_if_lazy <reflex::type_traits::is_tuple<FusionSeq>::value,
721  at<const FusionSeq, I >> ::type
722  get(const FusionSeq & seq)
723  {
724  return boost::fusion::at_c<I>(seq);
725  }
726 
727  template <size_t I, class... Args>
728  // Note reference (ref) at the end.
729  typename at<std::tuple<Args...>, I>::type &
730  get(std::tuple<Args...> & tuple)
731  {
732  return std::get<I>(tuple);
733  }
734 
735  template <size_t I, class... Args>
736  // Note reference (ref) at the end.
737  typename at<const std::tuple<Args...>, I>::type &
738  get(const std::tuple<Args...> & tuple)
739  {
740  return std::get<I>(tuple);
741  }
742 
743  template <size_t I, class First, class Second>
744  // Note reference (ref) at the end.
745  typename at<std::pair<First, Second>, I>::type &
746  get(std::pair<First, Second> & pair)
747  {
748  return std::get<I>(pair);
749  }
750 
751  template <size_t I, class First, class Second>
752  // Note reference (ref) at the end.
753  typename at<const std::pair<First, Second>, I>::type &
754  get(const std::pair<First, Second> & pair)
755  {
756  return std::get<I>(pair);
757  }
758 
759  template <class T>
760  struct remove_refs
761  {
762  typedef T type;
763  };
764 
765  template <class... Args>
766  struct remove_refs<std::tuple<Args...>>
767  {
768  typedef std::tuple<typename remove_reference<Args>::type...> type;
769  };
770 
771  template <class... Args>
772  struct remove_refs<const std::tuple<Args...>>
773  {
774  typedef const std::tuple<typename remove_reference<Args>::type...> type;
775  };
776 
777 
778  } // namespace meta
779 
780  namespace match {
781 
782  template <class T, size_t I, size_t... J>
784  {
785  // using Nested = typename MultiDimArray<T, J...>::type;
786  typedef typename MultiDimArray<T, J...>::type Nested;
787  // using type = std::array<Nested, I>;
788  typedef std::array<Nested, I> type;
789  };
790 
791  template <class T, size_t I>
792  struct MultiDimArray<T, I>
793  {
794  // using type = std::array<T, I>;
795  typedef std::array<T, I> type;
796  };
797 
798  } // namespace match
799 } // namespace reflex
800 
801 #endif // RTIREFLEX_ENABLE_IF_H
802 
Definition: enable_if.h:413
Definition: disc_union.h:139
std::array< Nested, I > type
Definition: enable_if.h:788
remove_all_extents< T >::type type
Definition: enable_if.h:606
remove_all_extents< T >::type type
Definition: enable_if.h:596
static const unsigned int value
Definition: enable_if.h:475
T type
Definition: enable_if.h:591
Array_Dimensions_Must_Be_Greater_Than_Zero type
Definition: enable_if.h:552
Definition: enable_if.h:610
Definition: disc_union.h:27
C::value_type value_type
Definition: enable_if.h:429
Definition: enable_if.h:329
Definition: enable_if.h:441
Definition: enable_if.h:590
Definition: enable_if.h:620
T type
Definition: enable_if.h:581
Definition: enable_if.h:115
boost::any_range< T, boost::forward_traversal_tag, T, std::ptrdiff_t > Range
Definition: enable_if.h:95
Definition: enable_if.h:783
Definition: enable_if.h:679
Definition: enable_if.h:263
Definition: enable_if.h:502
dim_list< Head > type
Definition: enable_if.h:532
void enum_cast(T &dst, DDS_Long src)
Casts integers to user-defined enumeration types.
Definition: enable_if.h:155
#define REFLEX_STATIC_STRING_BOUND
Static bound for string types. Default 256.
Definition: adapt_macros.h:23
Definition: enable_if.h:275
Definition: enable_if.h:580
Definition: enable_if.h:510
Definition: enable_if.h:132
dim_list< Head, Args...> type
Definition: enable_if.h:527
Definition: enable_if.h:311
Definition: enable_if.h:344
dim_list< 0 > type
Definition: enable_if.h:537
Definition: enable_if.h:389
std::tuple_element< I, std::pair< First, Second > >::type const type
Definition: enable_if.h:675
T type
Definition: enable_if.h:576
Definition: enable_if.h:380
Definition: enable_if.h:760
dim_cat< Dim, typename make_dim_list< T >::type >::type type
Definition: enable_if.h:559
Definition: enable_if.h:120
Definition: enable_if.h:293
MultiDimArray< T, J...>::type Nested
Definition: enable_if.h:786
BoundedRange(U &&...u)
Definition: enable_if.h:103
Definition: enable_if.h:284
T type
Definition: enable_if.h:762
Definition: enable_if.h:459
Definition: enable_if.h:631
static const unsigned int value
Definition: enable_if.h:481
Definition: enable_if.h:251
Definition: enable_if.h:639
Definition: enable_if.h:493
std::tuple_element< I, std::pair< First, Second > >::type type
Definition: enable_if.h:668
std::tuple_element< I, std::tuple< Args...> >::type type
Definition: enable_if.h:648
Definition: enable_if.h:536
Definition: enable_if.h:215
T type
Definition: enable_if.h:571
Definition: enable_if.h:398
Definition: enable_if.h:116
Definition: enable_if.h:134
const std::tuple< typename remove_reference< Args >::type...> type
Definition: enable_if.h:774
Definition: enable_if.h:353
Definition: enable_if.h:209
T type
Definition: enable_if.h:494
dim_cat< Dim, typename make_dim_list< T >::type >::type type
Definition: enable_if.h:566
T type
Definition: enable_if.h:503
Definition: enable_if.h:466
std::array< T, I > type
Definition: enable_if.h:795
Definition: enable_if.h:457
C::iterator iterator
Definition: enable_if.h:430
Array_Dimensions_Must_Be_Greater_Than_Zero type
Definition: enable_if.h:547
unsigned char octet_t
Definition: dd_extra.h:48
T type
Definition: enable_if.h:616
remove_all_extents< T >::type type
Definition: enable_if.h:601
container_traits_impl< C, is_container< C >::value >::iterator iterator
Definition: enable_if.h:453
std::tuple_element< I, std::tuple< Args...> >::type const type
Definition: enable_if.h:661
reflex::meta::false_type has_base
Definition: enable_if.h:467
boost::fusion::result_of::at_c< FusionSeq, I >::type type
Definition: enable_if.h:642
container_traits_impl< C, is_container< C >::value >::value_type value_type
Definition: enable_if.h:447
Definition: enable_if.h:523
Definition: enable_if.h:473
Definition: enable_if.h:253
std::tuple< typename remove_reference< Args >::type...> type
Definition: enable_if.h:768
Definition: enable_if.h:570
T type
Definition: enable_if.h:611
T type
Definition: enable_if.h:586
Definition: enable_if.h:320
Definition: bounded.h:21
Definition: enable_if.h:518
Definition: enable_if.h:119
Definition: enable_if.h:470
Definition: enable_if.h:302
T::type type
Definition: enable_if.h:511