/* named_params_example_header.hpp header file * * Copyright 2004 Cromwell D. Enage. Distributed under the Boost Software * License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at * http://www.boost.org/LICENSE_1_0.txt) */ #ifndef NAMED_PARAMS_EXAMPLE_HEADER_HPP #define NAMED_PARAMS_EXAMPLE_HEADER_HPP /* * Defines the boost::is_arithmetic metafunction. */ #include /* * Defines the boost::is_integral metafunction. */ #include /* * Defines the boost::remove_const metafunction. */ #include /* * Defines the boost::remove_reference metafunction. */ #include /* * Defines the boost::mpl::true_ and boost::mpl::false_ structs. */ #include /* * Defines the boost::mpl::_ typedef. */ #include /* * Defines the Boost.NamedParams library components. */ #include namespace zoo { struct sound_param; struct silence_param; struct timbre_param; struct volume_param; struct echo_count_param; template struct keywords_t : boost::keywords< boost::named_param< sound_param , boost::mpl::false_ > , boost::named_param< silence_param , boost::mpl::false_ > , boost::named_param< timbre_param , BooleanIntegralConstant > , boost::named_param< volume_param , boost::mpl::true_ , boost::is_arithmetic > , boost::named_param< echo_count_param , boost::mpl::true_ , boost::is_integral > > { }; namespace { boost::keyword sound_arg; boost::keyword silence_arg; boost::keyword timbre_arg; boost::keyword volume_arg; boost::keyword echo_count_arg; } // unnamed namespace template class timbre { private: const CharType* _s; OutputStream& _os; timbre() : _os(), _s() { } timbre(const timbre& copy) : _os(copy._os), _s(copy._s) { } public: timbre(OutputStream& os, const CharType* c) : _os(os), _s(c) { } void operator()(const CharType* sound, const CharType* silence) { _os << _s << ": " << sound << silence; } }; namespace voice_box { template < typename Sound , typename Timbre , typename VolumeType , typename EchoCountType > bool make_sound( Sound sound , Sound silence , Timbre& timbre , VolumeType volume , EchoCountType num_echoes ) { typedef typename boost::remove_const< typename boost::remove_reference::type >::type Volume; typedef typename boost::remove_const< typename boost::remove_reference::type >::type EchoCount; for (EchoCount count = EchoCount(); count < num_echoes; ++count) { timbre(sound, silence); } return volume > Volume(); } } // namespace voice_box BOOST_NAMED_PARAMS_FUN( bool // return type , make_animal_sound // name of function , 3 // number of arguments without defaults , 5 // total number of arguments , keywords_t // keyword restriction struct ) { return voice_box::make_sound( p[sound_arg] , p[silence_arg] , p[timbre_arg] , p[volume_arg | 0.0f] , p[echo_count_arg | 1] ); } template class parrot { private: OutputStream& _os; parrot() : _os() { } parrot(const parrot& copy) : _os(copy._os) { } public: explicit parrot(OutputStream& os) : _os(os) { } BOOST_NAMED_PARAMS_MEMBER_FUN( bool // return type , make_sound // name of function , 2 // number of arguments without defaults , 5 // total number of arguments , keywords_t // keyword restriction struct ) { timbre parrot_timbre(_os, "Squawwwk"); return voice_box::make_sound( p[sound_arg] , p[silence_arg] , p[timbre_arg | parrot_timbre] , p[volume_arg | 0.0f] , p[echo_count_arg | 1] ); } }; } // namespace zoo #endif /* NAMED_PARAMS_EXAMPLE_HEADER_HPP */