868: Add initialization material; extend point2d and box2d with it.

https://svn.lrde.epita.fr/svn/oln/trunk/olena Index: ChangeLog from Thierry Geraud <thierry.geraud@lrde.epita.fr> Add initialization material; extend point2d and box2d with it. * oln/core/init.hh, * oln/core/internal/initializer.hh, * oln/core/internal/instant_value.hh: New. * oln/core/concept/grid.hh (from, to): New. * oln/core/2d/grid2d.hh (row, col), (drow, dcol, nrows, ncols): New. * oln/core/2d/box2d.hh (init__): New. * oln/core/2d/point2d.hh (init__): New. * oln/core/gen/box.hh (init__): New. (operator<<): New. * oln/core/internal/utils.hh: Add FIXME. 2d/box2d.hh | 65 +++++++++++++++++++++++++- 2d/grid2d.hh | 9 +++ 2d/point2d.hh | 30 ++++++++++++ concept/grid.hh | 6 ++ gen/box.hh | 43 ++++++++++++++++- init.hh | 105 ++++++++++++++++++++++++++++++++++++++++++ internal/initializer.hh | 115 ++++++++++++++++++++++++++++++++++++++++++++++ internal/instant_value.hh | 85 ++++++++++++++++++++++++++++++++++ internal/utils.hh | 6 ++ 9 files changed, 460 insertions(+), 4 deletions(-) Index: oln/core/concept/grid.hh --- oln/core/concept/grid.hh (revision 867) +++ oln/core/concept/grid.hh (working copy) @@ -30,11 +30,17 @@ # define OLN_CORE_CONCEPT_GRID_HH # include <oln/core/equipment.hh> +# include <oln/core/internal/instant_value.hh> namespace oln { + /// Instant values. + oln_decl_instant_value(from); + oln_decl_instant_value(to); + + /// Concept-class "Grid". template <typename Exact> Index: oln/core/init.hh --- oln/core/init.hh (revision 0) +++ oln/core/init.hh (revision 0) @@ -0,0 +1,105 @@ +// Copyright (C) 2007 EPITA Research and Development Laboratory +// +// This file is part of the Olena Library. This library is free +// software; you can redistribute it and/or modify it under the terms +// of the GNU General Public License version 2 as published by the +// Free Software Foundation. +// +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this library; see the file COPYING. If not, write to +// the Free Software Foundation, 51 Franklin Street, Fifth Floor, +// Boston, MA 02111-1307, USA. +// +// As a special exception, you may use this file as part of a free +// software library without restriction. Specifically, if other files +// instantiate templates or use macros or inline functions from this +// file, or you compile this file and link it with other files to +// produce an executable, this file does not by itself cause the +// resulting executable to be covered by the GNU General Public +// License. This exception does not however invalidate any other +// reasons why the executable file might be covered by the GNU General +// Public License. + +#ifndef OLN_CORE_INIT_HH +# define OLN_CORE_INIT_HH + +# include <oln/core/internal/utils.hh> +# include <oln/core/internal/instant_value.hh> +# include <oln/core/internal/initializer.hh> + + + +namespace oln +{ + + + template <template<class> class M, typename V> + internal::initializer_< internal::singleton< M<V> > > + init(const internal::instant_value_<M,V>& v); + + + template <template<class> class M1, typename V1, + template<class> class M2, typename V2> + internal::initializer_< internal::pair< M1<V1>, M2<V2> > > + init(const internal::instant_value_<M1,V1>& v1, + const internal::instant_value_<M2,V2>& v2); + + + template <template<class> class M1, typename V1, + template<class> class M2, typename V2, + template<class> class M3, typename V3> + internal::initializer_< internal::triplet< M1<V1>, M2<V2>, M3<V3> > > + init(const internal::instant_value_<M1,V1>& v1, + const internal::instant_value_<M2,V2>& v2, + const internal::instant_value_<M3,V3>& v3); + + // ... + + + +# ifndef OLN_INCLUDE_ONLY + + template <template<class> class M, typename V> + internal::initializer_< internal::singleton< M<V> > > + init(const internal::instant_value_<M,V>& v) + { + internal::singleton< M<V> > tmp(v.value); + return tmp; + } + + template <template<class> class M1, typename V1, + template<class> class M2, typename V2> + internal::initializer_< internal::pair< M1<V1>, M2<V2> > > + init(const internal::instant_value_<M1,V1>& v1, + const internal::instant_value_<M2,V2>& v2) + { + internal::pair< M1<V1>, M2<V2> > tmp(v1.value, v2.value); + return tmp; + } + + template <template<class> class M1, typename V1, + template<class> class M2, typename V2, + template<class> class M3, typename V3> + internal::initializer_< internal::triplet< M1<V1>, M2<V2>, M3<V3> > > + init(const internal::instant_value_<M1,V1>& v1, + const internal::instant_value_<M2,V2>& v2, + const internal::instant_value_<M3,V3>& v3) + { + internal::triplet< M1<V1>, M2<V2>, M3<V3> > tmp(v1.value, v2.value, v3.value); + return tmp; + } + + // ... + +# endif // OLN_INCLUDE_ONLY + + +} // end of namespace oln + + +#endif // ! OLN_CORE_INIT_HH Property changes on: oln/core/init.hh ___________________________________________________________________ Name: svn:executable + * Index: oln/core/2d/grid2d.hh --- oln/core/2d/grid2d.hh (revision 867) +++ oln/core/2d/grid2d.hh (working copy) @@ -32,6 +32,7 @@ # include <oln/core/concept/grid.hh> + # define OLN_ENV_2D @@ -39,6 +40,14 @@ namespace oln { + /// Instant values. + oln_decl_instant_value(row); + oln_decl_instant_value(col); + oln_decl_instant_value(drow); + oln_decl_instant_value(dcol); + oln_decl_instant_value(nrows); + oln_decl_instant_value(ncols); + /// \{ /// Fwd decls. Index: oln/core/2d/box2d.hh --- oln/core/2d/box2d.hh (revision 867) +++ oln/core/2d/box2d.hh (working copy) @@ -28,18 +28,79 @@ #ifndef OLN_CORE_2D_BOX2D_HH # define OLN_CORE_2D_BOX2D_HH -# include <oln/core/gen/box.hh> # include <oln/core/2d/point2d.hh> namespace oln { + // Fwd decl. + template <typename P> class box_; + + // FIXME: box2d should be an actual type, not an alias... typedef box_<point2d> box2d; + + /// init__ + namespace internal + { + + template <typename C> + void init__(box2d& b, + const initializer_< pair< nrows_t<C>, ncols_t<C> > >& data); + + template <typename C> + void init__(box2d& b, + const initializer_< triplet< from_t<point2d>, nrows_t<C>, ncols_t<C> > >& data); + + } // end of namespace oln::internal + } // end of namespace oln -#endif // ! OLN_CORE_2D_BOX2D_HH +# include <oln/core/gen/box.hh> + +# ifndef OLN_INCLUDE_ONLY + +namespace oln +{ + + namespace internal + { + + template <typename C> + void init__(box2d& b, + const initializer_< pair< nrows_t<C>, ncols_t<C> > >& data) + { + C nrows = data->value1.value; + C ncols = data->value2.value; + precondition(nrows > 0 and ncols > 0); + b.pmin().row() = 0; + b.pmin().col() = 0; + b.pmax().row() = nrows - 1; + b.pmax().col() = ncols - 1; + } + + template <typename C> + void init__(box2d& b, + const initializer_< triplet< from_t<point2d>, nrows_t<C>, ncols_t<C> > >& data) + { + C nrows = data->value2.value; + C ncols = data->value3.value; + precondition(nrows > 0 and ncols > 0); + b.pmin() = data->value1.value; + b.pmax().row() = b.pmin().row() + nrows - 1; + b.pmax().col() = b.pmin().col() + ncols - 1; + } + + } // end of namespace oln::internal + +} // end of namespace oln + + +# endif // OLN_INCLUDE_ONLY + + +#endif // ! OLN_CORE_2D_BOX2D_HH Index: oln/core/2d/point2d.hh --- oln/core/2d/point2d.hh (revision 867) +++ oln/core/2d/point2d.hh (working copy) @@ -31,6 +31,7 @@ # include <oln/core/2d/grid2d.hh> # include <oln/core/internal/point2d.hh> +# include <oln/core/init.hh> namespace oln @@ -59,13 +60,25 @@ }; + /// init__ + namespace internal + { + template <typename C> + void init__(point2d& p, + const initializer_< pair< row_t<C>, col_t<C> > >& data); + } + + /// Usual 2D point class. class point2d : public internal::point2d_< point2d > { public: + /// Ctors. point2d(); point2d(int row, int col); + template <typename D> + point2d(const internal::initializer_<D>& data); }; @@ -82,6 +95,23 @@ this->col() = col; } + template <typename D> + point2d::point2d(const internal::initializer_<D>& data) + { + internal::init__(*this, data); + } + + namespace internal + { + template <typename C> + void init__(point2d& p, + const initializer_< pair< row_t<C>, col_t<C> > >& data) + { + p.row() = data->value1.value; // FIXME: first + p.col() = data->value2.value; // FIXME: second + } + } + # endif Index: oln/core/gen/box.hh --- oln/core/gen/box.hh (revision 867) +++ oln/core/gen/box.hh (working copy) @@ -32,11 +32,13 @@ # include <oln/core/concept/point.hh> # include <oln/core/concept/iterator_on_points.hh> # include <oln/core/internal/point_set_base.hh> +# include <oln/core/init.hh> namespace oln { + // Forward declarations. template <typename P> class box_; template <typename P> class box_fwd_piter_; @@ -62,6 +64,15 @@ }; + /// init__ + namespace internal + { + template <typename P> + void init__(box_<P>& b, + const initializer_< pair< from_t<P>, to_t<P> > >& data); + } + + /// Generic box class based on a point class. template <typename P> @@ -83,6 +94,8 @@ box_(); box_(const P& pmin, const P& pmax); + template <typename D> + box_(const internal::initializer_<D>& data); unsigned impl_npoints() const; bool impl_has(const P& p) const; @@ -99,6 +112,11 @@ }; // end of class oln::box_<P> + template <typename P> + std::ostream& operator<<(std::ostream& ostr, const box_<P>& b) + { + return ostr << "{ " << b.pmin() << " .. " << b.pmax() << " }"; + } // -------------------- iterators on box_<P> @@ -209,6 +227,13 @@ } template <typename P> + template <typename D> + box_<P>::box_(const internal::initializer_<D>& data) + { + internal::init__(*this, data); + } + + template <typename P> unsigned box_<P>::impl_npoints() const { @@ -268,6 +293,22 @@ } + // -------------------- init__ + + + namespace internal + { + + template <typename P> + void init__(box_<P>& b, + const initializer_< pair< from_t<P>, to_t<P> > >& data) + { + b.pmin() = data->value1.value; + b.pmax() = data->value2.value; + } + + } + // -------------------- box_fwd_piter_<P> @@ -407,7 +448,7 @@ return &p_; } -# endif +# endif // OLN_INCLUDE_ONLY } // end of namespace oln Index: oln/core/internal/initializer.hh --- oln/core/internal/initializer.hh (revision 0) +++ oln/core/internal/initializer.hh (revision 0) @@ -0,0 +1,115 @@ +// Copyright (C) 2007 EPITA Research and Development Laboratory +// +// This file is part of the Olena Library. This library is free +// software; you can redistribute it and/or modify it under the terms +// of the GNU General Public License version 2 as published by the +// Free Software Foundation. +// +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this library; see the file COPYING. If not, write to +// the Free Software Foundation, 51 Franklin Street, Fifth Floor, +// Boston, MA 02111-1307, USA. +// +// As a special exception, you may use this file as part of a free +// software library without restriction. Specifically, if other files +// instantiate templates or use macros or inline functions from this +// file, or you compile this file and link it with other files to +// produce an executable, this file does not by itself cause the +// resulting executable to be covered by the GNU General Public +// License. This exception does not however invalidate any other +// reasons why the executable file might be covered by the GNU General +// Public License. + +#ifndef OLN_CORE_INTERNAL_INITIALIZER_HH +# define OLN_CORE_INTERNAL_INITIALIZER_HH + + +namespace oln +{ + + + namespace ERROR + { + + template <typename S> + struct bad_initialization_of_ + { + template <typename D> + struct with_; + }; + + } // end of namespace oln::ERROR + + + + namespace internal + { + + + template <typename D> + struct initializer_ + { + initializer_(const D& data); + const D& operator*() const; + const D* operator->() const; + + protected: + D data_; + }; + + + template <typename Tag, typename Subject, typename D> + void init__(const Tag& t, Subject& s, const initializer_<D>& d); + + template <typename Subject, typename D> + void init__(Subject& s, const initializer_<D>& d); + + + +# ifndef OLN_INCLUDE_ONLY + + template <typename D> + initializer_<D>::initializer_(const D& data) + : data_(data) + {} + + template <typename D> + const D& + initializer_<D>::operator*() const + { + return this->data_; + } + + template <typename D> + const D* + initializer_<D>::operator->() const + { + return &(this->data_); + } + + template <typename Tag, typename Subject, typename D> + void init__(const Tag&, Subject&, const initializer_<D>&) + { + mlc::abort_<D, typename ERROR::bad_initialization_of_<Subject>::template with_<D> >::check(); + } + + template <typename Subject, typename D> + void init__(Subject&, const initializer_<D>&) + { + mlc::abort_<D, typename ERROR::bad_initialization_of_<Subject>::template with_<D> >::check(); + } + +# endif // OLN_INCLUDE_ONLY + + + } // end of namespace oln::internal + +} // end of namespace oln + + +#endif // ! OLN_CORE_INTERNAL_INITIALIZER_HH Property changes on: oln/core/internal/initializer.hh ___________________________________________________________________ Name: svn:executable + * Index: oln/core/internal/utils.hh --- oln/core/internal/utils.hh (revision 867) +++ oln/core/internal/utils.hh (working copy) @@ -35,6 +35,11 @@ namespace internal { + + // FIXME: Rename attributes as first / second / third / fourth? + + + /// Simple singleton class. template <typename T> @@ -109,4 +114,3 @@ #endif // ! OLN_CORE_INTERNAL_UTILS_HH - Index: oln/core/internal/instant_value.hh --- oln/core/internal/instant_value.hh (revision 0) +++ oln/core/internal/instant_value.hh (revision 0) @@ -0,0 +1,85 @@ +// Copyright (C) 2007 EPITA Research and Development Laboratory +// +// This file is part of the Olena Library. This library is free +// software; you can redistribute it and/or modify it under the terms +// of the GNU General Public License version 2 as published by the +// Free Software Foundation. +// +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this library; see the file COPYING. If not, write to +// the Free Software Foundation, 51 Franklin Street, Fifth Floor, +// Boston, MA 02111-1307, USA. +// +// As a special exception, you may use this file as part of a free +// software library without restriction. Specifically, if other files +// instantiate templates or use macros or inline functions from this +// file, or you compile this file and link it with other files to +// produce an executable, this file does not by itself cause the +// resulting executable to be covered by the GNU General Public +// License. This exception does not however invalidate any other +// reasons why the executable file might be covered by the GNU General +// Public License. + +#ifndef OLN_CORE_INTERNAL_INSTANT_VALUE_HH +# define OLN_CORE_INTERNAL_INSTANT_VALUE_HH + + +# define oln_decl_instant_value(Name) \ + \ +template <typename V> \ +struct Name##_t : public internal::instant_value_< Name##_t, V> \ +{ \ + Name##_t(const V& v) { this->value = v; } \ +}; \ + \ +template <typename V> \ +Name##_t<V> Name(const V& v) \ +{ \ + return Name##_t<V>(v); \ +} \ + \ +struct e_n_d___w_i_t_h___s_e_m_i_c_o_l_u_m_n + + + +namespace oln +{ + + namespace internal + { + + /// Class internal::instant_value_<M,V>. + + template <template<class> class M, typename V> + struct instant_value_ + { + V value; + + template <typename W> + operator M<W>() const; + }; + + +# ifndef OLN_INCLUDE_ONLY + + template <template<class> class M, typename V> + template <typename W> + instant_value_<M,V>::operator M<W>() const + { + M<W> tmp(this->value); + return tmp; + } + +# endif // OLN_INCLUDE_ONLY + + } // end of namespace oln::internal + +} // end of namespace oln + + +#endif // ! OLN_CORE_INTERNAL_INSTANT_VALUE_HH
participants (1)
-
Thierry Geraud