• Main Page
  • Related Pages
  • Modules
  • Namespaces
  • Classes
  • Files
  • File List

equiv.hh

00001 // Copyright (C) 2007, 2008, 2009 EPITA Research and Development Laboratory (LRDE)
00002 //
00003 // This file is part of Olena.
00004 //
00005 // Olena is free software: you can redistribute it and/or modify it under
00006 // the terms of the GNU General Public License as published by the Free
00007 // Software Foundation, version 2 of the License.
00008 //
00009 // Olena is distributed in the hope that it will be useful,
00010 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00011 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012 // General Public License for more details.
00013 //
00014 // You should have received a copy of the GNU General Public License
00015 // along with Olena.  If not, see <http://www.gnu.org/licenses/>.
00016 //
00017 // As a special exception, you may use this file as part of a free
00018 // software project without restriction.  Specifically, if other files
00019 // instantiate templates or use macros or inline functions from this
00020 // file, or you compile this file and link it with other files to produce
00021 // an executable, this file does not by itself cause the resulting
00022 // executable to be covered by the GNU General Public License.  This
00023 // exception does not however invalidate any other reasons why the
00024 // executable file might be covered by the GNU General Public License.
00025 
00026 #ifndef MLN_VALUE_EQUIV_HH
00027 # define MLN_VALUE_EQUIV_HH
00028 
00034 # include <mln/core/concept/value.hh>
00035 
00036 
00037 # define mln_value_equiv(V)  typename mln::value::internal::equiv_<V>::ret
00038 # define mln_value_equiv_(V)          mln::value::internal::equiv_<V>::ret
00039 
00040 
00041 
00042 namespace mln
00043 {
00044 
00045   namespace value
00046   {
00047 
00048     // Fwd decl.
00049     namespace internal { template <typename T> struct equiv_; }
00050 
00051 
00052 
00054     template <typename V>
00055     typename internal::equiv_<V>::ret
00056     equiv(const mln::Value<V>& v);
00057 
00058 
00059 
00060 # ifndef MLN_INCLUDE_ONLY
00061 
00062     namespace internal
00063     {
00064 
00065       typedef char yes_;
00066       struct no_ { char dummy[2]; };
00067 
00068       template <typename T>
00069       struct make_
00070       {
00071         static T* ptr();
00072       };
00073 
00074 
00075       // Fwd decl.
00076       template <unsigned id, typename T>
00077       struct equiv_ret_;
00078 
00079       // Fwd decl.
00080       template <typename V>
00081       const typename internal::equiv_<V>::ret&
00082       run_equiv_(const V& v);
00083 
00084 
00085       template <typename V, typename T>
00086       inline
00087       const typename internal::equiv_<V>::ret&
00088       run_equiv_(const Value<V>* v, const T*)
00089       {
00090         return run_equiv_(exact(v)->to_equiv()); // Rec.
00091       }
00092 
00093       template <typename V>
00094       inline
00095       const V&
00096       run_equiv_(const void*, const V* v)
00097       {
00098         return *v; // Stop rec.
00099       }
00100 
00101       template <typename V>
00102       inline
00103       const typename internal::equiv_<V>::ret&
00104       run_equiv_(const V& v)
00105       {
00106         return run_equiv_(&v, &v);
00107       }
00108 
00109 
00110       template <typename T>
00111       struct equiv_ret_< 1, T > // Rec.
00112       {
00113         typedef typename T::equiv V;
00114         typedef typename equiv_<V>::ret ret;
00115       };
00116 
00117       template <typename T>
00118       struct equiv_ret_< 2, T > // Stop rec.
00119       {
00120         typedef T ret;
00121       };
00122 
00123       template <typename V>
00124       yes_ equiv_selector_(Value<V>*);
00125 
00126       no_  equiv_selector_(void*);
00127 
00128       template <typename T>
00129       struct equiv_
00130       {
00131         enum { id = sizeof(equiv_selector_(make_<T>::ptr())) };
00132         typedef typename equiv_ret_<id, T>::ret ret;
00133 
00134         inline
00135         static ret run(const T& t)
00136         {
00137           return ret::run(t);
00138         }
00139       };
00140 
00141     } // end of namespace mln::value::internal
00142 
00143 
00144 
00145     template <typename V>
00146     inline
00147     typename internal::equiv_<V>::ret
00148     equiv(const mln::Value<V>& v)
00149     {
00150       return internal::run_equiv_(exact(v));
00151     }
00152 
00153 
00154 # endif // ! MLN_INCLUDE_ONLY
00155 
00156   } // end of namespace mln::value
00157 
00158 } // end of namespace mln
00159 
00160 # include <mln/value/cast.hh>
00161 
00162 #endif // ! MLN_VALUE_EQUIV_HH

Generated on Thu Sep 8 2011 18:31:48 for Milena (Olena) by  doxygen 1.7.1