1 : // Copyright 2012 Google Inc. All Rights Reserved.
2 : //
3 : // Licensed under the Apache License, Version 2.0 (the "License");
4 : // you may not use this file except in compliance with the License.
5 : // You may obtain a copy of the License at
6 : //
7 : // http://www.apache.org/licenses/LICENSE-2.0
8 : //
9 : // Unless required by applicable law or agreed to in writing, software
10 : // distributed under the License is distributed on an "AS IS" BASIS,
11 : // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 : // See the License for the specific language governing permissions and
13 : // limitations under the License.
14 :
15 : #ifndef SYZYGY_COMMON_COMPARABLE_H_
16 : #define SYZYGY_COMMON_COMPARABLE_H_
17 :
18 : namespace common {
19 :
20 : // A 'mixin' class for endowing any class with comparison operators,
21 : // provided it implements a 3-way compare function with signature
22 : // 'int Compare(const T& other) const;'.
23 : template<typename T> class Comparable {
24 : public:
25 E : bool operator==(const T& other) const {
26 E : return reinterpret_cast<const T*>(this)->Compare(other) == 0;
27 E : }
28 :
29 E : bool operator!=(const T& other) const {
30 E : return reinterpret_cast<const T*>(this)->Compare(other) != 0;
31 E : }
32 :
33 E : bool operator<=(const T& other) const {
34 E : return reinterpret_cast<const T*>(this)->Compare(other) <= 0;
35 E : }
36 :
37 E : bool operator<(const T& other) const {
38 E : return reinterpret_cast<const T*>(this)->Compare(other) < 0;
39 E : }
40 :
41 E : bool operator>=(const T& other) const {
42 E : return reinterpret_cast<const T*>(this)->Compare(other) >= 0;
43 E : }
44 :
45 E : bool operator>(const T& other) const {
46 E : return reinterpret_cast<const T*>(this)->Compare(other) > 0;
47 E : }
48 : };
49 :
50 : } // namespace common
51 :
52 : #endif // SYZYGY_COMMON_COMPARABLE_H_
|