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

site_relative_iterator_base.hh

00001 // Copyright (C) 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_CORE_INTERNAL_SITE_RELATIVE_ITERATOR_BASE_HH
00027 # define MLN_CORE_INTERNAL_SITE_RELATIVE_ITERATOR_BASE_HH
00028 
00038 
00039 
00040 # include <vector>
00041 # include <mln/core/internal/site_iterator_base.hh>
00042 # include <mln/metal/converts_to.hh>
00043 
00044 
00045 namespace mln
00046 {
00047 
00048   namespace internal
00049   {
00050 
00061     template <typename S, typename E>
00062     class site_relative_iterator_base : public site_iterator_base< S, E >
00063     {
00064     public:
00065 
00067       site_relative_iterator_base();
00068 
00069       template <typename P>
00070       void center_at(const P& c);
00071 
00073       void start_();
00074 
00076       void next_();
00077 
00080       const S& site_set() const;
00081 
00083       const mln_psite(S)& center() const;
00084 
00090       const mln_psite(S)& subj_();
00091 
00093       const mln_psite(S)& p_hook_() const;
00094 
00096       void change_target(const S& s);
00097 
00103       E& update();
00104 
00105     protected:
00106 
00109       const mln_psite(S)* c_;
00110 
00111     private:
00112 
00113       // Allows inherited classes to do extra work while centering.
00114       // Default implementation.
00115       template <typename P>
00116       void center_at_(const P& c);
00117 
00119       mln_psite(S) p_;
00120     };
00121 
00122 
00123 
00124 # ifndef MLN_INCLUDE_ONLY
00125 
00126     template <typename S, typename E>
00127     inline
00128     site_relative_iterator_base<S,E>::site_relative_iterator_base()
00129       : c_(0)
00130     {
00131       void (E::*m1)() = & E::do_start_;
00132       m1 = 0;
00133       void (E::*m2)() = & E::do_next_;
00134       m2 = 0;
00135       mln_psite(S) (E::*m3)() const = & E::compute_p_;
00136       m3 = 0;
00137     }
00138 
00139     template <typename S, typename E>
00140     template <typename P>
00141     inline
00142     void
00143     site_relative_iterator_base<S,E>::center_at(const P& c)
00144     {
00145       mlc_converts_to(P, const mln_psite(S)&)::check();
00146       c_ = & static_cast< const mln_psite(S)& >(c);
00147       exact(this)->center_at_(c); // May call some extra code.
00148       this->invalidate();
00149     }
00150 
00151     template <typename S, typename E>
00152     inline
00153     void
00154     site_relative_iterator_base<S,E>::start_()
00155     {
00156       exact(this)->do_start_();
00157       if (this->is_valid())
00158         p_ = exact(this)->compute_p_();
00159     }
00160 
00161     template <typename S, typename E>
00162     inline
00163     void
00164     site_relative_iterator_base<S,E>::next_()
00165     {
00166       exact(this)->do_next_();
00167       if (this->is_valid())
00168         p_ = exact(this)->compute_p_();
00169     }
00170 
00171     template <typename S, typename E>
00172     inline
00173     const mln_psite(S)&
00174     site_relative_iterator_base<S,E>::center() const
00175     {
00176       mln_precondition(c_ != 0);
00177       return *c_;
00178     }
00179 
00180     template <typename S, typename E>
00181     inline
00182     const S&
00183     site_relative_iterator_base<S, E>::site_set() const
00184     {
00185       mln_precondition(this->s_ != 0);
00186       return *this->s_;
00187     }
00188 
00189     template <typename S, typename E>
00190     inline
00191     const mln_psite(S)&
00192     site_relative_iterator_base<S,E>::subj_()
00193     {
00194       mln_assertion(exact(this)->compute_p_() == p_);
00195       return p_;
00196     }
00197 
00198     template <typename S, typename E>
00199     inline
00200     const mln_psite(S)&
00201     site_relative_iterator_base<S,E>::p_hook_() const
00202     {
00203       return p_;
00204     }
00205 
00206     template <typename S, typename E>
00207     inline
00208     void
00209     site_relative_iterator_base<S,E>::change_target(const S& s)
00210     {
00211       this->s_ = & s;
00212       // p might be also updated since it can hold a pointer towards
00213       // the set it designates, so:
00214       if_possible::change_target(p_, s);
00215       // Last:
00216       this->invalidate();
00217     }
00218 
00219     template <typename S, typename E>
00220     inline
00221     E&
00222     site_relative_iterator_base<S,E>::update()
00223     {
00224       mln_precondition(this->s_ && c_);
00225       p_ = exact(this)->compute_p_();
00226       mln_postcondition(this->is_valid());
00227       return exact(*this);
00228     }
00229 
00230     template <typename S, typename E>
00231     template <typename P>
00232     inline
00233     void
00234     site_relative_iterator_base<S,E>::center_at_(const P&)
00235     {
00236       // Default is no-op, meaning "no extra code".
00237     }
00238 
00239 # endif // ! MLN_INCLUDE_ONLY
00240 
00241   } // end of namespace mln::internal
00242 
00243 } // end of namespace mln
00244 
00245 
00246 #endif // ! MLN_CORE_INTERNAL_SITE_RELATIVE_ITERATOR_BASE_HH

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