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

proxy.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_PROXY_HH
00027 # define MLN_VALUE_PROXY_HH
00028 
00032 
00033 # include <mln/core/concept/value.hh>
00034 # include <mln/trait/value_.hh>
00035 # include <mln/metal/unconst.hh>
00036 
00037 
00038 namespace mln
00039 {
00040 
00041   // Fwd decl.
00042   namespace value {
00043     template <typename I> class proxy;
00044   }
00045 
00046 
00047   namespace trait
00048   {
00049 
00050     template <typename I>
00051     struct value_< mln::value::proxy<I> >
00052       :
00053       value_< mln_value(I) >
00054     {
00055     };
00056 
00057     template <typename I>
00058     struct value_< mln::value::proxy<const I> >
00059       :
00060       value_< mln_value(I) >
00061     {
00062     };
00063 
00064   } // end of namespace trait
00065 
00066 
00067   namespace value
00068   {
00069 
00074     template <typename I>
00075     class proxy : public Value< proxy<I> >
00076     {
00077     public:
00078 
00080       typedef void enc; // FIXME
00081 
00083       typedef mln_value(I) equiv;
00084 
00086       proxy(I& ima, const mln_psite(I)& p);
00087 
00089       ~proxy();
00090 
00092       template <typename V>
00093       proxy<I>& operator=(const V& v);
00094 
00096       proxy<I>& operator=(const proxy<I>& rhs);
00097 
00099       template <typename II>
00100       proxy<I>& operator=(const proxy<II>& rhs);
00101 
00103       template <typename V>
00104       operator V() const;
00105 
00107       operator mln_value(I)() const;
00108 
00110       mln_value(I) to_value() const;
00111 
00112     protected:
00113       I& ima_;
00114       mln_psite(I) p_;
00115     };
00116 
00117 
00118 
00123     template <typename I>
00124     class proxy< const I > : public Value< proxy<const I> >
00125     {
00126     public:
00127 
00129       typedef void enc; // FIXME
00130 
00132       typedef mln_value(I) equiv;
00133 
00135       proxy(const I& ima, const mln_psite(I)& p);
00136 
00138       ~proxy();
00139 
00141       template <typename V>
00142       operator V() const;
00143 
00145       operator mln_value(I)() const;
00146 
00148       mln_value(I) to_value() const;
00149 
00150     protected:
00151       const I& ima_;
00152       mln_psite(I) p_;
00153     };
00154 
00155 
00156 
00157     template <typename I>
00158     bool operator==(const proxy<I>& lhs, const mln_value(I)& rhs);
00159 
00160     template <typename I>
00161     bool operator==(const mln_value(I)& lhs, const proxy<I>& rhs);
00162 
00163     template <typename I, typename J>
00164     bool operator==(const proxy<I>& lhs, const proxy<J>& rhs);
00165 
00166 
00167     template <typename I>
00168     bool operator<(const proxy<I>& lhs, const mln_value(I)& rhs);
00169 
00170     template <typename I>
00171     bool operator<(const mln_value(I)& lhs, const proxy<I>& rhs);
00172 
00173     template <typename I, typename J>
00174     bool operator<(const proxy<I>& lhs, const proxy<J>& rhs);
00175 
00176 
00177     // FIXME: Ops such as +=, etc.
00178 
00179 
00180 
00188     template <typename I>
00189     std::ostream& operator<<(std::ostream& ostr, const proxy<I>& x);
00190 
00191 
00192 # ifndef MLN_INCLUDE_ONLY
00193 
00194     // proxy<I>
00195 
00196     template <typename I>
00197     inline
00198     proxy<I>::proxy(I& ima, const mln_psite(I)& p)
00199       : ima_(ima),
00200         p_(p)
00201     {
00202     }
00203 
00204     template <typename I>
00205     inline
00206     proxy<I>::~proxy()
00207     {
00208       mln_rvalue(I) (I::*mr)(const mln_psite(I)&) const = & I::read_;
00209       mr = 0;
00210       void (I::*mw)(const mln_psite(I)&, const mln_value(I)&) = & I::write_;
00211       mw = 0;
00212     }
00213 
00214     template <typename I>
00215     template <typename V>
00216     inline
00217     proxy<I>&
00218     proxy<I>::operator=(const V& v)
00219     {
00220       ima_.write_(p_, v);
00221       return *this;
00222     }
00223 
00224     template <typename I>
00225     inline
00226     proxy<I>&
00227     proxy<I>::operator=(const proxy<I>& rhs)
00228     {
00229       this->operator=(rhs.to_value());
00230       return *this;
00231     }
00232 
00233     template <typename I>
00234     template <typename II>
00235     inline
00236     proxy<I>&
00237     proxy<I>::operator=(const proxy<II>& rhs)
00238     {
00239       this->operator=(rhs.to_value());
00240       return *this;
00241     }
00242 
00243     template <typename I>
00244     template <typename V>
00245     inline
00246     proxy<I>::operator V() const
00247     {
00248       return ima_.read_(p_);
00249     }
00250 
00251     template <typename I>
00252     inline
00253     proxy<I>::operator mln_value(I)() const
00254     {
00255       return ima_.read_(p_);
00256     }
00257 
00258     template <typename I>
00259     inline
00260     mln_value(I)
00261     proxy<I>::to_value() const
00262     {
00263       return ima_.read_(p_);
00264     }
00265 
00266     // proxy<const I>
00267 
00268     template <typename I>
00269     inline
00270     proxy<const I>::proxy(const I& ima, const mln_psite(I)& p)
00271       : ima_(ima),
00272         p_(p)
00273     {
00274     }
00275 
00276     template <typename I>
00277     inline
00278     proxy<const I>::~proxy()
00279     {
00280       mln_rvalue(I) (I::*mr)(const mln_psite(I)&) const = & I::read_;
00281       mr = 0;
00282     }
00283 
00284     template <typename I>
00285     template <typename V>
00286     inline
00287     proxy<const I>::operator V() const
00288     {
00289       return ima_.read_(p_);
00290     }
00291 
00292     template <typename I>
00293     inline
00294     proxy<const I>::operator mln_value(I)() const
00295     {
00296       return ima_.read_(p_);
00297     }
00298 
00299     template <typename I>
00300     inline
00301     mln_value(I)
00302     proxy<const I>::to_value() const
00303     {
00304       return ima_.read_(p_);
00305     }
00306 
00307     // operator <<
00308 
00309     template <typename I>
00310     inline
00311     std::ostream& operator<<(std::ostream& ostr, const proxy<I>& x)
00312     {
00313       return ostr << x.to_value();
00314     }
00315 
00316     // operator ==
00317 
00318     template <typename I>
00319     inline
00320     bool operator==(const proxy<I>& lhs, const mln_value(I)& rhs)
00321     {
00322       return lhs.to_value() == rhs;
00323     }
00324 
00325     template <typename I>
00326     inline
00327     bool operator==(const mln_value(I)& lhs, const proxy<I>& rhs)
00328     {
00329       return lhs == rhs.to_value();
00330     }
00331 
00332     template <typename I, typename J>
00333     inline
00334     bool operator==(const proxy<I>& lhs, const proxy<J>& rhs)
00335     {
00336       return lhs.to_value() == rhs.to_value();
00337     }
00338 
00339     // operator <
00340 
00341     template <typename I>
00342     inline
00343     bool operator<(const proxy<I>& lhs, const mln_value(I)& rhs)
00344     {
00345       return lhs.to_value() < rhs;
00346     }
00347 
00348     template <typename I>
00349     inline
00350     bool operator<(const mln_value(I)& lhs, const proxy<I>& rhs)
00351     {
00352       return lhs < rhs.to_value();
00353     }
00354 
00355     template <typename I, typename J>
00356     inline
00357     bool operator<(const proxy<I>& lhs, const proxy<J>& rhs)
00358     {
00359       return lhs.to_value() < rhs.to_value();
00360     }
00361 
00362 # endif // ! MLN_INCLUDE_ONLY
00363 
00364   } // end of namespace mln::value
00365 
00366 } // end of namespace mln
00367 
00368 
00369 #endif // ! MLN_VALUE_PROXY_HH

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