Jpp  19.1.0
the software that should make you happy
JComparisonAvailable.hh
Go to the documentation of this file.
1 #ifndef __JLANG__JCOMPARISONAVAILABLE__
2 #define __JLANG__JCOMPARISONAVAILABLE__
3 
4 #include <type_traits>
5 
6 #include "JLang/JNullType.hh"
7 
8 
9 /**
10  * \author mdejong
11  */
12 
13 namespace JLANG {}
14 namespace JPP { using namespace JLANG; }
15 
16 namespace JLANG {
17 
18  /**
19  * Local namespace for fallback implementations for comparison operators.
20  */
21  namespace JLANG_LOCAL {
22 
23  template<class T>
24  class JTypedef {
25 
26  template<class U> static auto first_type(U*) -> decltype(std::declval<typename U::first_type>());
27  template<typename> static auto first_type(...) -> std::false_type;
28 
29  template<class U> static auto second_type(U*) -> decltype(std::declval<typename U::second_type>());
30  template<typename> static auto second_type(...) -> std::false_type;
31 
32  template<class U> static auto value_type(U*) -> decltype(std::declval<typename U::value_type>());
33  template<typename> static auto value_type(...) -> std::false_type;
34 
35  public:
36  static const bool has_first_type = !std::is_same<std::false_type, decltype(first_type<T> (0))>::value;
37  static const bool has_second_type = !std::is_same<std::false_type, decltype(second_type<T>(0))>::value;
38  static const bool has_value_type = !std::is_same<std::false_type, decltype(value_type<T> (0))>::value;
39  };
40 
41 
42  /**
43  * Template definition of test availability of comparison operators.
44  */
45  template<class T,
46  bool has_first_and_second_type = JTypedef<T>::has_first_type && JTypedef<T>::has_second_type,
47  bool has_value_type = JTypedef<T>::has_value_type>
49 
50  /**
51  * Template specialisation of test availability of comparison operators of non-composite data types.
52  */
53  template<class T>
54  class JComparisonAvailable<T, false, false>
55  {
56  template<class U> static auto eq(U*) -> decltype(std::declval<U>() == std::declval<U>());
57  template<class U> static auto ne(U*) -> decltype(std::declval<U>() != std::declval<U>());
58  template<class U> static auto lt(U*) -> decltype(std::declval<U>() < std::declval<U>());
59  template<class U> static auto gt(U*) -> decltype(std::declval<U>() > std::declval<U>());
60  template<class U> static auto le(U*) -> decltype(std::declval<U>() <= std::declval<U>());
61  template<class U> static auto ge(U*) -> decltype(std::declval<U>() >= std::declval<U>());
62 
63  template<typename> static auto eq(...) -> std::false_type;
64  template<typename> static auto ne(...) -> std::false_type;
65  template<typename> static auto lt(...) -> std::false_type;
66  template<typename> static auto gt(...) -> std::false_type;
67  template<typename> static auto le(...) -> std::false_type;
68  template<typename> static auto ge(...) -> std::false_type;
69 
70  public:
71  static const bool has_eq = std::is_same<bool, decltype(eq<T>(0))>::value; //!< true if operator== available; else false
72  static const bool has_ne = std::is_same<bool, decltype(ne<T>(0))>::value; //!< true if operator!= available; else false
73  static const bool has_lt = std::is_same<bool, decltype(lt<T>(0))>::value; //!< true if operator< available; else false
74  static const bool has_gt = std::is_same<bool, decltype(gt<T>(0))>::value; //!< true if operator> available; else false
75  static const bool has_le = std::is_same<bool, decltype(le<T>(0))>::value; //!< true if operator<= available; else false
76  static const bool has_ge = std::is_same<bool, decltype(ge<T>(0))>::value; //!< true if operator>= available; else false
77  };
78 
79 
80  /**
81  * Template specialisation of test availability of comparison operators of composite data types which have a type definitions for first_type and second_type.\n
82  * This applies to STL containers such as std::pair.\n
83  * Note that STL provides for the comparison of the container based on comparisons of its elements.
84  */
85  template<class T>
86  class JComparisonAvailable<T, true, false>
87  {
88  typedef typename T::first_type first_type;
89  typedef typename T::second_type second_type;
90 
91  public:
98  };
99 
100 
101  /**
102  * Template specialisation of test availability of comparison operators of composite data types which have a type definition for for value_type.\n
103  * This applies to STL containers such as std::vector and std::set.\n
104  * Note that STL provides for the comparison of the container based on comparisons of its elements.\n
105  */
106  template<class T>
107  class JComparisonAvailable<T, false, true> :
108  public JComparisonAvailable<typename T::value_type>
109  {};
110 
111 
112  /**
113  * Template base class for data structures without equality capability.
114  * This class implements the operators <tt> == != </tt>.
115  */
116  template<class T>
117  class JNoequals {
118  private:
119  /**
120  * Equal operator.
121  *
122  * \param first first object
123  * \param second second object
124  * \return true if two objects are equal; else false
125  */
126  friend inline JNullType operator==(const T& first, const T& second)
127  {
128  return JNullType();
129  }
130 
131 
132  /**
133  * Not equal operator.
134  *
135  * \param first first object
136  * \param second second object
137  * \return true if two objects are not equal; else false
138  */
139  friend inline JNullType operator!=(const T& first, const T& second)
140  {
141  return JNullType();
142  }
143  };
144  }
145 
148 }
149 
150 #endif
static auto le(U *) -> decltype(std::declval< U >()<=std::declval< U >())
static auto gt(U *) -> decltype(std::declval< U >() > std::declval< U >())
static auto ne(U *) -> decltype(std::declval< U >() !=std::declval< U >())
static auto eq(U *) -> decltype(std::declval< U >()==std::declval< U >())
static auto ge(U *) -> decltype(std::declval< U >() >=std::declval< U >())
static auto lt(U *) -> decltype(std::declval< U >()< std::declval< U >())
Template definition of test availability of comparison operators.
Template base class for data structures without equality capability.
friend JNullType operator!=(const T &first, const T &second)
Not equal operator.
friend JNullType operator==(const T &first, const T &second)
Equal operator.
static auto first_type(...) -> std::false_type
static auto second_type(U *) -> decltype(std::declval< typename U::second_type >())
static auto value_type(...) -> std::false_type
static auto second_type(...) -> std::false_type
static auto value_type(U *) -> decltype(std::declval< typename U::value_type >())
static auto first_type(U *) -> decltype(std::declval< typename U::first_type >())
Auxiliary classes and methods for language specific functionality.
This name space includes all other name spaces (except KM3NETDAQ, KM3NET and ANTARES).
Auxiliary class for no type definition.
Definition: JNullType.hh:19