增加伪函数类Function

This commit is contained in:
Nomango 2018-04-22 15:52:46 +08:00
parent 6578751de4
commit e318679d3d
4 changed files with 72 additions and 11 deletions

28
core/Common/Function.cpp Normal file
View File

@ -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<void()> func)
: m_func(func)
{
}
void e2d::Function::operator()(void) const
{
if (m_func)
{
m_func();
}
}
e2d::Function::operator bool() const
{
return static_cast<bool>(m_func);
}

View File

@ -11,17 +11,6 @@ namespace e2d
{
// 函数对象
typedef std::function<void()> Function;
// 创建函数对象
template<typename Object, typename Func>
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<void()> func
);
template<typename Func>
Function(
Func func
)
: m_func(func)
{
}
template<typename Func, typename Object>
Function(
Func&& func,
Object&& obj
)
{
m_func = std::bind(func, obj);
}
void operator() (void) const;
operator bool() const;
protected:
std::function<void()> m_func;
};
// 基础对象
class Object
{

View File

@ -216,6 +216,7 @@
<ClCompile Include="..\..\core\Collider\ColliderEllipse.cpp" />
<ClCompile Include="..\..\core\Collider\ColliderRect.cpp" />
<ClCompile Include="..\..\core\Common\Color.cpp" />
<ClCompile Include="..\..\core\Common\Function.cpp" />
<ClCompile Include="..\..\core\Common\TextStyle.cpp" />
<ClCompile Include="..\..\core\Common\Object.cpp" />
<ClCompile Include="..\..\core\Common\Point.cpp" />

View File

@ -228,6 +228,9 @@
<ClCompile Include="..\..\core\Common\Color.cpp">
<Filter>Common</Filter>
</ClCompile>
<ClCompile Include="..\..\core\Common\Function.cpp">
<Filter>Common</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="..\..\core\easy2d.h" />