default_ops.hxx

00001 // default_ops.hxx: this file is part of the Vaucanson project.
00002 //
00003 // Vaucanson, a generic library for finite state machines.
00004 //
00005 // Copyright (C) 2001, 2002, 2003, 2004, 2006 The Vaucanson Group.
00006 //
00007 // This program is free software; you can redistribute it and/or
00008 // modify it under the terms of the GNU General Public License
00009 // as published by the Free Software Foundation; either version 2
00010 // of the License, or (at your option) any later version.
00011 //
00012 // The complete GNU General Public Licence Notice can be found as the
00013 // `COPYING' file in the root directory.
00014 //
00015 // The Vaucanson Group consists of people listed in the `AUTHORS' file.
00016 //
00017 #ifndef VCSN_DESIGN_PATTERN_DEFAULT_OPS_HXX
00018 # define VCSN_DESIGN_PATTERN_DEFAULT_OPS_HXX
00019 
00020 # include <vaucanson/design_pattern/default_ops.hh>
00021 # include <vaucanson/misc/contract.hh>
00022 
00023 namespace vcsn {
00024 
00025   /*--------------------.
00026   | Structure::contains |
00027   `--------------------*/
00028 
00029   template<typename S, typename T>
00030   bool op_contains(const Structure<S>&, const T&)
00031   {
00032     return false;
00033   }
00034 
00035   /*--------------------.
00036   | Standard comparison |
00037   `--------------------*/
00038 
00039   template<typename S, typename T, typename U>
00040   bool op_eq(const Structure<S>&,
00041              const T& v1,
00042              const U& v2)
00043   {
00044     return v1 == v2;
00045   }
00046 
00047   template<typename S, typename T, typename U>
00048   bool op_lt(const Structure<S>&,
00049              const T& v1,
00050              const U& v2)
00051   {
00052     return v1 < v2;
00053   }
00054 
00055 
00056   template<typename S, typename V, typename T, typename U>
00057   bool op_eq(const Structure<S>&,
00058              const Structure<V>&,
00059              const T& v1,
00060              const U& v2)
00061   {
00062     return v1 == v2;
00063   }
00064 
00065   template<typename S, typename V, typename T, typename U>
00066   bool op_lt(const Structure<S>&,
00067              const Structure<V>&,
00068              const T& v1,
00069              const U& v2)
00070   {
00071     return v1 < v2;
00072   }
00073 
00074   /*------------.
00075   | Conversions |
00076   `------------*/
00077 
00078   template<typename S, typename R, typename T>
00079   R op_convert(const Structure<S> &,
00080                SELECTOR(R), const T& data)
00081   {
00082     return static_cast<R>(data);
00083   }
00084 
00085   template<typename S, typename T>
00086   const T& op_convert(const Structure<S>&,
00087                       SELECTOR(T), const T& from_data)
00088   {
00089     return from_data;
00090   }
00091 
00092   template<typename S, typename T>
00093   const T& op_convert(const Structure<S>& s1, SELECTOR(T),
00094                       const Structure<S>& s2, const T& from_data)
00095   {
00096     precondition(& s1 == & s2);
00097     return from_data;
00098   }
00099 
00100   template<typename S, typename T>
00101   const S& op_convert(const Structure<S>&, const Structure<T>&)
00102   {
00103     static_error(no_convertion_operator_available);
00104     return S ();
00105   }
00106 
00107   /*---------------------.
00108   | Default construction |
00109   `---------------------*/
00110 
00111   template<typename S, typename T>
00112   T op_default(const Structure<S>&, SELECTOR(T))
00113   {
00114     return T();
00115   }
00116 
00117   /*-----.
00118   | Swap |
00119   `-----*/
00120 
00121   template<typename S, typename T>
00122   void op_swap(const Structure<S>&,
00123                T& v1,
00124                T& v2)
00125   {
00126     std::swap(v1, v2);
00127   }
00128 
00129   /*-----------.
00130   | Assignment |
00131   `-----------*/
00132 
00133 
00134   template<typename S, typename T, typename U>
00135   void op_assign(const Structure<S>&, T& dst, const U& src)
00136   {
00137     dst = src;
00138   }
00139 
00140   template<typename S, typename T, typename U>
00141   void op_assign(const Structure<S>& s1,
00142                  const Structure<S>& s2,
00143                  T& dst,
00144                  const U& src)
00145   {
00146     precondition(& s1 == & s2);
00147     (void) s2;
00148     op_assign(s1.self(), dst, src);
00149   }
00150 
00151 # define INOP_IMPL(Name)                                \
00152   template<typename S, typename T, typename U>          \
00153   void op_in_ ## Name (const Structure<S>& s1,          \
00154                        const Structure<S>& s2,          \
00155                        T& dst,                          \
00156                        const U& arg)                    \
00157   {                                                     \
00158     precondition(& s1 == & s2);                         \
00159     (void) s2;                                          \
00160     return op_in_ ## Name (s1.self(), dst, arg);        \
00161   }
00162 
00163   INOP_IMPL(add);
00164   INOP_IMPL(sub);
00165   INOP_IMPL(mul);
00166   INOP_IMPL(div);
00167   INOP_IMPL(mod);
00168 # undef INOP_IMPL
00169 
00170 
00171 
00172 # define BINOP_IMPL(Name)                       \
00173   template<typename S, typename T, typename U>  \
00174   T op_ ## Name (const Structure<S>& s1,        \
00175                  const Structure<S>& s2,        \
00176                  const T& v1,                   \
00177                  const U& v2)                   \
00178   {                                             \
00179     precondition(& s1 == & s2);                 \
00180     (void) s2;                                  \
00181     return op_ ## Name(s1.self(), v1, v2);      \
00182   }
00183 
00184   BINOP_IMPL(add);
00185   BINOP_IMPL(sub);
00186   BINOP_IMPL(mul);
00187   BINOP_IMPL(div);
00188   BINOP_IMPL(mod);
00189 # undef BINOP_IMPL
00190 
00191   template<typename S, typename St, typename T>
00192   St& op_rin(const Structure<S>& s, St& st, const T& v)
00193   {
00194     return st >> v;
00195   }
00196 
00197   template<typename S, typename St, typename T>
00198   St& op_rout(const Structure<S>&, St& st, const T& v)
00199   {
00200     st << v;
00201     return st;
00202   }
00203 
00204 } // vcsn
00205 
00206 #endif // ! VCSN_DESIGN_PATTERN_DEFAULT_OPS_HXX

Generated on Sat Jul 29 17:12:58 2006 for Vaucanson by  doxygen 1.4.6