664: Add xtd mutators and disable mutable facilities of other functions.

2006-10-20 Thierry GERAUD <theo@tegucigalpa.lrde.epita.fr> Add mutators and disable mutable facilities of other functions. * xtd/mutator.hh: New. * xtd/abstract/mutable_fun.hh: New. * tests/accessor.cc: New. * xtd/res.hh: Add comment. * xtd/accessor.hh (xtd_decl_accessor): Disable mutable impl_op. (xtd_decl_accessor): Add static check. * xtd/abstract/open_nary_fun.hh (fun_operator_1): Disable mutable impl of default_case_ and add commented mutable_res. (open_nary_fun_<1,E>): Disable mutable version of operator(). * xtd/abstract/plain_nary_fun.hh (plain_nary_fun_<1,E>): Disable mutable version of operator(). * xtd/abstract/plain_fun.hh: Add commented code for xtd_mutable_res. * xtd/mfun.hh (m1fun_): Disable mutable version of impl_calc. * xtd/Makefile.am: Update. * tests/Makefile.am: Update. Index: xtd/res.hh =================================================================== --- xtd/res.hh (revision 663) +++ xtd/res.hh (working copy) @@ -84,6 +84,8 @@ # define xtd_expr_res(F, Args) typename xtd::internal::get_expr_res_< F, Args >::ret # define xtd_expr_res_(F, Args) xtd::internal::get_expr_res_< F, Args >::ret +// FIXME: Uncomment below. +// # define xtd_mutable_res_1(F, A1) typename xtd::internal::get_res_< F, A1 >::ret // internal so do not use it; prefer xtd_res_0(F) Index: xtd/mutator.hh =================================================================== --- xtd/mutator.hh (revision 0) +++ xtd/mutator.hh (revision 0) @@ -0,0 +1,97 @@ +// Copyright (C) 2006 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 XTD_MUTATOR_HH +# define XTD_MUTATOR_HH + +# include <mlc/typedef.hh> +# include <mlc/cmp.hh> + +# include <xtd/abstract/plain_nary_fun.hh> +# include <xtd/abstract/mutable_fun.hh> + + +# define xtd_decl_mutator(MethodName) \ + \ +namespace xtd \ +{ \ + \ + namespace mutator \ + { \ + struct MethodName##_type; \ + \ + mlc_decl_typedef(MethodName##_type); \ + \ + template <typename T> \ + struct ERROR__DECLARATION_typedef_OF_##MethodName##_type_NOT_FOUND_IN_; \ + } \ + \ + \ + template <typename A> \ + struct res_< mutator::MethodName##_type, A > \ + \ + : private mlc::assert_< mlc::neq_< mlc_typedef_in(mutator, A, MethodName##_type), \ + mlc::not_found >, \ + mutator::ERROR__DECLARATION_typedef_OF_##MethodName##_type_NOT_FOUND_IN_<A> > \ + \ + { \ + typedef mlc_typedef_in(mutator, A, MethodName##_type) ret; \ + }; \ + \ + namespace mutator \ + { \ + \ + struct MethodName##_type \ + \ + : public abstract::mutable_fun_< MethodName##_type > \ + { \ + template <typename A> \ + typename res_<MethodName##_type, A>::ret \ + impl_op(const A& arg) const \ + { \ + return arg.MethodName(); \ + } \ + \ + template <typename A> \ + typename res_<MethodName##_type, A>::ret & \ + impl_op(A& arg) const \ + { \ + return arg.MethodName(); \ + } \ + }; \ + \ + static const MethodName##_type MethodName; \ + \ + } \ + \ +} \ + \ +struct e_n_d__w_i_t_h__s_e_m_i_c_o_l_o_n + + + +#endif // ! XTD_MUTATOR_HH Index: xtd/accessor.hh =================================================================== --- xtd/accessor.hh (revision 663) +++ xtd/accessor.hh (working copy) @@ -28,54 +28,75 @@ #ifndef XTD_ACCESSOR_HH # define XTD_ACCESSOR_HH +# include <mlc/typedef.hh> +# include <mlc/cmp.hh> + # include <xtd/abstract/plain_nary_fun.hh> # include <xtd/mfun.hh> -# define xtd_decl_accessor(MethodName) \ - \ -namespace xtd \ -{ \ - \ - namespace accessor \ - { \ - template <typename T> struct MethodName##_; \ - } \ - \ - template <typename T> \ - struct fun_traits_< accessor::MethodName##_<T> > \ - { \ - typedef T arg_type; \ - typedef typename T::MethodName##_type res_type; \ - }; \ - \ - namespace accessor \ - { \ - \ - template <typename T> \ - struct MethodName##_ \ - : public xtd::abstract::plain_nary_fun_< 1, MethodName##_<T> > \ - { \ - xtd_res(MethodName##_<T>) impl_op(const T& arg) const \ - { \ - return arg.MethodName(); \ - } \ - xtd_res(MethodName##_<T>)& impl_op(T& arg) const \ - { \ - return arg.MethodName(); \ - } \ - }; \ - \ - typedef xtd::m1fun_<MethodName##_> MethodName##_type; \ - \ - static const MethodName##_type MethodName; \ - \ - } \ - \ -} \ - \ +# define xtd_decl_accessor(MethodName) \ + \ +namespace xtd \ +{ \ + \ + namespace accessor \ + { \ + template <typename T> struct MethodName##_; \ + \ + mlc_decl_typedef(MethodName##_type); \ + \ + template <typename T> \ + struct ERROR__DECLARATION_typedef_OF_##MethodName##_type_NOT_FOUND_IN_; \ + } \ + \ + template <typename T> \ + struct fun_traits_< accessor::MethodName##_<T> > \ + : private mlc::assert_< mlc::neq_< mlc_typedef_in(accessor, T, MethodName##_type), \ + mlc::not_found >, \ + accessor::ERROR__DECLARATION_typedef_OF_##MethodName##_type_NOT_FOUND_IN_<T> > \ + { \ + typedef T arg_type; \ + typedef mlc_typedef_in(accessor, T, MethodName##_type) res_type; \ + }; \ + \ + namespace accessor \ + { \ + \ + template <typename T> \ + struct MethodName##_ \ + : public xtd::abstract::plain_nary_fun_< 1, MethodName##_<T> >, \ + private mlc::assert_< mlc::neq_< mlc_typedef(T, MethodName##_type), \ + mlc::not_found >, \ + ERROR__DECLARATION_typedef_OF_##MethodName##_type_NOT_FOUND_IN_<T> > \ + { \ + xtd_res(MethodName##_<T>) impl_op(const T& arg) const \ + { \ + return arg.MethodName(); \ + } \ + }; \ + \ + typedef xtd::m1fun_<MethodName##_> MethodName##_type; \ + \ + static const MethodName##_type MethodName; \ + \ + } \ + \ +} \ + \ struct e_n_d__w_i_t_h__s_e_m_i_c_o_l_o_n +// FIXME: To be inserted above. +// In traits: +// typedef res_type & mutable_res_type; + +// In struct: +// xtd_mutable_res(MethodName##_<T>) impl_op(T& arg) const +// { +// return arg.MethodName(); +// } + + #endif // ! XTD_ACCESSOR_HH Index: xtd/abstract/mutable_fun.hh =================================================================== --- xtd/abstract/mutable_fun.hh (revision 0) +++ xtd/abstract/mutable_fun.hh (revision 0) @@ -0,0 +1,132 @@ +// Copyright (C) 2006 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 XTD_ABSTRACT_MUTABLE_FUN_HH +# define XTD_ABSTRACT_MUTABLE_FUN_HH + +# include <xtd/res.hh> +# include <xtd/abstract/plain_fun.hh> +# include <xtd/abstract/meta_fun.hh> + + +namespace xtd +{ + + namespace ERROR + { + struct ARG_SHOULD_NOT_BE_A_PLAIN_FUN; + struct ARG1_SHOULD_NOT_BE_A_PLAIN_FUN; + struct ARG2_SHOULD_NOT_BE_A_PLAIN_FUN; + struct ARG3_SHOULD_NOT_BE_A_PLAIN_FUN; + + struct ARG_SHOULD_NOT_BE_A_META_FUN; + + } // end of namespace xtd::ERROR + + + namespace tag + { + + struct mutable_fun_operator; + + } // end of namespace xtd::tag + + + // mutable_fun_<E>::operator()(const A& a) const + // mutable_fun_<E>::operator()(A& a) const + + template <typename E, typename A> + struct default_case_ < tag::mutable_fun_operator, + mlc::pair_<E, A> > + { + typedef typename res_<E, A>::ret res; + typedef res& mutable_res; + + static res impl(const E& target, + const A& a) + { + return target.impl_op(a); + } + + static mutable_res impl(const E& target, + A& a) + { + return target.impl_op(a); + } + }; + + + namespace abstract + { + + template <typename E> + class mutable_fun_ + + : public open_fun_<E> + { + public: + + template <typename A> + struct lcase_ + + : private mlc::assert_< mlc_is_not_a(A, xtd::abstract::plain_fun_), + xtd::ERROR::ARG_SHOULD_NOT_BE_A_PLAIN_FUN >, + + private mlc::assert_< mlc_is_not_a(A, xtd::abstract::meta_fun_), + xtd::ERROR::ARG_SHOULD_NOT_BE_A_META_FUN >, + + public xtd::case_< xtd::tag::mutable_fun_operator, + mlc::pair_<E, A> >::ret + {}; + + // Const version. + template <typename A> + typename lcase_<A>::res + operator()(const A& a) const + { + return lcase_<A>::impl(this->exact_(), a); + } + + // Mutable version. + template <typename A> + typename lcase_<A>::mutable_res + operator()(A& a) const + { + return lcase_<A>::impl(this->exact_(), a); + } + + protected: + mutable_fun_() {} + }; + + } // end of namespace xtd::abstract + + +} // end of namespace xtd + + +#endif // ! XTD_ABSTRACT_MUTABLE_FUN_HH Index: xtd/abstract/open_nary_fun.hh =================================================================== --- xtd/abstract/open_nary_fun.hh (revision 663) +++ xtd/abstract/open_nary_fun.hh (working copy) @@ -76,6 +76,7 @@ mlc::pair_<E, A> > { typedef xtd_res_1(E, A) res; +// typedef xtd_mutable_res_1(E, A) mutable_res; static res impl(const E& target, const A& a) @@ -83,12 +84,13 @@ return target.impl_calc(a); } - // The "mutable a" version below is dedicated to mutators. - static res& impl(const E& target, - A& a) - { - return target.impl_calc(a); - } + // FIXME: Uncomment below. +// // The "mutable a" version below is dedicated to mutators. +// static mutable_res impl(const E& target, +// A& a) +// { +// return target.impl_calc(a); +// } }; @@ -232,13 +234,14 @@ return case_<A>::impl(this->exact_(), a); } - // The "mutable a" version below is dedicated to mutators. - template <typename A> - typename case_<A>::res & - operator()(A& a) const - { - return case_<A>::impl(this->exact_(), a); - } + // FIXME: Uncomment below. +// // The "mutable a" version below is dedicated to mutators. +// template <typename A> +// typename case_<A>::res & +// operator()(A& a) const +// { +// return case_<A>::impl(this->exact_(), a); +// } protected: open_nary_fun_() {} Index: xtd/abstract/plain_nary_fun.hh =================================================================== --- xtd/abstract/plain_nary_fun.hh (revision 663) +++ xtd/abstract/plain_nary_fun.hh (working copy) @@ -144,11 +144,12 @@ return this->exact().impl_op(arg); } - // The "mutable arg" version below is dedicated to mutators. - xtd_res(E)& operator()(xtd_arg(E)& arg) const - { - return this->exact().impl_op(arg); - } + // FIXME: Uncomment below. +// // The "mutable arg" version below is dedicated to mutators. +// xtd_mutable_res(E) operator()(xtd_arg(E)& arg) const +// { +// return this->exact().impl_op(arg); +// } template <typename A> xtd_res(E) operator()(const A& arg) const Index: xtd/abstract/plain_fun.hh =================================================================== --- xtd/abstract/plain_fun.hh (revision 663) +++ xtd/abstract/plain_fun.hh (working copy) @@ -36,6 +36,8 @@ */ # define xtd_res(F) typename xtd::typedef_::res_type::from_< xtd::fun_traits_< F > >::ret +// FIXME: Uncomment below. +// # define xtd_mutable_res(F) typename xtd::typedef_::mutable_res_type::from_< xtd::fun_traits_< F > >::ret @@ -66,6 +68,8 @@ mlc_decl_typedef(res_type); + // FIXME: Uncomment below. +// mlc_decl_typedef(mutable_res_type); namespace abstract { Index: xtd/mfun.hh =================================================================== --- xtd/mfun.hh (revision 663) +++ xtd/mfun.hh (working copy) @@ -89,18 +89,20 @@ static const F_ f_; return f_(a); } - template <typename A> - xtd_res(F<A>)& impl_calc(A& a) const - // --------- - { - typedef F<A> F_; - mlc::assert_< mlc::and_< mlc_is_a(F_, abstract::plain_fun_), - mlc_is_a(F_, abstract::nary_fun_<1>) >, - xtd::ERROR::AN_xtd_m1fun_SHOULD_TAKE_AS_PARAMETER_AN_xtd_plain_nary_fun_WITH_n_BEING_1 - >::check(); - static const F_ f_; - return f_(a); - } + + // FIXME: Uncomment below. +// template <typename A> +// xtd_mutable_res(F<A>) impl_calc(A& a) const +// // --------- +// { +// typedef F<A> F_; +// mlc::assert_< mlc::and_< mlc_is_a(F_, abstract::plain_fun_), +// mlc_is_a(F_, abstract::nary_fun_<1>) >, +// xtd::ERROR::AN_xtd_m1fun_SHOULD_TAKE_AS_PARAMETER_AN_xtd_plain_nary_fun_WITH_n_BEING_1 +// >::check(); +// static const F_ f_; +// return f_(a); +// } }; Index: xtd/Makefile.am =================================================================== --- xtd/Makefile.am (revision 663) +++ xtd/Makefile.am (working copy) @@ -8,6 +8,7 @@ abstract/fun_nary_expr.hh \ abstract/meta_fun.hh \ abstract/meta_nary_fun.hh \ + abstract/mutable_fun.hh \ abstract/nary_fun.hh \ abstract/open_fun.hh \ abstract/open_nary_fun.hh \ @@ -44,6 +45,7 @@ math/tan.hh \ math/tanh.hh \ \ + accessor.hh \ arg.hh \ args.hh \ bind.hh \ @@ -53,6 +55,7 @@ math.hh \ mexpr.hh \ mfun.hh \ + mutator.hh \ optraits.hh \ res.hh \ traits.hh \ Index: tests/accessor.cc =================================================================== --- tests/accessor.cc (revision 0) +++ tests/accessor.cc (revision 0) @@ -0,0 +1,52 @@ +// Copyright (C) 2006 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. + +#include <cassert> +#include <xtd/accessor.hh> +#include <xtd/mutator.hh> + + +struct test +{ + typedef int member_type; + int member_; + int member() const { return member_; } + int& member() { return member_; } +}; + + +xtd_decl_accessor(member); +xtd_decl_mutator(member); + + +int main() +{ + test obj; + xtd::mutator::member(obj) = 51; + assert( xtd::accessor::member(obj) == 51 ); + assert( xtd::accessor::member(obj) == xtd::mutator::member(obj) ); +} Index: tests/Makefile.am =================================================================== --- tests/Makefile.am (revision 663) +++ tests/Makefile.am (working copy) @@ -15,6 +15,7 @@ check_PROGRAMS = \ abs \ + accessor \ bind \ cast \ cfun \ @@ -25,6 +26,7 @@ optraits abs_SOURCES = abs.cc +accessor_SOURCES = accessor.cc bind_SOURCES = bind.cc cast_SOURCES = cast.cc cfun_SOURCES = cfun.cc
participants (1)
-
Thierry GERAUD