Skip to content

Instantly share code, notes, and snippets.

@wayt
Created November 30, 2013 15:29
Show Gist options
  • Save wayt/7720436 to your computer and use it in GitHub Desktop.
Save wayt/7720436 to your computer and use it in GitHub Desktop.
#ifndef FUNCTION_H_
# define FUNCTION_H_
#include <cstdlib>
template<class T>
class Function
{
public:
Function() : _obj(NULL), _func(NULL) {}
virtual ~Function() { delete _inner; }
template<class U>
Function(U* obj) : _obj(NULL), _func(NULL) { _obj = new InnerClass<U>(obj); }
Function(T* func) : _obj(NULL), _func(func) {}
template<class U>
void operator=(U* obj) { delete _obj; _obj = new InnerClass<U>(obj); _func = NULL; }
private:
template<class U>
class InnerClass
{
public:
InnerClass() : _obj(NULL) {}
InnerClass(U* obj) : _obj(obj) {}
private:
U* _obj;
};
InnerClass* _inner;
T* _func;
};
template<class Retour>
class Function<Retour(void)>
{
public:
Function(Retour(*func)(void)) : _obj(NULL), _func(func) {}
void operator=(Retour(*func)(void)) { _func = func; if (_inner) { delete _inner; _inner = NULL; } }
Retour operator()() { return _inner ? inner->operator()() : _func(); }
private:
template<class U>
class InnerClass
{
public:
Retour operator()(void) { return _obj(); }
};
Retour(*_func)(void);
};
template<class Retour, class T>
class Function<Retour(T)>
{
public:
Function(Retour(*func)(void)) : _obj(NULL), _func(func) {}
void operator=(Retour(*func)(T)) { _func = func; if (_inner) { delete _inner; _inner = NULL; } }
Retour operator()(T t) { return _inner ? inner->operator()(t) : _func(t); }
private:
template<class U>
class InnerClass
{
public:
Retour operator()(T t) { return _obj(t); }
};
Retour(*_func)(T);
};
template<class Retour, class T, class I>
class Function<Retour(T, I)>
{
public:
Function(Retour(*func)(void)) : _obj(NULL), _func(func) {}
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:
template<class U>
class InnerClass
{
public:
Retour operator()(T t, I i) { return _obj(t, i); }
};
Retour(*_func)(T, I);
};
template<class Retour, class T, class I, class K>
class Function<Retour(T, I, K)>
{
public:
Function(Retour(*func)(void)) : _obj(NULL), _func(func) {}
void operator=(Retour(*func)(T, I, K)) { _func = func; if (_inner) { delete _inner; _inner = NULL; } }
Retour operator()(T t, I i, K k) { return _inner ? inner->operator()(t, i, k) : _func(t, i, k); }
private:
template<class U>
class InnerClass
{
public:
Retour operator()(T t, I i, K k) { return _obj(t, i, k); }
};
Retour(*_func)(T, I, K);
};
template<class Retour, class T, class I, class K, class L>
class Function<Retour(T, I, K, L)>
{
public:
Function(Retour(*func)(void)) : _obj(NULL), _func(func) {}
void operator=(Retour(*func)(T, I, K, L)) { _func = func; if (_inner) { delete _inner; _inner = NULL; } }
Retour operator()(T t, I i, K k, L l) { return _inner ? inner->operator()(t, i, k, l) : _func(t, i, k, l); }
private:
template<class U>
class InnerClass
{
public:
Retour operator()(T t, I i, K k, L l) { return _obj(t, i, k, l); }
};
Retour(*_func)(T, I, K, L);
};
#endif /* !FUNCTION_H_ */
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment