paludis  Version 2.2.0
operators.hh
1 /* vim: set sw=4 sts=4 et foldmethod=syntax : */
2 
3 /*
4  * Copyright (c) 2007 Ciaran McCreesh
5  *
6  * This file is part of the Paludis package manager. Paludis is free software;
7  * you can redistribute it and/or modify it under the terms of the GNU General
8  * Public License version 2, as published by the Free Software Foundation.
9  *
10  * Paludis is distributed in the hope that it will be useful, but WITHOUT ANY
11  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
12  * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
13  * details.
14  *
15  * You should have received a copy of the GNU General Public License along with
16  * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
17  * Place, Suite 330, Boston, MA 02111-1307 USA
18  */
19 
20 #ifndef PALUDIS_GUARD_PALUDIS_UTIL_OPERATORS_HH
21 #define PALUDIS_GUARD_PALUDIS_UTIL_OPERATORS_HH 1
22 
24 
25 /**
26  * Various classes that simplify implementing operators for classes.
27  *
28  * \ingroup g_oo
29  *
30  * \section Examples
31  *
32  * - None at this time.
33  */
34 
35 namespace paludis
36 {
37  /**
38  * Classes with a base in this namespace define comparison operators via
39  * operator==.
40  *
41  * \ingroup g_oo
42  */
43  namespace equality_operators
44  {
45  /**
46  * Classes inheriting this define relational operators via
47  * operator==.
48  *
49  * \ingroup g_oo
50  */
52  {
53  /**
54  * Template voodoo to assert that we're not trying to call operators
55  * on something we're not supposed to.
56  */
57  template <typename T_>
59  {
60  typedef T_ Type;
61  };
62  };
63 
64  template <typename T1_>
65  inline bool operator!= (
66  const T1_ & a,
67  const typename T1_::template AssertHasEqualityOperators<T1_>::Type & b) PALUDIS_ATTRIBUTE((warn_unused_result));
68 
69  template <typename T1_>
70  inline bool operator!= (
71  const T1_ & a,
72  const typename T1_::template AssertHasEqualityOperators<T1_>::Type & b)
73  {
74  return ! (a == b);
75  }
76  }
77 
78  /**
79  * Classes with a base in this namespace define comparison operators via
80  * operator< and operator==.
81  *
82  * \ingroup g_oo
83  */
84  namespace relational_operators
85  {
86  /**
87  * Classes inheriting this define relational operators via
88  * operator< and operator==.
89  *
90  * \ingroup g_oo
91  */
94  {
95  /**
96  * Template voodoo to assert that we're not trying to call operators
97  * on something we're not supposed to.
98  */
99  template <typename T_>
101  {
102  typedef T_ Type;
103  };
104  };
105 
106  template <typename T1_>
107  inline bool operator> (
108  const T1_ & a,
109  const typename T1_::template AssertHasRelationalOperators<T1_>::Type & b) PALUDIS_ATTRIBUTE((warn_unused_result));
110 
111  template <typename T1_>
112  inline bool operator> (
113  const T1_ & a,
114  const typename T1_::template AssertHasRelationalOperators<T1_>::Type & b)
115  {
116  return b < a;
117  }
118 
119  template <typename T1_>
120  inline bool operator<= (
121  const T1_ & a,
122  const typename T1_::template AssertHasRelationalOperators<T1_>::Type & b) PALUDIS_ATTRIBUTE((warn_unused_result));
123 
124  template <typename T1_>
125  inline bool operator<= (
126  const T1_ & a,
127  const typename T1_::template AssertHasRelationalOperators<T1_>::Type & b)
128  {
129  return ! (b < a);
130  }
131 
132  template <typename T1_>
133  inline bool operator>= (
134  const T1_ & a,
135  const typename T1_::template AssertHasRelationalOperators<T1_>::Type & b) PALUDIS_ATTRIBUTE((warn_unused_result));
136 
137  template <typename T1_>
138  inline bool operator>= (
139  const T1_ & a,
140  const typename T1_::template AssertHasRelationalOperators<T1_>::Type & b)
141  {
142  return ! (a < b);
143  }
144  }
145 
146  /**
147  * Classes with a base in this namespace define arithmetic operators via
148  * operator+= etc.
149  *
150  * \ingroup g_oo
151  */
152  namespace arithmetic_operators
153  {
154  /**
155  * Classes inheriting this define arithmetic operators via
156  * via operator+= etc.
157  *
158  * \ingroup g_oo
159  */
161  {
162  /**
163  * Template voodoo to assert that we're not trying to call operators
164  * on something we're not supposed to.
165  */
166  template <typename T_>
168  {
169  typedef T_ Type;
170  };
171  };
172 
173  template <typename T1_>
174  inline T1_ operator+ (
175  const T1_ & a,
176  const typename T1_::template AssertHasArithmeticOperators<T1_>::Type & b) PALUDIS_ATTRIBUTE((warn_unused_result));
177 
178  template <typename T1_>
179  inline T1_ operator+ (
180  const T1_ & a,
181  const typename T1_::template AssertHasArithmeticOperators<T1_>::Type & b)
182  {
183  T1_ result(a);
184  result += b;
185  return result;
186  }
187 
188  template <typename T1_>
189  inline T1_ operator- (
190  const T1_ & a,
191  const typename T1_::template AssertHasArithmeticOperators<T1_>::Type & b) PALUDIS_ATTRIBUTE((warn_unused_result));
192 
193  template <typename T1_>
194  inline T1_ operator- (
195  const T1_ & a,
196  const typename T1_::template AssertHasArithmeticOperators<T1_>::Type & b)
197  {
198  T1_ result(a);
199  result -= b;
200  return result;
201  }
202 
203  template <typename T1_>
204  inline T1_ operator* (
205  const T1_ & a,
206  const typename T1_::template AssertHasArithmeticOperators<T1_>::Type & b) PALUDIS_ATTRIBUTE((warn_unused_result));
207 
208  template <typename T1_>
209  inline T1_ operator* (
210  const T1_ & a,
211  const typename T1_::template AssertHasArithmeticOperators<T1_>::Type & b)
212  {
213  T1_ result(a);
214  result *= b;
215  return result;
216  }
217 
218  template <typename T1_>
219  inline T1_ operator/ (
220  const T1_ & a,
221  const typename T1_::template AssertHasArithmeticOperators<T1_>::Type & b) PALUDIS_ATTRIBUTE((warn_unused_result));
222 
223  template <typename T1_>
224  inline T1_ operator/ (
225  const T1_ & a,
226  const typename T1_::template AssertHasArithmeticOperators<T1_>::Type & b)
227  {
228  T1_ result(a);
229  result /= b;
230  return result;
231  }
232  }
233 }
234 
235 #endif
Definition: about_metadata-fwd.hh:23
#define PALUDIS_ATTRIBUTE(x)
Definition: attributes.hh:64