Commit de320d4b authored by Thierry Geraud's avatar Thierry Geraud
Browse files

First draft of extended functions.

- xtd/math.hh: New file.
- xtd/res.hh: New file.
- xtd/cast.hh: New file.
- xtd/math: New file.
- xtd/math/trigo.hh: New file.
- xtd/math/arith.hh: New file.
- xtd/math/id.hh: New file.
- xtd/mexpr.hh: New file.
- xtd/args.hh: New file.
- xtd/literal.hh: New file.
- xtd/ops_expr.hh: New file.
- xtd/abstract: New file.
- xtd/abstract/nary_fun.hh: New file.
- xtd/abstract/exact.hh: New file.
- xtd/abstract/fun_expr.hh: New file.
- xtd/abstract/any.hh: New file.
- xtd/abstract/fun.hh: New file.
- xtd/abstract/plain_fun.hh: New file.
- xtd/abstract/meta_fun.hh: New file.
- xtd/mfun.hh: New file.
- xtd/traits.hh: New file.
- xtd/builtin: New file.
- xtd/builtin/traits.hh: New file.
- xtd/arg.hh: New file.
- xtd/case.hh: New file.
- tests/id.cc: New file.
- tests/cos.cc: New file.
- tests/cast.cc: New file.
- tests/Makefile.am: Update.


git-svn-id: https://svn.lrde.epita.fr/svn/oln/trunk@434 4aad255d-cdde-0310-9447-f3009e2ae8c0
parent 6a732930
2006-03-22 Thierry Geraud <theo@lrde.epita.fr>
First draft of extended functions.
* xtd/math.hh: New file.
* xtd/res.hh: New file.
* xtd/cast.hh: New file.
* xtd/math: New file.
* xtd/math/trigo.hh: New file.
* xtd/math/arith.hh: New file.
* xtd/math/id.hh: New file.
* xtd/mexpr.hh: New file.
* xtd/args.hh: New file.
* xtd/literal.hh: New file.
* xtd/ops_expr.hh: New file.
* xtd/abstract: New file.
* xtd/abstract/nary_fun.hh: New file.
* xtd/abstract/exact.hh: New file.
* xtd/abstract/fun_expr.hh: New file.
* xtd/abstract/any.hh: New file.
* xtd/abstract/fun.hh: New file.
* xtd/abstract/plain_fun.hh: New file.
* xtd/abstract/meta_fun.hh: New file.
* xtd/mfun.hh: New file.
* xtd/traits.hh: New file.
* xtd/builtin: New file.
* xtd/builtin/traits.hh: New file.
* xtd/arg.hh: New file.
* xtd/case.hh: New file.
* tests/id.cc: New file.
* tests/cos.cc: New file.
* tests/cast.cc: New file.
* tests/Makefile.am: Update.
2006-03-14 Roland Levillain <roland@lrde.epita.fr>
* tests/Makefile.am (mlcdir, nobase_mlc_HEADERS): Remove.
......
## Process this file through Automake to create Makefile.in -*- Makefile -*-
AM_CPPFLAGS = -I$(top_srcdir)/extended -I$(top_srcdir)/metalic
AM_CPPFLAGS = -I$(top_srcdir)/extended
# FIXME: Add
#
# AM_CXXFLAGS = $(CXXFLAGS_STRICT) $(CXXFLAGS_OPTIMIZE) -ggdb
#
# when oln.m4 is available in the distribution.
check_PROGRAMS = \
id \
cast \
cos
id_SOURCES = id.cc
cast_SOURCES = cast.cc
cos_SOURCES = cos.cc
TESTS = $(check_PROGRAMS)
#include <iostream>
#include <xtd/cast.hh>
#include <xtd/arg.hh>
#include <xtd/math/trigo.hh>
int main()
{
{
xtd::plain_cast_<int, float> fun;
std::cout << fun(3.14) << std::endl;
}
{
xtd::meta_cast_<int> fun;
std::cout << fun(3.14) << std::endl;
}
{
using xtd::cast_;
using xtd::cos;
using xtd::_1;
std::cout << (cast_<int>(cos(_1)))(3.14) << std::endl;
std::cout << cos(cast_<float>(_1))(3) << std::endl;
}
// {
// xtd::cos_<int> c;
// }
// {
// using xtd::cos;
// cos(1);
// }
}
#include <iostream>
#include <xtd/math.hh>
int main()
{
// {
// using xtd::cos;
// std::cout << cos(5.1f) << std::endl;
// }
// {
// using xtd::cos;
// using xtd::sin;
// using xtd::_1;
// using xtd::_2;
// std::cout << cos(_1)(5.1f) << std::endl;
// std::cout << (cos(_1) + sin(_2))(5.f, 1.f) << std::endl;
// std::cout << (cos(_1) + sin(_2)).nargs() << std::endl;
// }
// {
// using xtd::cos;
// using xtd::_1;
// using xtd::_2;
// std::cout << (cos(_1)(_2)).nargs() << std::endl; // gives: 2
// }
{
using xtd::cos;
using xtd::sin;
using xtd::_1;
using xtd::_2;
// std::cout << (cos(_1) * cos(_1) + sin(_2) * sin(_2))(5.f, 5.f) << std::endl;
// std::cout << (_1)(_1)(51.f) << std::endl;
// std::cout << (_1 + _1)(51.f) << std::endl;
// std::cout << (_1 + _2)(5.f, 1.f) << std::endl;
// (cos(_1) + sin(_1))(_1);
// std::cout << (cos(_1) * cos(_1) + sin(_1) * sin(_1))(_1)(51.f) << std::endl;
std::cout << (_1 * _1 + _2 * _2)(cos(_1), sin(_1))(51.f) << std::endl;
// std::cout << ((cos(_1) + sin(_2))(_1, _1)).nargs() << std::endl;
}
}
#include <iostream>
#include <xtd/math/id.hh>
// namespace xtd
// {
// struct xfail;
// template <>
// struct fun_traits_< xfail >
// {
// typedef int res_type;
// };
// struct xfail : public xtd::abstract::plain_fun_< xfail >
// {
// };
// }
int main()
{
// {
// xtd::xfail test;
// }
{
xtd::plain_id_<int> id_int;
std::cout << id_int(51) << std::endl;
xtd::plain_id_<float> id_float;
std::cout << id_float(5.1f) << std::endl;
}
}
// Copyright (C) 2002, 2005, 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, 59 Temple Place - Suite 330, 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 EXTENDED_ABSTRACT_ANY_HH
# define EXTENDED_ABSTRACT_ANY_HH
namespace xtd
{
namespace abstract
{
/*! \class xtd::abstract::any
**
** Abstract base class for static hierarchies in xtd. Parameter E
** is the exact type.
*/
template <class E>
class any
{
public:
typedef E exact_type;
// exact is not provided by stc::any so that xtd is independant
// from the stc lib.
E& exact() { return *(E*)(void*)(this); }
const E& exact() const { return *(const E*)(const void*)(this); }
protected:
any() {}
};
} // end of namespace abstract::xtd
} // end of namespace xtd
#endif // ! EXTENDED_ABSTRACT_ANY_HH
// Copyright (C) 2002, 2005, 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, 59 Temple Place - Suite 330, 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 EXTENDED_ABSTRACT_EXACT_HH
# define EXTENDED_ABSTRACT_EXACT_HH
namespace xtd
{
namespace abstract
{
template <typename E> class plain_fun_;
template <typename E> class meta_fun_;
template <typename E> class fun_expr_;
} // end of namespace xtd::abstract
// plain_fun_
template <typename E>
const E* exact_of(const abstract::plain_fun_<E>* f)
{
return (const E*)(const void*)(f);
}
template <typename E>
const E& exact_of(const abstract::plain_fun_<E>& f)
{
return *exact_of(&f);
}
// meta_fun_
template <typename E>
const E* exact_of(const abstract::meta_fun_<E>* f)
{
return (const E*)(const void*)(f);
}
template <typename E>
const E& exact_of(const abstract::meta_fun_<E>& f)
{
return *exact_of(&f);
}
// fun_expr_
template <typename E>
const E* exact_of(const abstract::fun_expr_<E>* f)
{
return (const E*)(const void*)(f);
}
template <typename E>
const E& exact_of(const abstract::fun_expr_<E>& f)
{
return *exact_of(&f);
}
} // end of namespace xtd
#endif // ! EXTENDED_ABSTRACT_EXACT_HH
// Copyright (C) 2002, 2005, 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, 59 Temple Place - Suite 330, 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 EXTENDED_ABSTRACT_FUN_HH
# define EXTENDED_ABSTRACT_FUN_HH
namespace xtd
{
namespace abstract
{
/*! \class xtd::abstract::fun_
**
** Abstract base class for xtd functions. Parameter E is the
** exact type of the function.
*/
template <typename E>
class fun_
{
protected:
fun_() {}
};
} // end of namespace xtd::abstract
} // end of namespace xtd
#endif // ! EXTENDED_ABSTRACT_FUN_HH
// Copyright (C) 2002, 2005, 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, 59 Temple Place - Suite 330, 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 EXTENDED_ABSTRACT_FUN_EXPR_HH
# define EXTENDED_ABSTRACT_FUN_EXPR_HH
# include <mlc/bool.hh>
# include <mlc/is_a.hh>
# include <mlc/comma.hh>
# include <xtd/args.hh>
# include <xtd/res.hh>
# include <xtd/abstract/exact.hh>
# include <xtd/abstract/meta_fun.hh>
namespace xtd
{
namespace ERROR
{
struct INTERNAL_ILL_FORMED_CALL_TO_xtd_fun_expr_eval;
} // end of namespace xtd::ERROR
namespace abstract
{
/*! \class xtd::abstract::fun_expr_<E>
**
** Abstract base class for function expressions. Parameter E is
** the exact type of the function expression.
**
** Design note: this class does not derive from xtd::abstract::any
** to avoid diamond inheritance since fun_expr classes are also
** meta_fun classes.
*/
template <typename E>
class fun_expr_
// FIXME: at that point, we should verify that nargs_<E> is user-defined...
: public fun_<E>
{
public:
// evaluation:
template <typename Args>
xtd_expr_res(E, Args)
eval(const Args& as) const
{
mlc::assert_< mlc_is_a(Args, xtd::abstract::args),
xtd::ERROR::INTERNAL_ILL_FORMED_CALL_TO_xtd_fun_expr_eval >::check();
return exact_of(this)->impl_eval(as);
}
// FIXME: the get_nargs method is obsolete; remove it!
unsigned nargs() const { return xtd_nargs(E); }
protected:
fun_expr_() {}
};
/*! \class xtd::abstract::nary_fun_expr_<n, E>
**
** Abstract base class for function expressions with an explicit
** number of arguments.
**
** Parameter n is the number of arguments with n being 0, 1, 2, or
** 3.
**
** Parameter E is the exact type of the function.
*/
template <unsigned n, typename E>
class nary_fun_expr_;
/*! \class xtd::abstract::nary_fun_expr_<0, E>
**
** Abstract base class for function expressions taking no
** argument.
**
** Parameter E is the exact type of the function.
*/
template <typename E>
class nary_fun_expr_< 0, E >
: public fun_expr_<E>,
public meta_nary_fun_<0, E>
{
public:
xtd_res_0(E) impl_calc() const
{
return this->eval(mk_args());
}
protected:
nary_fun_expr_() {}
};
/*! \class xtd::abstract::nary_fun_expr_<1, E>
**
** Abstract base class for function expressions taking one
** argument.
**
** Parameter E is the exact type of the function.
*/
template <typename E>
class nary_fun_expr_< 1, E >
: public fun_expr_<E>,
public meta_nary_fun_<1, E>
{
public:
template <typename A>
xtd_res_1(E, A)
impl_calc(const A& a) const
{
return this->eval(mk_args(a));
}
protected:
nary_fun_expr_() {}
};
/*! \class xtd::abstract::nary_fun_expr_<2, E>
**
** Abstract base class for function expressions taking two
** arguments.
**
** Parameter E is the exact type of the function.
*/
template <typename E>
class nary_fun_expr_< 2, E >
: public fun_expr_<E>,
public meta_nary_fun_<2, E>
{
public:
template <typename A1, typename A2>
xtd_res_2(E, A1, A2)
impl_calc(const A1& a1, const A2& a2) const
{
return this->eval(mk_args(a1, a2));
}
protected:
nary_fun_expr_() {}
};
/*! \class xtd::abstract::nary_fun_expr_<3, E>
**
** Abstract base class for function expressions taking three
** arguments.
**
** Parameter E is the exact type of the function.
*/
template <typename E>
class nary_fun_expr_< 3, E >
: public fun_expr_<E>,
public meta_nary_fun_<3, E>
{
public:
template <typename A1, typename A2, typename A3>
xtd_res_3(E, A1, A2, A3)
impl_calc(const A1& a1, const A2& a2, const A3& a3) const
{
return this->eval(mk_args(a1, a2, a3));
}
protected:
nary_fun_expr_() {}
};