Created
November 30, 2013 15:52
-
-
Save wayt/7720698 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
template<class Retour, class T, class I> | |
class Function<Retour(T, I)> | |
{ | |
public: | |
Function() : _inner(NULL), _func(NULL) {} | |
virtual ~Function() { delete _inner; } | |
template<class U> | |
Function(U* obj) : _inner(NULL), _func(NULL) { _inner = new InnerClass<U>(obj); } | |
Function(Retour(*func)(T, I)) : _inner(NULL), _func(func) {} | |
template<class U> | |
void operator=(U* obj) { delete _inner; _inner = new InnerClass<U>(obj); _func = NULL; } | |
void operator=(Retour(*func)(T, I)) { _func = func; if (_inner) { delete _inner; _inner = NULL; } } | |
Retour operator()(T t, I i) { return _inner ? _inner->operator()(t, i) : _func(t, i); } | |
private: | |
class IInnerClass | |
{ | |
public: | |
virtual ~IInnerClass() {} | |
virtual Retour operator()(T, I) = 0; | |
}; | |
template<class U> | |
class InnerClass : public IInnerClass | |
{ | |
public: | |
InnerClass() : _obj(NULL) {} | |
InnerClass(U* obj) : _obj(obj) {} | |
Retour operator()(T t, I i) { return _obj->operator()(t, i); } | |
private: | |
U* _obj; | |
}; | |
IInnerClass* _inner; | |
Retour(*_func)(T, I); | |
}; | |
int main() | |
{ | |
Function<int(char)> f = &function; | |
f('c'); | |
callable* call = new callable; | |
Function<void(std::string const&)> h = call; | |
h("Coucou"); | |
Function<void(char, char)> g = boost::bind(&secondfunction, _1, _2); | |
g('u', 'c'); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment