Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // File: main.cpp, created on Sep 14, 2011 by WeltEnSTurm
- #include "functor.hpp"
- #include <iostream>
- #include <string>
- #define DEBUG
- #ifdef DEBUG
- #define DEBUGDO(x) {x}
- #else
- #define DEBUGDO(x)
- #endif
- void test1(std::string s){
- std::cout << "Hello, I'm test1 with " << s << std::endl;
- }
- void test2(std::string s){
- std::cout << "Hello, I'm test2 with " << s << std::endl;
- }
- void test3(std::string s){
- std::cout << "Hello, I'm test3 with " << s << std::endl;
- }
- int main(){
- DEBUGDO(
- std::cout << "What?" << std::endl;
- );
- functor<std::string> test;
- test += test1;
- test += test2;
- test += test3;
- test("good morning");
- return 0;
- }
- // File: functor.hpp, created on Sep 14, 2011 by WeltEnSTurm
- #ifndef FUNCTOR_HPP_
- #define FUNCTOR_HPP_
- #include <vector>
- template<typename... T_args>
- class functor {
- protected:
- typedef void(*__func)(T_args...);
- std::vector<__func> mFunctions;
- public:
- void operator () (T_args... a){
- for(long i=0; i<(long)mFunctions.size(); i++)
- mFunctions[i](a...);
- }
- void operator () (__func f){
- mFunctions.push_back(f);
- }
- void operator = (__func f){
- clear();
- mFunctions.push_back(f);
- }
- void operator += (__func f){
- mFunctions.push_back(f);
- }
- void operator -= (__func f){
- for(long i=0; i<mFunctions.size(); i++)
- if(mFunctions[i] == f){
- mFunctions.erase(mFunctions.begin() + i);
- break;
- }
- }
- void clear(){
- mFunctions.clear();
- }
- };
- #endif /* FUNCTOR_HPP_ */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement