function_list<>: Simplify with C++11

This commit is contained in:
Thomas Geymayer 2017-12-04 07:50:47 +01:00
parent 1f12966628
commit 68053d64b5
3 changed files with 26 additions and 58 deletions

View File

@ -28,10 +28,6 @@ set(HEADERS
StateMachine.hxx
)
set(DETAIL_HEADERS
detail/function_list_template.hxx
)
set(SOURCES
SGAtomic.cxx
SGBinding.cxx
@ -48,7 +44,6 @@ set(SOURCES
)
simgear_component(structure structure "${SOURCES}" "${HEADERS}")
simgear_component(structure/detail structure/detail "" "${DETAIL_HEADERS}")
if(ENABLE_TESTS)

View File

@ -1,37 +0,0 @@
#ifndef SG_FUNCTION_LIST_HXX_
# error function_list - do not include this file!
#endif
#ifndef SG_DONT_DO_ANYTHING
#define n BOOST_PP_ITERATION()
#define SG_FUNC_TYPE boost::function<Ret (BOOST_PP_ENUM_PARAMS(n, A))>
#define SG_LIST_TYPE std::vector<SG_FUNC_TYPE >
template<class Ret BOOST_PP_ENUM_TRAILING_PARAMS(n, class A)>
class function_list<Ret (BOOST_PP_ENUM_PARAMS(n, A))>:
public SG_LIST_TYPE
{
public:
typedef SG_FUNC_TYPE function_type;
typedef typename SG_LIST_TYPE::iterator iterator;
typedef typename SG_LIST_TYPE::const_iterator const_iterator;
Ret operator()(BOOST_PP_ENUM_BINARY_PARAMS(n, A, a)) const
{
if( this->empty() )
return Ret();
const_iterator list_end = --this->end();
for(const_iterator f = this->begin(); f != list_end; ++f)
if( *f )
(*f)(BOOST_PP_ENUM_PARAMS(n, a));
return (*list_end) ? (*list_end)(BOOST_PP_ENUM_PARAMS(n, a)) : Ret();
}
};
#undef n
#undef SG_FUNC_TYPE
#undef SG_LIST_TYPE
#endif // SG_DONT_DO_ANYTHING

View File

@ -20,33 +20,43 @@
#define SG_FUNCTION_LIST_HXX_
#include <boost/function.hpp>
#include <boost/preprocessor/iteration/iterate.hpp>
#include <boost/preprocessor/repetition/enum_binary_params.hpp>
#include <boost/preprocessor/repetition/enum_trailing_params.hpp>
#include <boost/utility/enable_if.hpp>
#include <vector>
namespace simgear
{
template<typename Sig> class function_list;
// Build dependency for CMake, gcc, etc.
# define SG_DONT_DO_ANYTHING
# include <simgear/structure/detail/function_list_template.hxx>
# undef SG_DONT_DO_ANYTHING
# define BOOST_PP_ITERATION_LIMITS (0, 3)
# define BOOST_PP_FILENAME_1 <simgear/structure/detail/function_list_template.hxx>
# include BOOST_PP_ITERATE()
/**
* Handle a list of callbacks like a single boost::function.
*
* @tparam Sig Function signature.
*/
template<typename Sig>
class function_list<boost::function<Sig> >:
public function_list<Sig>
template<class Ret, class ... Args>
class function_list<Ret(Args...)>:
public std::vector<boost::function<Ret(Args...)>>
{
public:
Ret operator()(Args ... args) const
{
if( this->empty() )
return Ret();
auto list_end = --this->end();
for(auto f = this->begin(); f != list_end; ++f)
if( *f )
(*f)(args...);
return (*list_end) ? (*list_end)(args...) : Ret();
}
};
/**
* Handle a list of callbacks with the same signature as the given
* boost::function type.
*/
template<class Ret, class ... Args>
class function_list<boost::function<Ret(Args...)>>:
public function_list<Ret(Args...)>
{
};