operators.hh

00001 /* vim: set sw=4 sts=4 et foldmethod=syntax : */
00002 
00003 /*
00004  * Copyright (c) 2007 Ciaran McCreesh
00005  *
00006  * This file is part of the Paludis package manager. Paludis is free software;
00007  * you can redistribute it and/or modify it under the terms of the GNU General
00008  * Public License version 2, as published by the Free Software Foundation.
00009  *
00010  * Paludis is distributed in the hope that it will be useful, but WITHOUT ANY
00011  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
00012  * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
00013  * details.
00014  *
00015  * You should have received a copy of the GNU General Public License along with
00016  * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
00017  * Place, Suite 330, Boston, MA  02111-1307  USA
00018  */
00019 
00020 #ifndef PALUDIS_GUARD_PALUDIS_UTIL_OPERATORS_HH
00021 #define PALUDIS_GUARD_PALUDIS_UTIL_OPERATORS_HH 1
00022 
00023 #include <paludis/util/attributes.hh>
00024 
00025 /**
00026  * Various classes that simplify implementing operators for classes.
00027  *
00028  * \ingroup g_oo
00029  *
00030  * \section Examples
00031  *
00032  * - None at this time.
00033  */
00034 
00035 namespace paludis
00036 {
00037     /**
00038      * Classes with a base in this namespace define comparison operators via
00039      * operator==.
00040      *
00041      * \ingroup g_oo
00042      */
00043     namespace equality_operators
00044     {
00045         /**
00046          * Classes inheriting this define relational operators via
00047          * operator==.
00048          *
00049          * \ingroup g_oo
00050          */
00051         struct HasEqualityOperators
00052         {
00053             /**
00054              * Template voodoo to assert that we're not trying to call operators
00055              * on something we're not supposed to.
00056              */
00057             template <typename T_>
00058             struct AssertHasEqualityOperators
00059             {
00060                 typedef T_ Type;
00061             };
00062         };
00063 
00064         template <typename T1_>
00065         inline bool operator!= (
00066                 const T1_ & a,
00067                 const typename T1_::template AssertHasEqualityOperators<T1_>::Type & b) PALUDIS_ATTRIBUTE((warn_unused_result));
00068 
00069         template <typename T1_>
00070         inline bool operator!= (
00071                 const T1_ & a,
00072                 const typename T1_::template AssertHasEqualityOperators<T1_>::Type & b)
00073         {
00074             return ! (a == b);
00075         }
00076     }
00077 
00078     /**
00079      * Classes with a base in this namespace define comparison operators via
00080      * operator< and operator==.
00081      *
00082      * \ingroup g_oo
00083      */
00084     namespace relational_operators
00085     {
00086         /**
00087          * Classes inheriting this define relational operators via
00088          * operator< and operator==.
00089          *
00090          * \ingroup g_oo
00091          */
00092         struct HasRelationalOperators :
00093             public equality_operators::HasEqualityOperators
00094         {
00095             /**
00096              * Template voodoo to assert that we're not trying to call operators
00097              * on something we're not supposed to.
00098              */
00099             template <typename T_>
00100             struct AssertHasRelationalOperators
00101             {
00102                 typedef T_ Type;
00103             };
00104         };
00105 
00106         template <typename T1_>
00107         inline bool operator> (
00108                 const T1_ & a,
00109                 const typename T1_::template AssertHasRelationalOperators<T1_>::Type & b) PALUDIS_ATTRIBUTE((warn_unused_result));
00110 
00111         template <typename T1_>
00112         inline bool operator> (
00113                 const T1_ & a,
00114                 const typename T1_::template AssertHasRelationalOperators<T1_>::Type & b)
00115         {
00116             return b < a;
00117         }
00118 
00119         template <typename T1_>
00120         inline bool operator<= (
00121                 const T1_ & a,
00122                 const typename T1_::template AssertHasRelationalOperators<T1_>::Type & b) PALUDIS_ATTRIBUTE((warn_unused_result));
00123 
00124         template <typename T1_>
00125         inline bool operator<= (
00126                 const T1_ & a,
00127                 const typename T1_::template AssertHasRelationalOperators<T1_>::Type & b)
00128         {
00129             return ! (b < a);
00130         }
00131 
00132         template <typename T1_>
00133         inline bool operator>= (
00134                 const T1_ & a,
00135                 const typename T1_::template AssertHasRelationalOperators<T1_>::Type & b) PALUDIS_ATTRIBUTE((warn_unused_result));
00136 
00137         template <typename T1_>
00138         inline bool operator>= (
00139                 const T1_ & a,
00140                 const typename T1_::template AssertHasRelationalOperators<T1_>::Type & b)
00141         {
00142             return ! (a < b);
00143         }
00144     }
00145 
00146     /**
00147      * Classes with a base in this namespace define arithmetic operators via
00148      * operator+= etc.
00149      *
00150      * \ingroup g_oo
00151      */
00152     namespace arithmetic_operators
00153     {
00154         /**
00155          * Classes inheriting this define arithmetic operators via
00156          * via operator+= etc.
00157          *
00158          * \ingroup g_oo
00159          */
00160         struct HasArithmeticOperators
00161         {
00162             /**
00163              * Template voodoo to assert that we're not trying to call operators
00164              * on something we're not supposed to.
00165              */
00166             template <typename T_>
00167             struct AssertHasArithmeticOperators
00168             {
00169                 typedef T_ Type;
00170             };
00171         };
00172 
00173         template <typename T1_>
00174         inline T1_ operator+ (
00175                 const T1_ & a,
00176                 const typename T1_::template AssertHasArithmeticOperators<T1_>::Type & b) PALUDIS_ATTRIBUTE((warn_unused_result));
00177 
00178         template <typename T1_>
00179         inline T1_ operator+ (
00180                 const T1_ & a,
00181                 const typename T1_::template AssertHasArithmeticOperators<T1_>::Type & b)
00182         {
00183             T1_ result(a);
00184             result += b;
00185             return result;
00186         }
00187 
00188         template <typename T1_>
00189         inline T1_ operator- (
00190                 const T1_ & a,
00191                 const typename T1_::template AssertHasArithmeticOperators<T1_>::Type & b) PALUDIS_ATTRIBUTE((warn_unused_result));
00192 
00193         template <typename T1_>
00194         inline T1_ operator- (
00195                 const T1_ & a,
00196                 const typename T1_::template AssertHasArithmeticOperators<T1_>::Type & b)
00197         {
00198             T1_ result(a);
00199             result -= b;
00200             return result;
00201         }
00202 
00203         template <typename T1_>
00204         inline T1_ operator* (
00205                 const T1_ & a,
00206                 const typename T1_::template AssertHasArithmeticOperators<T1_>::Type & b) PALUDIS_ATTRIBUTE((warn_unused_result));
00207 
00208         template <typename T1_>
00209         inline T1_ operator* (
00210                 const T1_ & a,
00211                 const typename T1_::template AssertHasArithmeticOperators<T1_>::Type & b)
00212         {
00213             T1_ result(a);
00214             result *= b;
00215             return result;
00216         }
00217 
00218         template <typename T1_>
00219         inline T1_ operator/ (
00220                 const T1_ & a,
00221                 const typename T1_::template AssertHasArithmeticOperators<T1_>::Type & b) PALUDIS_ATTRIBUTE((warn_unused_result));
00222 
00223         template <typename T1_>
00224         inline T1_ operator/ (
00225                 const T1_ & a,
00226                 const typename T1_::template AssertHasArithmeticOperators<T1_>::Type & b)
00227         {
00228             T1_ result(a);
00229             result /= b;
00230             return result;
00231         }
00232     }
00233 }
00234 
00235 #endif

Generated on Mon Sep 21 10:36:08 2009 for paludis by  doxygen 1.5.4