2013-11-30 13:08:38 +03:00
|
|
|
/*
|
|
|
|
* FunctionList.h, part of VCMI engine
|
|
|
|
*
|
|
|
|
* Authors: listed in file AUTHORS in main folder
|
|
|
|
*
|
|
|
|
* License: GNU General Public License v2.0 or later
|
|
|
|
* Full text of license available in license.txt file, in main folder
|
|
|
|
*
|
|
|
|
*/
|
2017-07-13 10:26:03 +02:00
|
|
|
#pragma once
|
2013-11-30 13:08:38 +03:00
|
|
|
|
2022-07-26 15:07:42 +02:00
|
|
|
VCMI_LIB_NAMESPACE_BEGIN
|
|
|
|
|
2013-11-30 13:08:38 +03:00
|
|
|
/// List of functions that share the same signature - can be used to call all of them easily
|
|
|
|
template<typename Signature>
|
|
|
|
class CFunctionList
|
|
|
|
{
|
2014-08-04 21:33:59 +03:00
|
|
|
std::vector<std::function<Signature> > funcs;
|
2014-11-12 22:45:48 +02:00
|
|
|
public:
|
|
|
|
CFunctionList( std::nullptr_t ) { }
|
|
|
|
CFunctionList( int ) { }
|
|
|
|
CFunctionList( ){ }
|
2013-11-30 13:08:38 +03:00
|
|
|
|
|
|
|
template <typename Functor>
|
|
|
|
CFunctionList(const Functor &f)
|
|
|
|
{
|
2014-08-04 21:33:59 +03:00
|
|
|
funcs.push_back(std::function<Signature>(f));
|
2013-11-30 13:08:38 +03:00
|
|
|
}
|
2014-11-12 22:45:48 +02:00
|
|
|
|
2014-08-04 21:33:59 +03:00
|
|
|
CFunctionList(const std::function<Signature> &first)
|
2013-11-30 13:08:38 +03:00
|
|
|
{
|
|
|
|
if (first)
|
|
|
|
funcs.push_back(first);
|
|
|
|
}
|
2014-11-12 22:45:48 +02:00
|
|
|
|
|
|
|
CFunctionList & operator+=(const CFunctionList<Signature> &first)
|
2013-11-30 13:08:38 +03:00
|
|
|
{
|
2014-11-12 22:45:48 +02:00
|
|
|
for( auto & fun : first.funcs)
|
2013-11-30 13:08:38 +03:00
|
|
|
{
|
2014-11-12 22:45:48 +02:00
|
|
|
funcs.push_back(fun);
|
2013-11-30 13:08:38 +03:00
|
|
|
}
|
2014-11-12 22:45:48 +02:00
|
|
|
return *this;
|
2013-11-30 13:08:38 +03:00
|
|
|
}
|
2014-11-12 22:45:48 +02:00
|
|
|
|
2013-11-30 13:08:38 +03:00
|
|
|
void clear()
|
|
|
|
{
|
|
|
|
funcs.clear();
|
|
|
|
}
|
2014-11-12 22:45:48 +02:00
|
|
|
|
2013-11-30 13:08:38 +03:00
|
|
|
operator bool() const
|
|
|
|
{
|
2014-11-12 22:45:48 +02:00
|
|
|
return !funcs.empty();
|
2013-11-30 13:08:38 +03:00
|
|
|
}
|
2014-11-12 22:45:48 +02:00
|
|
|
|
|
|
|
template <typename... Args>
|
|
|
|
void operator()(Args ... args) const
|
2013-11-30 13:08:38 +03:00
|
|
|
{
|
2014-11-12 22:45:48 +02:00
|
|
|
std::vector<std::function<Signature> > funcs_copy = funcs;
|
|
|
|
for( auto & fun : funcs_copy)
|
2013-11-30 13:08:38 +03:00
|
|
|
{
|
2014-11-12 22:45:48 +02:00
|
|
|
if (fun)
|
|
|
|
fun(args...);
|
2013-11-30 13:08:38 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
2022-07-26 15:07:42 +02:00
|
|
|
|
|
|
|
VCMI_LIB_NAMESPACE_END
|