2013-11-30 13:08:38 +03:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* 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
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
/// List of functions that share the same signature - can be used to call all of them easily
|
|
|
|
template<typename Signature>
|
|
|
|
class CFunctionList
|
|
|
|
{
|
|
|
|
public:
|
2014-08-04 21:33:59 +03:00
|
|
|
std::vector<std::function<Signature> > funcs;
|
2013-11-30 13:08:38 +03:00
|
|
|
|
|
|
|
CFunctionList(int){};
|
|
|
|
CFunctionList(){};
|
|
|
|
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-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);
|
|
|
|
}
|
|
|
|
CFunctionList(std::nullptr_t)
|
|
|
|
{}
|
2014-08-04 21:33:59 +03:00
|
|
|
CFunctionList & operator+=(const std::function<Signature> &first)
|
2013-11-30 13:08:38 +03:00
|
|
|
{
|
|
|
|
funcs.push_back(first);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
void add(const CFunctionList<Signature> &first)
|
|
|
|
{
|
|
|
|
for (size_t i = 0; i < first.funcs.size(); i++)
|
|
|
|
{
|
|
|
|
funcs.push_back(first.funcs[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
void clear()
|
|
|
|
{
|
|
|
|
funcs.clear();
|
|
|
|
}
|
|
|
|
operator bool() const
|
|
|
|
{
|
|
|
|
return funcs.size();
|
|
|
|
}
|
|
|
|
void operator()() const
|
|
|
|
{
|
2014-08-04 21:33:59 +03:00
|
|
|
std::vector<std::function<Signature> > funcs2 = funcs; //backup
|
2013-11-30 13:08:38 +03:00
|
|
|
for(size_t i=0;i<funcs2.size(); ++i)
|
|
|
|
{
|
|
|
|
if (funcs2[i])
|
|
|
|
funcs2[i]();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
template <typename Arg>
|
|
|
|
void operator()(const Arg & a) const
|
|
|
|
{
|
2014-08-04 21:33:59 +03:00
|
|
|
std::vector<std::function<Signature> > funcs2 = funcs; //backup
|
2013-11-30 13:08:38 +03:00
|
|
|
for(int i=0;i<funcs2.size(); i++)
|
|
|
|
{
|
|
|
|
if (funcs2[i])
|
|
|
|
funcs2[i](a);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Me wants variadic templates :(
|
|
|
|
template <typename Arg1, typename Arg2>
|
|
|
|
void operator()(Arg1 & a, Arg2 & b) const
|
|
|
|
{
|
2014-08-04 21:33:59 +03:00
|
|
|
std::vector<std::function<Signature> > funcs2 = funcs; //backup
|
2013-11-30 13:08:38 +03:00
|
|
|
for(int i=0;i<funcs2.size(); i++)
|
|
|
|
{
|
|
|
|
if (funcs2[i])
|
|
|
|
funcs2[i](a, b);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|