#ifndef CORELIB_FUNC_H #define CORELIB_FUNC_H #if 0 #include "SmartPointer.h" namespace CoreLib { namespace Basic { template class FuncPtr { public: virtual TResult operator()(Arguments...) = 0; virtual bool operator == (const FuncPtr *) { return false; } virtual ~FuncPtr() {} }; template class CdeclFuncPtr : public FuncPtr { public: typedef TResult (*FuncType)(Arguments...); private: FuncType funcPtr; public: CdeclFuncPtr(FuncType func) :funcPtr(func) { } virtual TResult operator()(Arguments... params) override { return funcPtr(params...); } virtual bool operator == (const FuncPtr * ptr) override { auto cptr = dynamic_cast*>(ptr); if (cptr) return funcPtr == cptr->funcPtr; else return false; } }; template class MemberFuncPtr : public FuncPtr { public: typedef TResult (Class::*FuncType)(Arguments...); private: FuncType funcPtr; Class * object; public: MemberFuncPtr(Class * obj, FuncType func) : funcPtr(func), object(obj) { } virtual TResult operator()(Arguments... params) override { return (object->*funcPtr)(params...); } virtual bool operator == (const FuncPtr * ptr) override { auto cptr = dynamic_cast*>(ptr); if (cptr) return funcPtr == cptr->funcPtr && object == cptr->object; else return false; } }; template class LambdaFuncPtr : public FuncPtr { private: F func; public: LambdaFuncPtr(const F & _func) : func(_func) {} virtual TResult operator()(Arguments... params) override { return func(params...); } virtual bool operator == (const FuncPtr * /*ptr*/) override { return false; } }; template class Func { private: RefPtr> funcPtr; public: Func(){} Func(typename CdeclFuncPtr::FuncType func) { funcPtr = new CdeclFuncPtr(func); } template Func(Class * object, typename MemberFuncPtr::FuncType func) { funcPtr = new MemberFuncPtr(object, func); } template Func(const TFuncObj & func) { funcPtr = new LambdaFuncPtr(func); } Func & operator = (typename CdeclFuncPtr::FuncType func) { funcPtr = new CdeclFuncPtr(func); return *this; } template Func & operator = (const MemberFuncPtr & func) { funcPtr = new MemberFuncPtr(func); return *this; } template Func & operator = (const TFuncObj & func) { funcPtr = new LambdaFuncPtr(func); return *this; } bool operator == (const Func & f) { return *funcPtr == f.funcPtr.Ptr(); } bool operator != (const Func & f) { return !(*this == f); } TResult operator()(Arguments... params) { return (*funcPtr)(params...); } }; // template // using Procedure = Func; template class Procedure : public Func { private: RefPtr> funcPtr; public: Procedure(){} Procedure(const Procedure & proc) { funcPtr = proc.funcPtr; } Procedure(typename CdeclFuncPtr::FuncType func) { funcPtr = new CdeclFuncPtr(func); } template Procedure(Class * object, typename MemberFuncPtr::FuncType func) { funcPtr = new MemberFuncPtr(object, func); } template Procedure(const TFuncObj & func) { funcPtr = new LambdaFuncPtr(func); } Procedure & operator = (typename CdeclFuncPtr::FuncType func) { funcPtr = new CdeclFuncPtr(func); return *this; } template Procedure & operator = (const MemberFuncPtr & func) { funcPtr = new MemberFuncPtr(func); return *this; } template Procedure & operator = (const TFuncObj & func) { funcPtr = new LambdaFuncPtr(func); return *this; } Procedure & operator = (const Procedure & proc) { funcPtr = proc.funcPtr; } void Clear() { funcPtr = nullptr; } void operator()(Arguments... params) { if (funcPtr) (*funcPtr)(params...); } }; } } #endif #endif