Vaucanson 1.4
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, 2005, 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 # include <algorithm>
00023 
00024 namespace vcsn {
00025 
00026   /*--------------------.
00027   | Structure::contains |
00028   `--------------------*/
00029 
00030   template<typename S, typename T>
00031   bool op_contains(const Structure<S>&, const T&)
00032   {
00033     return false;
00034   }
00035 
00036   /*--------------------.
00037   | Standard comparison |
00038   `--------------------*/
00039 
00040   template<typename S, typename T, typename U>
00041   bool op_eq(const Structure<S>&,
00042              const T& v1,
00043              const U& v2)
00044   {
00045     return v1 == v2;
00046   }
00047 
00048   template<typename S, typename T, typename U>
00049   bool op_lt(const Structure<S>&,
00050              const T& v1,
00051              const U& v2)
00052   {
00053     return v1 < v2;
00054   }
00055 
00056 
00057   template<typename S, typename V, typename T, typename U>
00058   bool op_eq(const Structure<S>&,
00059              const Structure<V>&,
00060              const T& v1,
00061              const U& v2)
00062   {
00063     return v1 == v2;
00064   }
00065 
00066   template<typename S, typename V, typename T, typename U>
00067   bool op_lt(const Structure<S>&,
00068              const Structure<V>&,
00069              const T& v1,
00070              const U& v2)
00071   {
00072     return v1 < v2;
00073   }
00074 
00075   /*------------.
00076   | Conversions |
00077   `------------*/
00078 
00079   template<typename S, typename R, typename T>
00080   R op_convert(const Structure<S> &,
00081                SELECTOR(R), const T& data)
00082   {
00083     return static_cast<R>(data);
00084   }
00085 
00086   template<typename S, typename T>
00087   const T& op_convert(const Structure<S>&,
00088                       SELECTOR(T), const T& from_data)
00089   {
00090     return from_data;
00091   }
00092 
00093   template<typename S, typename T>
00094   const T& op_convert(const Structure<S>& s1, SELECTOR(T),
00095                       const Structure<S>& s2, const T& from_data)
00096   {
00097     precondition(& s1 == & s2);
00098     static_cast<void> (s1); static_cast<void> (s2);
00099     return from_data;
00100   }
00101 
00102   template<typename S, typename T>
00103   const S& op_convert(const Structure<S>&, const Structure<T>&)
00104   {
00105     static_error(no_convertion_operator_available);
00106     return S ();
00107   }
00108 
00109   /*---------------------.
00110   | Default construction |
00111   `---------------------*/
00112 
00113   template<typename S, typename T>
00114   T op_default(const Structure<S>&, SELECTOR(T))
00115   {
00116     return T();
00117   }
00118 
00119   /*-----.
00120   | Swap |
00121   `-----*/
00122 
00123   template<typename S, typename T>
00124   void op_swap(const Structure<S>&,
00125                T& v1,
00126                T& v2)
00127   {
00128     std::swap(v1, v2);
00129   }
00130 
00131   /*-----------.
00132   | Assignment |
00133   `-----------*/
00134 
00135   template<typename S, typename T, typename U>
00136   void
00137   op_assign(const Structure<S>&, T& dst, const U& src)
00138   {
00139     dst = src;
00140   }
00141 
00142   template<typename S, typename T, typename U>
00143   void op_assign(const Structure<S>& s1,
00144                  const Structure<S>& s2,
00145                  T& dst,
00146                  const U& src)
00147   {
00148     precondition(& s1 == & s2);
00149     (void) s2;
00150     op_assign(s1.self(), dst, src);
00151   }
00152 
00153 # define INOP_IMPL(Name)                                \
00154   template<typename S, typename T, typename U>          \
00155   void op_in_ ## Name (const Structure<S>& s1,          \
00156                        const Structure<S>& s2,          \
00157                        T& dst,                          \
00158                        const U& arg)                    \
00159   {                                                     \
00160     precondition(& s1 == & s2);                         \
00161     (void) s2;                                          \
00162     return op_in_ ## Name (s1.self(), dst, arg);        \
00163   }
00164 
00165   INOP_IMPL(add);
00166   INOP_IMPL(sub);
00167   INOP_IMPL(mul);
00168   INOP_IMPL(div);
00169   INOP_IMPL(mod);
00170 # undef INOP_IMPL
00171 
00172 
00173 
00174 # define BINOP_IMPL(Name)                       \
00175   template<typename S, typename T, typename U>  \
00176   T op_ ## Name (const Structure<S>& s1,        \
00177                  const Structure<S>& s2,        \
00178                  const T& v1,                   \
00179                  const U& v2)                   \
00180   {                                             \
00181     precondition(& s1 == & s2);                 \
00182     (void) s2;                                  \
00183     return op_ ## Name(s1.self(), v1, v2);      \
00184   }
00185 
00186   BINOP_IMPL(add);
00187   BINOP_IMPL(sub);
00188   BINOP_IMPL(mul);
00189   BINOP_IMPL(div);
00190   BINOP_IMPL(mod);
00191 # undef BINOP_IMPL
00192 
00193   template<typename S, typename St, typename T>
00194   St& op_rin(const Structure<S>& s, St& st, const T& v)
00195   {
00196     return st >> v;
00197   }
00198 
00199   template<typename S, typename St, typename T>
00200   St& op_rout(const Structure<S>&, St& st, const T& v)
00201   {
00202     st << v;
00203     return st;
00204   }
00205 
00206 } // vcsn
00207 
00208 #endif // ! VCSN_DESIGN_PATTERN_DEFAULT_OPS_HXX