2008-12-21 21:17:35 +02:00
|
|
|
#ifndef FUNCTIONLIST_H
|
|
|
|
#define FUNCTIONLIST_H
|
|
|
|
|
2008-08-15 15:11:42 +03:00
|
|
|
#include <boost/function.hpp>
|
2009-05-19 21:23:04 +03:00
|
|
|
#include <vector>
|
2008-08-15 15:11:42 +03:00
|
|
|
|
2009-04-15 17:03:31 +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
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2011-02-22 13:52:36 +02:00
|
|
|
/// List of functions that share the same signature - can be used to call all of them easily
|
2008-08-20 09:57:53 +03:00
|
|
|
template<typename Signature>
|
2008-08-15 15:11:42 +03:00
|
|
|
class CFunctionList
|
|
|
|
{
|
|
|
|
public:
|
2008-08-20 09:57:53 +03:00
|
|
|
std::vector<boost::function<Signature> > funcs;
|
2008-08-15 15:11:42 +03:00
|
|
|
|
|
|
|
CFunctionList(int){};
|
|
|
|
CFunctionList(){};
|
2008-08-25 13:25:16 +03:00
|
|
|
template <typename Functor>
|
|
|
|
CFunctionList(const Functor &f)
|
|
|
|
{
|
|
|
|
funcs.push_back(boost::function<Signature>(f));
|
|
|
|
}
|
2008-08-20 09:57:53 +03:00
|
|
|
CFunctionList(const boost::function<Signature> &first)
|
2008-08-15 15:11:42 +03:00
|
|
|
{
|
|
|
|
funcs.push_back(first);
|
|
|
|
}
|
2008-08-22 15:21:09 +03:00
|
|
|
CFunctionList(boost::function<Signature> &first)
|
|
|
|
{
|
|
|
|
funcs.push_back(first);
|
|
|
|
}
|
2008-08-20 09:57:53 +03:00
|
|
|
CFunctionList & operator+=(const boost::function<Signature> &first)
|
2008-08-15 15:11:42 +03:00
|
|
|
{
|
|
|
|
funcs.push_back(first);
|
|
|
|
return *this;
|
|
|
|
}
|
2009-04-14 15:47:09 +03:00
|
|
|
void add(const CFunctionList<Signature> &first)
|
|
|
|
{
|
|
|
|
for (size_t i = 0; i < first.funcs.size(); i++)
|
|
|
|
{
|
|
|
|
funcs.push_back(first.funcs[i]);
|
|
|
|
}
|
|
|
|
}
|
2008-08-28 20:36:34 +03:00
|
|
|
void clear()
|
|
|
|
{
|
|
|
|
funcs.clear();
|
|
|
|
}
|
2008-08-15 15:11:42 +03:00
|
|
|
operator bool() const
|
|
|
|
{
|
|
|
|
return funcs.size();
|
|
|
|
}
|
|
|
|
void operator()() const
|
|
|
|
{
|
2008-08-20 09:57:53 +03:00
|
|
|
std::vector<boost::function<Signature> > funcs2 = funcs; //backup
|
2009-04-14 15:47:09 +03:00
|
|
|
for(size_t i=0;i<funcs2.size(); ++i)
|
|
|
|
{
|
2008-08-15 15:11:42 +03:00
|
|
|
funcs2[i]();
|
2009-04-14 15:47:09 +03:00
|
|
|
}
|
2008-08-15 15:11:42 +03:00
|
|
|
}
|
2008-08-22 15:21:09 +03:00
|
|
|
template <typename Arg>
|
|
|
|
void operator()(const Arg & a) const
|
|
|
|
{
|
|
|
|
std::vector<boost::function<Signature> > funcs2 = funcs; //backup
|
2009-04-14 15:47:09 +03:00
|
|
|
for(int i=0;i<funcs2.size(); i++)
|
|
|
|
{
|
2008-08-22 15:21:09 +03:00
|
|
|
funcs2[i](a);
|
2009-04-14 15:47:09 +03:00
|
|
|
}
|
2008-08-22 15:21:09 +03:00
|
|
|
}
|
2008-09-24 11:15:49 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
template<typename Signature>
|
|
|
|
class CFunctionList2
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
std::vector<boost::function<Signature> > funcs;
|
|
|
|
|
|
|
|
CFunctionList2(int){};
|
|
|
|
CFunctionList2(){};
|
|
|
|
template <typename Functor>
|
|
|
|
CFunctionList2(const Functor &f)
|
|
|
|
{
|
|
|
|
funcs.push_back(boost::function<Signature>(f));
|
|
|
|
}
|
|
|
|
CFunctionList2(const boost::function<Signature> &first)
|
|
|
|
{
|
|
|
|
funcs.push_back(first);
|
|
|
|
}
|
|
|
|
CFunctionList2(boost::function<Signature> &first)
|
|
|
|
{
|
|
|
|
funcs.push_back(first);
|
|
|
|
}
|
|
|
|
CFunctionList2 & operator+=(const boost::function<Signature> &first)
|
|
|
|
{
|
|
|
|
funcs.push_back(first);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
void clear()
|
|
|
|
{
|
|
|
|
funcs.clear();
|
|
|
|
}
|
|
|
|
operator bool() const
|
|
|
|
{
|
|
|
|
return funcs.size();
|
|
|
|
}
|
|
|
|
template <typename Arg>
|
|
|
|
void operator()(const Arg & a) const
|
|
|
|
{
|
|
|
|
std::vector<boost::function<Signature> > funcs2 = funcs; //backup
|
2009-04-14 15:47:09 +03:00
|
|
|
for(size_t i=0;i<funcs2.size(); ++i)
|
|
|
|
{
|
2008-09-24 11:15:49 +03:00
|
|
|
funcs2[i](a);
|
2009-04-14 15:47:09 +03:00
|
|
|
}
|
2008-09-24 11:15:49 +03:00
|
|
|
}
|
2008-12-21 21:17:35 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif //FUNCTIONLISt_H
|