diff --git a/core/Common/Function.cpp b/core/Common/Function.cpp new file mode 100644 index 00000000..bccc917a --- /dev/null +++ b/core/Common/Function.cpp @@ -0,0 +1,28 @@ +#include "..\e2dcommon.h" + +e2d::Function::Function() + : m_func(nullptr) +{} + +e2d::Function::Function(std::nullptr_t) + : m_func(nullptr) +{ +} + +e2d::Function::Function(std::function func) + : m_func(func) +{ +} + +void e2d::Function::operator()(void) const +{ + if (m_func) + { + m_func(); + } +} + +e2d::Function::operator bool() const +{ + return static_cast(m_func); +} \ No newline at end of file diff --git a/core/e2dcommon.h b/core/e2dcommon.h index 1da1c6c2..b37ccf4d 100644 --- a/core/e2dcommon.h +++ b/core/e2dcommon.h @@ -11,17 +11,6 @@ namespace e2d { -// 函数对象 -typedef std::function Function; - -// 创建函数对象 -template -inline Function CreateFunc(Object&& obj, Func&& func) -{ - return std::bind(func, obj); -} - - #if HIGHER_THAN_VS2012 // 初始化列表 @@ -544,6 +533,46 @@ struct NodeProperty }; +// 函数对象 +class Function +{ +public: + Function(); + + Function( + std::nullptr_t + ); + + Function( + std::function func + ); + + template + Function( + Func func + ) + : m_func(func) + { + } + + template + Function( + Func&& func, + Object&& obj + ) + { + m_func = std::bind(func, obj); + } + + void operator() (void) const; + + operator bool() const; + +protected: + std::function m_func; +}; + + // 基础对象 class Object { diff --git a/project/vs2017/Easy2D.vcxproj b/project/vs2017/Easy2D.vcxproj index 2d422607..9de76ccf 100644 --- a/project/vs2017/Easy2D.vcxproj +++ b/project/vs2017/Easy2D.vcxproj @@ -216,6 +216,7 @@ + diff --git a/project/vs2017/Easy2D.vcxproj.filters b/project/vs2017/Easy2D.vcxproj.filters index e2d4b8d2..34d8337d 100644 --- a/project/vs2017/Easy2D.vcxproj.filters +++ b/project/vs2017/Easy2D.vcxproj.filters @@ -228,6 +228,9 @@ Common + + Common +