summaryrefslogtreecommitdiff
path: root/cpp/include/Ice/Comparable.h
blob: 8d9f522221af4a3101e3318cce79248f2ea7ab35 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
// **********************************************************************
//
// Copyright (c) 2003-present ZeroC, Inc. All rights reserved.
//
// This copy of Ice is licensed to you under the terms described in the
// ICE_LICENSE file included in this distribution.
//
// **********************************************************************

#ifndef ICE_COMPARABLE_H
#define ICE_COMPARABLE_H

#include <functional>

namespace Ice
{

/**
 * Compares the contents of two smart pointers.
 * @param lhs The left-hand side.
 * @param rhs The right-hand side.
 * @return True if the contents are equal, false otherwise.
 */
template<typename T, typename U>
inline bool targetEqualTo(const T& lhs, const U& rhs)
{
    if(lhs && rhs)
    {
        return *lhs == *rhs;
    }
    else
    {
        return !lhs && !rhs;
    }
}

/**
 * Compares the contents of two smart pointers.
 * @param lhs The left-hand side.
 * @param rhs The right-hand side.
 * @return True if the left-hand side compares less than the right-hand side, false otherwise.
 */
template<typename T, typename U>
inline bool targetLess(const T& lhs, const U& rhs)
{
    if(lhs && rhs)
    {
        return *lhs < *rhs;
    }
    else
    {
        return !lhs && rhs;
    }
}

/**
 * Compares the contents of two smart pointers.
 * @param lhs The left-hand side.
 * @param rhs The right-hand side.
 * @return True if the left-hand side compares greater than the right-hand side, false otherwise.
 */
template<typename T, typename U>
inline bool targetGreater(const T& lhs, const U& rhs)
{
    return targetLess(rhs, lhs);
}

/**
 * Compares the contents of two smart pointers.
 * @param lhs The left-hand side.
 * @param rhs The right-hand side.
 * @return True if the left-hand side compares less than or equal to the right-hand side, false otherwise.
 */
template<typename T, typename U>
inline bool targetLessEqual(const T& lhs, const U& rhs)
{
    return !targetGreater(lhs, rhs);
}

/**
 * Compares the contents of two smart pointers.
 * @param lhs The left-hand side.
 * @param rhs The right-hand side.
 * @return True if the left-hand side compares greater than or equal to the right-hand side, false otherwise.
 */
template<typename T, typename U>
inline bool targetGreaterEqual(const T& lhs, const U& rhs)
{
    return !targetLess(lhs, rhs);
}

/**
 * Compares the contents of two smart pointers.
 * @param lhs The left-hand side.
 * @param rhs The right-hand side.
 * @return True if the contents are not equal, false otherwise.
 */
template<typename T, typename U>
inline bool targetNotEqualTo(const T& lhs, const U& rhs)
{
    return !targetEqualTo(lhs, rhs);
}

#ifdef ICE_CPP11_MAPPING

/**
 * Functor class that compares the contents of two smart pointers of the given type using the given comparator.
 * \headerfile Ice/Ice.h
 */
template<typename T, template<typename> class Compare>
struct TargetCompare
{
    /**
     * Executes the functor to compare the contents of two smart pointers.
     * @return True if the contents satisfy the given comparator, false otherwise.
     */
    bool operator()(const T& lhs, const T& rhs) const
    {
        if(lhs && rhs)
        {
            return Compare<typename T::element_type>()(*lhs, *rhs);
        }
        else
        {
            return Compare<bool>()(static_cast<const bool>(lhs), static_cast<const bool>(rhs));
        }
    }
};

//
// Relational operators for generated structs and classes
//

/**
 * Relational operator for generated structs and classes.
 * @param lhs The left-hand side.
 * @param rhs The right-hand side.
 * @return True if the left-hand side compares less than the right-hand side, false otherwise.
 */
template<class C, typename = std::enable_if<std::is_member_function_pointer<decltype(&C::ice_tuple)>::value>>
bool operator<(const C& lhs, const C& rhs)
{
   return lhs.ice_tuple() < rhs.ice_tuple();
}

/**
 * Relational operator for generated structs and classes.
 * @param lhs The left-hand side.
 * @param rhs The right-hand side.
 * @return True if the left-hand side compares less than or equal to the right-hand side, false otherwise.
 */
template<class C, typename = std::enable_if<std::is_member_function_pointer<decltype(&C::ice_tuple)>::value>>
bool operator<=(const C& lhs, const C& rhs)
{
   return lhs.ice_tuple() <= rhs.ice_tuple();
}

/**
 * Relational operator for generated structs and classes.
 * @param lhs The left-hand side.
 * @param rhs The right-hand side.
 * @return True if the left-hand side compares greater than the right-hand side, false otherwise.
 */
template<class C, typename = std::enable_if<std::is_member_function_pointer<decltype(&C::ice_tuple)>::value>>
bool operator>(const C& lhs, const C& rhs)
{
   return lhs.ice_tuple() > rhs.ice_tuple();
}

/**
 * Relational operator for generated structs and classes.
 * @param lhs The left-hand side.
 * @param rhs The right-hand side.
 * @return True if the left-hand side compares greater than or equal to the right-hand side, false otherwise.
 */
template<class C, typename = std::enable_if<std::is_member_function_pointer<decltype(&C::ice_tuple)>::value>>
bool operator>=(const C& lhs, const C& rhs)
{
   return lhs.ice_tuple() >= rhs.ice_tuple();
}

/**
 * Relational operator for generated structs and classes.
 * @param lhs The left-hand side.
 * @param rhs The right-hand side.
 * @return True if the left-hand side compares equal to the right-hand side, false otherwise.
 */
template<class C, typename = std::enable_if<std::is_member_function_pointer<decltype(&C::ice_tuple)>::value>>
bool operator==(const C& lhs, const C& rhs)
{
   return lhs.ice_tuple() == rhs.ice_tuple();
}

/**
 * Relational operator for generated structs and classes.
 * @param lhs The left-hand side.
 * @param rhs The right-hand side.
 * @return True if the left-hand side is not equal to the right-hand side, false otherwise.
 */
template<class C, typename = std::enable_if<std::is_member_function_pointer<decltype(&C::ice_tuple)>::value>>
bool operator!=(const C& lhs, const C& rhs)
{
   return lhs.ice_tuple() != rhs.ice_tuple();
}

#endif

}

#endif