The Boost Parameter Library
参考:
1. 英文文档:
http://www.boost.org/doc/libs/1_54_0/libs/parameter/doc/html/index.html2. 中文文档:
http://stlchina.huhoo.net/boost/libs/parameter/doc/html/index.html3. 中文文档下载svn:
http://boost-doc-zh.googlecode.com/svn/trunk
测试代码:
#include <boost/parameter.hpp>
#include <boost/type_traits.hpp>
#include <boost/mpl/is_sequence.hpp>
#include <boost/mpl/void.hpp>
#include <boost/preprocessor/repetition.hpp>
#include <boost/noncopyable.hpp>
#include <string>
#include <iostream>
#include <memory>
namespace libnofx
{
BOOST_PARAMETER_NAME(host)
BOOST_PARAMETER_NAME(port)
BOOST_PARAMETER_NAME(handler)
BOOST_PARAMETER_NAME(io_service)
BOOST_PARAMETER_FUNCTION(
(bool), server_init, tag,
(required
(host, (std::string const &))
(port, (std::string const &))
(handler, *))
(optional
(io_service, *, 0))
)
{
host_type str;
std::cout << "host=" << host << std::endl;
std::cout << "port=" << port << std::endl;
std::cout << "handler=" << handler << std::endl;
std::cout << "io_service=" << io_service << std::endl;
return true;
}
}
namespace libnofx
{
BOOST_PARAMETER_NAME(arg1)
BOOST_PARAMETER_NAME(arg2)
struct callable2
{
BOOST_PARAMETER_CONST_MEMBER_FUNCTION(
(void), print/*operator()*/, tag,
(required
(arg1, (int))
(arg2, (int)))
)
{
std::cout << arg1 << ", " << arg2 << std::endl;
}
};
}
namespace libnofx
{
BOOST_PARAMETER_NAME(name)
BOOST_PARAMETER_NAME(index)
struct myclass_impl
{
template <class ArgumentPack>
myclass_impl(ArgumentPack const& args)
{
std::cout << "name = " << args[_name]
<< "; index = " << args[_index | 42]
<< std::endl;
}
};
struct myclass : myclass_impl
{
BOOST_PARAMETER_CONSTRUCTOR(
myclass, (myclass_impl), tag
, (required (name,*)) (optional (index,*))) // no semicolon
};
}
namespace libnofx
{
BOOST_PARAMETER_TEMPLATE_KEYWORD(class_type)
BOOST_PARAMETER_TEMPLATE_KEYWORD(base_list)
BOOST_PARAMETER_TEMPLATE_KEYWORD(held_type)
BOOST_PARAMETER_TEMPLATE_KEYWORD(copyable)
using namespace boost;
using boost::mpl::_;
# define BOOST_PYTHON_MAX_BASES 10
namespace detail { struct bases_base {}; }
// A type list for specifying bases
//template < BOOST_PP_ENUM_PARAMS_WITH_A_DEFAULT(BOOST_PYTHON_MAX_BASES, typename Base, mpl::void_) >
//struct bases : detail::bases_base
//{
//};
template <class A0 = void, class A1 = void, class A2 = void, class A3 = void, class A4 = void>
struct bases : detail::bases_base
{};
typedef parameter::parameters<
parameter::required<tag::class_type, is_class<_> >
, parameter::optional<
parameter::deduced<tag::base_list>
, is_base_and_derived<detail::bases_base, _>
>
, parameter::optional<
parameter::deduced<tag::held_type>
, mpl::not_<
mpl::or_<
is_base_and_derived<detail::bases_base, _>
, is_same<boost::noncopyable, _>
>
>
>
, parameter::optional<
parameter::deduced<tag::copyable>
, is_same<boost::noncopyable, _>
>
> class_signature;
template <
class A0
, class A1 = parameter::void_
, class A2 = parameter::void_
, class A3 = parameter::void_
>
struct class_
{
// Create ArgumentPack
typedef typename
class_signature::bind<A0,A1,A2,A3>::type args;
// Extract first logical parameter.
typedef typename parameter::binding<
args, tag::class_type>::type class_type;
typedef typename parameter::binding<
args, tag::base_list, bases<> >::type base_list;
typedef typename parameter::binding<
args, tag::held_type, class_type>::type held_type;
typedef typename parameter::binding<
args, tag::copyable, void>::type copyable;
};
struct B { virtual ~B(); };
struct D : B { ~D(); };
//typedef class_<class_type<B>, copyable<boost::noncopyable> > c1;
//typedef class_<D, held_type<std::auto_ptr<D> >, base_list<bases<B> > > c2;
typedef class_<B, boost::noncopyable> c1;
typedef class_<D, std::auto_ptr<D>, bases<B> > c2;
}
using namespace libnofx;
int main(int argc, char* argv[])
{
/* 2.1 Parameter-Enabled Functions
*/
bool success = false;
success = server_init("0.0.0.0", "4112", "0x12345678", 12345678);
success = server_init(_port="4113", _host="127.0.0.1", _handler="0x87654321");
/* 2.2 Parameter-Enabled Member
*/
int arg1 = 1;
int arg2 = 2;
callable2 ca2;
ca2.print(arg1, arg2);
/* 2.3 Parameter-Enabled Constructors
*/
myclass x("bob", 3); // positional
myclass y(_index = 12, _name = "sally"); // named
myclass z("june"); // positional/defaulted
/* 2.4 Parameter-Enabled Class Templates
*/
BOOST_MPL_ASSERT((boost::is_same<c1::class_type, B>));
BOOST_MPL_ASSERT((boost::is_same<c1::base_list, bases<> >));
BOOST_MPL_ASSERT((boost::is_same<c1::held_type, B>));
BOOST_MPL_ASSERT((boost::is_same<c1::copyable, boost::noncopyable>));
BOOST_MPL_ASSERT((boost::is_same<c2::class_type, D>));
BOOST_MPL_ASSERT((boost::is_same<c2::base_list, bases<B> >));
BOOST_MPL_ASSERT((boost::is_same<c2::held_type, std::auto_ptr<D> >));
BOOST_MPL_ASSERT((boost::is_same<c2::copyable, void>));
return 0;
}
keyword tag type 和 Parameter-Enabled Constructors 预编译后的代码样例:
namespace libnofx
{
namespace tag
{
struct name
{
static char const* keyword_name()
{
return "name";
}
typedef boost::parameter::value_type< boost::mpl::_2, name, boost::parameter::void_ > _;
typedef boost::parameter::value_type< boost::mpl::_2, name, boost::parameter::void_ > _1; };
}
namespace
{
::boost::parameter::keyword<tag::name> const& _name = ::boost::parameter::keyword<tag::name>::instance;
}
namespace tag
{
struct index
{
static char const* keyword_name()
{
return "index";
}
typedef boost::parameter::value_type< boost::mpl::_2, index, boost::parameter::void_ > _;
typedef boost::parameter::value_type< boost::mpl::_2, index, boost::parameter::void_ > _1; };
}
namespace
{
::boost::parameter::keyword<tag::index> const& _index = ::boost::parameter::keyword<tag::index>::instance;
}
struct myclass_impl
{
template <class ArgumentPack>
myclass_impl(ArgumentPack const& args)
{
std::cout << "name = " << args[_name]
<< "; index = " << args[_index | 42]
<< std::endl;
}
};
struct myclass : myclass_impl
{
template <class BoostParameterDummy>
struct boost_param_params_79ctor : boost::parameter::parameters<
boost::parameter::required<tag::name
, typename boost::parameter::aux::unwrap_predicate< void * >::type >
, boost::parameter::optional<tag::index
, typename boost::parameter::aux::unwrap_predicate< void * >::type >
>
{};
typedef boost_param_params_79ctor <int> constructor_parameters79;
template< class ParameterArgumentType0>
explicit myclass( const ParameterArgumentType0 & a0
, typename boost::parameter::aux::match< constructor_parameters79
, ParameterArgumentType0 >::type = constructor_parameters79() )
: boost::parameter::aux::unaryfunptr_arg_type< void(*)(myclass_impl) >::type (
constructor_parameters79()( a0 ) )
{}
template< class ParameterArgumentType0 , class ParameterArgumentType1>
myclass( const ParameterArgumentType0 & a0 , const ParameterArgumentType1 & a1
, typename boost::parameter::aux::match< constructor_parameters79, ParameterArgumentType0
, ParameterArgumentType1 >::type = constructor_parameters79() )
: boost::parameter::aux::unaryfunptr_arg_type< void(*)(myclass_impl) >::type
( constructor_parameters79()( a0 , a1 ) )
{}
};
}