Jpp
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
JComparable.hh
Go to the documentation of this file.
1 #ifndef __JLANG__JCOMPARABLE__
2 #define __JLANG__JCOMPARABLE__
3 
4 #include "JLang/JNullType.hh"
5 #include "JLang/JClass.hh"
6 
7 
8 /**
9  * \author mdejong
10  */
11 
12 namespace JLANG {}
13 namespace JPP { using namespace JLANG; }
14 
15 namespace JLANG {
16 
17 
18  /**
19  * Template definition of auxiliary base class for comparison of data structures.
20  *
21  * The various specialisations of this class implement the operators <tt> < > <= >= == != </tt>.
22  */
23  template<class JFirst_t, class JSecond_t = JNullType>
24  struct JComparable;
25 
26 
27  /**
28  * General purpose specialisation of class JComparable for any data type.
29  *
30  * The template parameter should have the corresponding member method:
31  * <pre>
32  * bool less(const JClass_t&) const;
33  * </pre>
34  *
35  * This class uses in-class friend operators (see Barton-Nackman trick).
36  */
37  template<class JClass_t>
38  struct JComparable<JClass_t, JNullType>
39  {
40  /**
41  * Less than operator.
42  *
43  * \param first first object
44  * \param second second object
45  * \return true if first object is less than second object; else false
46  */
47  friend bool operator<(const JClass_t& first,
48  const JClass_t& second)
49  {
50  return first.less(second);
51  }
52 
53 
54  /**
55  * Greater than operator.
56  *
57  * \param first first object
58  * \param second second object
59  * \return true if first object is greater than second object; else false
60  */
61  friend bool operator>(const JClass_t& first,
62  const JClass_t& second)
63  {
64  return second.less(first);
65  }
66 
67 
68  /**
69  * Less than or equal operator.
70  *
71  * \param first first object
72  * \param second second object
73  * \return true if first object is less than or equal to second object; else false
74  */
75  friend bool operator<=(const JClass_t& first,
76  const JClass_t& second)
77  {
78  return !second.less(first);
79  }
80 
81 
82  /**
83  * Greater than or equal operator.
84  *
85  * \param first first object
86  * \param second second object
87  * \return true if first object is greater than or equal to second object; else false
88  */
89  friend bool operator>=(const JClass_t& first,
90  const JClass_t& second)
91  {
92  return !first.less(second);
93  }
94 
95 
96  /**
97  * Equal operator.
98  *
99  * \param first first object
100  * \param second second object
101  * \return true if two objects are equal; else false
102  */
103  friend bool operator==(const JClass_t& first,
104  const JClass_t& second)
105  {
106  return !first.less(second) && !second.less(first);
107  }
108 
109 
110  /**
111  * Not equal operator.
112  *
113  * \param first first object
114  * \param second second object
115  * \return true if two objects are not equal; else false
116  */
117  friend bool operator!=(const JClass_t& first,
118  const JClass_t& second)
119  {
120  return first.less(second) || second.less(first);
121  }
122  };
123 
124 
125  /**
126  * Specialisation of class JComparable for two data types.
127  *
128  * The first template parameter should have the corresponding member methods:
129  * <pre>
130  * bool less(const JFirst_t&) const;
131  * bool less(const JSecond_t&) const;
132  * bool more(const JSecond_t&) const;
133  * </pre>
134  * where <tt>JFirst_t</tt> andd <tt>JSecond_t</tt> refers to the first and second template parameter, respectively.
135  * The second template parameter may be a primitive data type.
136  * This class uses in-class friend operators (see Barton-Nackman trick).
137  */
138  template<class JFirst_t, class JSecond_t>
139  struct JComparable :
140  public JComparable<JFirst_t>
141  {
142  /**
143  * Less than operator.
144  *
145  * \param first first object
146  * \param second second object
147  * \return true if first object is less than second object; else false
148  */
149  friend bool operator<(const JFirst_t& first,
150  typename JClass<JSecond_t>::argument_type second)
151  {
152  return first.less(second);
153  }
154 
155 
156  /**
157  * Less than operator.
158  *
159  * \param first first object
160  * \param second second object
161  * \return true if first object is less than second object; else false
162  */
163  friend bool operator<(typename JClass<JSecond_t>::argument_type first,
164  const JFirst_t& second)
165  {
166  return second.more(first);
167  }
168 
169 
170  /**
171  * Greater than operator.
172  *
173  * \param first first object
174  * \param second second object
175  * \return true if first object is greater than second object; else false
176  */
177  friend bool operator>(const JFirst_t& first,
178  typename JClass<JSecond_t>::argument_type second)
179  {
180  return first.more(second);
181  }
182 
183 
184  /**
185  * Greater than operator.
186  *
187  * \param first first object
188  * \param second second object
189  * \return true if first object is greater than second object; else false
190  */
191  friend bool operator>(typename JClass<JSecond_t>::argument_type first,
192  const JFirst_t& second)
193  {
194  return second.less(first);
195  }
196 
197 
198  /**
199  * Less than or equal operator.
200  *
201  * \param first first object
202  * \param second second object
203  * \return true if first object is less than or equal to second object; else false
204  */
205  friend bool operator<=(const JFirst_t& first,
206  typename JClass<JSecond_t>::argument_type second)
207  {
208  return !first.more(second);
209  }
210 
211 
212  /**
213  * Less than or equal operator.
214  *
215  * \param first first object
216  * \param second second object
217  * \return true if first object is less than or equal to second object; else false
218  */
219  friend bool operator<=(typename JClass<JSecond_t>::argument_type first,
220  const JFirst_t& second)
221  {
222  return second.more(first);
223  }
224 
225 
226  /**
227  * Greater than or equal operator.
228  *
229  * \param first first object
230  * \param second second object
231  * \return true if first object is greater than or equal to second object; else false
232  */
233  friend bool operator>=(const JFirst_t& first,
234  typename JClass<JSecond_t>::argument_type second)
235  {
236  return !first.less(second);
237  }
238 
239 
240  /**
241  * Greater than or equal operator.
242  *
243  * \param first first object
244  * \param second second object
245  * \return true if first object is greater than or equal to second object; else false
246  */
247  friend bool operator>=(typename JClass<JSecond_t>::argument_type first,
248  const JFirst_t& second)
249 
250  {
251  return second.less(first);
252  }
253 
254 
255  /**
256  * Equal operator.
257  *
258  * \param first first object
259  * \param second second object
260  * \return true if two objects are equal; else false
261  */
262  friend bool operator==(const JFirst_t& first,
263  typename JClass<JSecond_t>::argument_type second)
264  {
265  return !first.less(second) && !first.more(second);
266  }
267 
268 
269  /**
270  * Equal operator.
271  *
272  * \param first first object
273  * \param second second object
274  * \return true if two objects are equal; else false
275  */
276  friend bool operator==(typename JClass<JSecond_t>::argument_type first,
277  const JFirst_t& second)
278 
279  {
280  return !second.less(first) && !second.more(first);
281  }
282 
283 
284  /**
285  * Not equal operator.
286  *
287  * \param first first object
288  * \param second second object
289  * \return true if two objects are not equal; else false
290  */
291  friend bool operator!=(const JFirst_t& first,
292  typename JClass<JSecond_t>::argument_type second)
293  {
294  return first.less(second) || first.more(second);
295  }
296 
297 
298  /**
299  * Not equal operator.
300  *
301  * \param first first object
302  * \param second second object
303  * \return true if two objects are not equal; else false
304  */
305  friend bool operator!=(typename JClass<JSecond_t>::argument_type first,
306  const JFirst_t& second)
307  {
308  return second.less(first) || second.more(first);
309  }
310  };
311 }
312 
313 #endif
friend bool operator>(const JClass_t &first, const JClass_t &second)
Greater than operator.
Definition: JComparable.hh:61
friend bool operator==(const JClass_t &first, const JClass_t &second)
Equal operator.
Definition: JComparable.hh:103
friend bool operator!=(const JClass_t &first, const JClass_t &second)
Not equal operator.
Definition: JComparable.hh:117
friend bool operator>(typename JClass< JSecond_t >::argument_type first, const JFirst_t &second)
Greater than operator.
Definition: JComparable.hh:191
friend bool operator<=(const JFirst_t &first, typename JClass< JSecond_t >::argument_type second)
Less than or equal operator.
Definition: JComparable.hh:205
JArgument< T >::argument_type argument_type
Definition: JClass.hh:64
friend bool operator!=(typename JClass< JSecond_t >::argument_type first, const JFirst_t &second)
Not equal operator.
Definition: JComparable.hh:305
friend bool operator>=(const JClass_t &first, const JClass_t &second)
Greater than or equal operator.
Definition: JComparable.hh:89
Auxiliary class for no type definition.
Definition: JNullType.hh:19
friend bool operator!=(const JFirst_t &first, typename JClass< JSecond_t >::argument_type second)
Not equal operator.
Definition: JComparable.hh:291
friend bool operator>(const JFirst_t &first, typename JClass< JSecond_t >::argument_type second)
Greater than operator.
Definition: JComparable.hh:177
Template definition of auxiliary base class for comparison of data structures.
Definition: JComparable.hh:24
friend bool operator==(const JFirst_t &first, typename JClass< JSecond_t >::argument_type second)
Equal operator.
Definition: JComparable.hh:262
friend bool operator<=(const JClass_t &first, const JClass_t &second)
Less than or equal operator.
Definition: JComparable.hh:75
friend bool operator<(const JClass_t &first, const JClass_t &second)
Less than operator.
Definition: JComparable.hh:47
friend bool operator<(const JFirst_t &first, typename JClass< JSecond_t >::argument_type second)
Less than operator.
Definition: JComparable.hh:149
friend bool operator==(typename JClass< JSecond_t >::argument_type first, const JFirst_t &second)
Equal operator.
Definition: JComparable.hh:276
friend bool operator>=(const JFirst_t &first, typename JClass< JSecond_t >::argument_type second)
Greater than or equal operator.
Definition: JComparable.hh:233
friend bool operator>=(typename JClass< JSecond_t >::argument_type first, const JFirst_t &second)
Greater than or equal operator.
Definition: JComparable.hh:247