mirror of
				https://github.com/vcmi/vcmi.git
				synced 2025-10-31 00:07:39 +02:00 
			
		
		
		
	* 0 is not convertible to std::function, nullptr should be used * std::ref(rand) is not convertible to function<int()>, used lambdas (why we dont just pass "rand" ? ) * CFunctionList needs to be constructible from nullptr * move constructor for CMapInfo (Visual cannot generate them :( ) * #ifdefed some stuff that is not needed anymore since cmath is updated with C99 stuff * using std::make_unique instead of our vstd implementation CSelector: * introduced a class in place of typedef * Having an overloaded && || operators over sth that is convertible to bool… Wasn't a good idea after all. Purged the operators, replaced with And/Or methods (chaining-style). * constructor that is present only when constructing from class or function (SFINAE). std::function has an implicit converting constructor from T causing ambiguities (even if the overload would cause compile error in the body)
		
			
				
	
	
		
			133 lines
		
	
	
		
			2.8 KiB
		
	
	
	
		
			C++
		
	
	
	
	
	
			
		
		
	
	
			133 lines
		
	
	
		
			2.8 KiB
		
	
	
	
		
			C++
		
	
	
	
	
	
| #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:
 | |
| 	std::vector<boost::function<Signature> > funcs;
 | |
| 
 | |
| 	CFunctionList(int){};
 | |
| 	CFunctionList(){};
 | |
| 	template <typename Functor> 
 | |
| 	CFunctionList(const Functor &f)
 | |
| 	{
 | |
| 		funcs.push_back(boost::function<Signature>(f));
 | |
| 	}
 | |
| 	CFunctionList(const boost::function<Signature> &first)
 | |
| 	{
 | |
| 		funcs.push_back(first);
 | |
| 	}
 | |
| 	CFunctionList(boost::function<Signature> &first)
 | |
| 	{
 | |
| 		funcs.push_back(first);
 | |
| 	}
 | |
| 	CFunctionList(std::nullptr_t)
 | |
| 	{}
 | |
| 	CFunctionList & operator+=(const boost::function<Signature> &first)
 | |
| 	{
 | |
| 		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
 | |
| 	{
 | |
| 		std::vector<boost::function<Signature> > funcs2 = funcs; //backup
 | |
| 		for(size_t i=0;i<funcs2.size(); ++i) 
 | |
| 		{
 | |
| 			if (funcs2[i])
 | |
| 				funcs2[i]();
 | |
| 		}
 | |
| 	}
 | |
| 	template <typename Arg> 
 | |
| 	void operator()(const Arg & a) const
 | |
| 	{
 | |
| 		std::vector<boost::function<Signature> > funcs2 = funcs; //backup
 | |
| 		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
 | |
| 	{
 | |
| 		std::vector<boost::function<Signature> > funcs2 = funcs; //backup
 | |
| 		for(int i=0;i<funcs2.size(); i++)
 | |
| 		{
 | |
| 			if (funcs2[i])
 | |
| 				funcs2[i](a, b);
 | |
| 		}
 | |
| 	}
 | |
| };
 | |
| 
 | |
| 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
 | |
| 		for(size_t i=0;i<funcs2.size(); ++i) 
 | |
| 		{
 | |
| 			funcs2[i](a);
 | |
| 		}
 | |
| 	}
 | |
| }; |