Jpp
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
JMultiEquals.hh
Go to the documentation of this file.
1 #ifndef __JLANG__JMULTIEQUALS__
2 #define __JLANG__JMULTIEQUALS__
3 
4 #include "JLang/JNullType.hh"
5 #include "JLang/JTypeList.hh"
6 #include "JLang/JType.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  * Template definition of auxiliary base class for composite data structures
20  * composed of base classes with equality evaluations capabilities.
21  *
22  * Each data type <tt>X</tt> in the type list should have the corresponding operator:
23  * <pre>
24  * bool operator==(const X& first, const X& second);
25  * </pre>
26  * This class uses in-class friend operators (see Barton-Nackman trick).
27  *
28  * This class implements the operators <tt> == != </tt>.
29  */
30  template<class JClass_t, class JTypelist_t>
31  struct JMultiEquals {
32  protected:
33  /**
34  * Equals method for composite data types.
35  *
36  * \param first first object
37  * \param second second object
38  * \param type type
39  * \return true if two objects are equal; else false
40  */
41  template<class JHead_t, class JTail_t>
42  static inline bool eq(const JClass_t& first,
43  const JClass_t& second,
44  const JType<JTypeList<JHead_t, JTail_t> >& type)
45  {
46  return (static_cast<const JHead_t&>(first) ==
47  static_cast<const JHead_t&>(second) &&
48  eq(first, second, JType<JTail_t>()));
49  }
50 
51 
52  /**
53  * Equals method for composite data types.
54  *
55  * \param first first object
56  * \param second second object
57  * \param type type
58  * \return true if two objects are equal; else false
59  */
60  template<class JHead_t>
61  static inline bool eq(const JClass_t& first,
62  const JClass_t& second,
64  {
65  return (static_cast<const JHead_t&>(first) ==
66  static_cast<const JHead_t&>(second));
67  }
68 
69  public:
70  /**
71  * Equal operator.
72  *
73  * \param first first object
74  * \param second second object
75  * \return true if two objects are equal; else false
76  */
77  friend bool operator==(const JClass_t& first,
78  const JClass_t& second)
79  {
80  return eq(first, second, JType<JTypelist_t>());
81  }
82 
83 
84  /**
85  * Not equal operator.
86  *
87  * \param first first object
88  * \param second second object
89  * \return true if two objects are not equal; else false
90  */
91  friend bool operator!=(const JClass_t& first,
92  const JClass_t& second)
93  {
94  return !eq(first, second, JType<JTypelist_t>());
95  }
96  };
97 }
98 
99 #endif
friend bool operator==(const JClass_t &first, const JClass_t &second)
Equal operator.
Definition: JMultiEquals.hh:77
friend bool operator!=(const JClass_t &first, const JClass_t &second)
Not equal operator.
Definition: JMultiEquals.hh:91
Auxiliary class for a type holder.
Definition: JType.hh:19
Template definition of auxiliary base class for composite data structures composed of base classes wi...
Definition: JMultiEquals.hh:31
Type list.
Definition: JTypeList.hh:22
static bool eq(const JClass_t &first, const JClass_t &second, const JType< JTypeList< JHead_t, JTail_t > > &type)
Equals method for composite data types.
Definition: JMultiEquals.hh:42
static bool eq(const JClass_t &first, const JClass_t &second, const JType< JTypeList< JHead_t, JNullType > > &type)
Equals method for composite data types.
Definition: JMultiEquals.hh:61