From fd3c70d5f0557c8c2897a19dbcd1e64c4124c026 Mon Sep 17 00:00:00 2001
From: Nomango <569629550@qq.com>
Date: Thu, 24 Jan 2019 14:07:18 +0800
Subject: [PATCH] add: Closure function
---
project/vs2013/Easy2D.vcxproj | 1 +
project/vs2013/Easy2D.vcxproj.filters | 3 +
project/vs2015/Easy2D.vcxproj | 1 +
project/vs2015/Easy2D.vcxproj.filters | 3 +
project/vs2017/Easy2D.vcxproj | 1 +
project/vs2017/Easy2D.vcxproj.filters | 3 +
src/core/Scene.cpp | 8 +--
src/core/Scene.h | 4 +-
src/core/closure.hpp | 87 +++++++++++++++++++++++++++
src/core/helper.hpp | 1 +
src/ui/Button.cpp | 8 +--
11 files changed, 110 insertions(+), 10 deletions(-)
create mode 100644 src/core/closure.hpp
diff --git a/project/vs2013/Easy2D.vcxproj b/project/vs2013/Easy2D.vcxproj
index 7656da8a..eeb88e11 100644
--- a/project/vs2013/Easy2D.vcxproj
+++ b/project/vs2013/Easy2D.vcxproj
@@ -26,6 +26,7 @@
+
diff --git a/project/vs2013/Easy2D.vcxproj.filters b/project/vs2013/Easy2D.vcxproj.filters
index 2111540b..39446fc8 100644
--- a/project/vs2013/Easy2D.vcxproj.filters
+++ b/project/vs2013/Easy2D.vcxproj.filters
@@ -197,6 +197,9 @@
core
+
+ core
+
diff --git a/project/vs2015/Easy2D.vcxproj b/project/vs2015/Easy2D.vcxproj
index ab2c482c..3e35a3fd 100644
--- a/project/vs2015/Easy2D.vcxproj
+++ b/project/vs2015/Easy2D.vcxproj
@@ -26,6 +26,7 @@
+
diff --git a/project/vs2015/Easy2D.vcxproj.filters b/project/vs2015/Easy2D.vcxproj.filters
index 2111540b..39446fc8 100644
--- a/project/vs2015/Easy2D.vcxproj.filters
+++ b/project/vs2015/Easy2D.vcxproj.filters
@@ -197,6 +197,9 @@
core
+
+ core
+
diff --git a/project/vs2017/Easy2D.vcxproj b/project/vs2017/Easy2D.vcxproj
index fd983f57..ae5126c0 100644
--- a/project/vs2017/Easy2D.vcxproj
+++ b/project/vs2017/Easy2D.vcxproj
@@ -26,6 +26,7 @@
+
diff --git a/project/vs2017/Easy2D.vcxproj.filters b/project/vs2017/Easy2D.vcxproj.filters
index 2111540b..39446fc8 100644
--- a/project/vs2017/Easy2D.vcxproj.filters
+++ b/project/vs2017/Easy2D.vcxproj.filters
@@ -197,6 +197,9 @@
core
+
+ core
+
diff --git a/src/core/Scene.cpp b/src/core/Scene.cpp
index 81c7153f..2b415262 100644
--- a/src/core/Scene.cpp
+++ b/src/core/Scene.cpp
@@ -26,8 +26,8 @@ namespace easy2d
{
Scene::Scene()
{
- AddListener(WindowEvent::Activate, std::bind(&Scene::OnActivate, this));
- AddListener(WindowEvent::Deavtivate, std::bind(&Scene::OnDeactivate, this));
+ AddListener(WindowEvent::Activate, Closure(this, &Scene::OnActivate));
+ AddListener(WindowEvent::Deavtivate, Closure(this, &Scene::OnDeactivate));
scene_ = this;
}
@@ -46,11 +46,11 @@ namespace easy2d
E2D_LOG(L"Scene exited");
}
- void Scene::OnDeactivate()
+ void Scene::OnDeactivate(Event const&)
{
}
- void Scene::OnActivate()
+ void Scene::OnActivate(Event const&)
{
}
diff --git a/src/core/Scene.h b/src/core/Scene.h
index 5172bbfc..7b0aa784 100644
--- a/src/core/Scene.h
+++ b/src/core/Scene.h
@@ -39,9 +39,9 @@ namespace easy2d
virtual void OnExit();
// 窗口获得焦点
- virtual void OnActivate();
+ virtual void OnActivate(Event const&);
// 窗口失去焦点
- virtual void OnDeactivate();
+ virtual void OnDeactivate(Event const&);
};
}
diff --git a/src/core/closure.hpp b/src/core/closure.hpp
new file mode 100644
index 00000000..629e04e2
--- /dev/null
+++ b/src/core/closure.hpp
@@ -0,0 +1,87 @@
+// Copyright (c) 2016-2018 Easy2D - Nomango
+//
+// Permission is hereby granted, free of charge, to any person obtaining a copy
+// of this software and associated documentation files (the "Software"), to deal
+// in the Software without restriction, including without limitation the rights
+// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+// copies of the Software, and to permit persons to whom the Software is
+// furnished to do so, subject to the following conditions:
+//
+// The above copyright notice and this permission notice shall be included in
+// all copies or substantial portions of the Software.
+//
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+// THE SOFTWARE.
+
+#pragma once
+#include
+
+#define CLOSURE_PARAMS_TYPE_1 typename Param1
+#define CLOSURE_PARAMS_1 Param1
+#define CLOSURE_PLACEHOLDERS_1 std::placeholders::_1
+#define CLOSURE_PARAMS_TYPE_2 typename Param1, typename Param2
+#define CLOSURE_PARAMS_2 Param1, Param2
+#define CLOSURE_PLACEHOLDERS_2 std::placeholders::_1, std::placeholders::_2
+#define CLOSURE_PARAMS_TYPE_3 typename Param1, typename Param2, typename Param3
+#define CLOSURE_PARAMS_3 Param1, Param2, Param3
+#define CLOSURE_PLACEHOLDERS_3 std::placeholders::_1, std::placeholders::_2, std::placeholders::_3
+#define CLOSURE_PARAMS_TYPE_4 typename Param1, typename Param2, typename Param3, typename Param4
+#define CLOSURE_PARAMS_4 Param1, Param2, Param3, Param4
+#define CLOSURE_PLACEHOLDERS_4 std::placeholders::_1, std::placeholders::_2, std::placeholders::_3, std::placeholders::_4
+#define CLOSURE_PARAMS_TYPE_5 typename Param1, typename Param2, typename Param3, typename Param4, typename Param5
+#define CLOSURE_PARAMS_5 Param1, Param2, Param3, Param4, Param5
+#define CLOSURE_PLACEHOLDERS_5 std::placeholders::_1, std::placeholders::_2, std::placeholders::_3, std::placeholders::_4, std::placeholders::_5
+#define CLOSURE_PARAMS_TYPE_6 typename Param1, typename Param2, typename Param3, typename Param4, typename Param5, typename Param6
+#define CLOSURE_PARAMS_6 Param1, Param2, Param3, Param4, Param5, Param6
+#define CLOSURE_PLACEHOLDERS_6 std::placeholders::_1, std::placeholders::_2, std::placeholders::_3, std::placeholders::_4, std::placeholders::_5, std::placeholders::_6
+
+#define CLOSURE_EXPAND(x) x
+
+#define CLOSURE_DECLARE(TYPE, PARAMS, PHS)\
+ template\
+ inline std::function Closure(T* ptr, R(T::*func)(CLOSURE_EXPAND(PARAMS)))\
+ {\
+ return std::bind(func, ptr, CLOSURE_EXPAND(PHS));\
+ }
+
+namespace easy2d
+{
+ template
+ inline std::function Closure(T* ptr, R(T::*func)(void))
+ {
+ return std::bind(func, ptr);
+ }
+
+ CLOSURE_DECLARE(CLOSURE_PARAMS_TYPE_1, CLOSURE_PARAMS_1, CLOSURE_PLACEHOLDERS_1);
+ CLOSURE_DECLARE(CLOSURE_PARAMS_TYPE_2, CLOSURE_PARAMS_2, CLOSURE_PLACEHOLDERS_2);
+ CLOSURE_DECLARE(CLOSURE_PARAMS_TYPE_3, CLOSURE_PARAMS_3, CLOSURE_PLACEHOLDERS_3);
+ CLOSURE_DECLARE(CLOSURE_PARAMS_TYPE_4, CLOSURE_PARAMS_4, CLOSURE_PLACEHOLDERS_4);
+ CLOSURE_DECLARE(CLOSURE_PARAMS_TYPE_5, CLOSURE_PARAMS_5, CLOSURE_PLACEHOLDERS_5);
+ CLOSURE_DECLARE(CLOSURE_PARAMS_TYPE_6, CLOSURE_PARAMS_6, CLOSURE_PLACEHOLDERS_6);
+}
+
+#undef CLOSURE_PARAMS_TYPE_1
+#undef CLOSURE_PARAMS_1
+#undef CLOSURE_PLACEHOLDERS_1
+#undef CLOSURE_PARAMS_TYPE_2
+#undef CLOSURE_PARAMS_2
+#undef CLOSURE_PLACEHOLDERS_2
+#undef CLOSURE_PARAMS_TYPE_3
+#undef CLOSURE_PARAMS_3
+#undef CLOSURE_PLACEHOLDERS_3
+#undef CLOSURE_PARAMS_TYPE_4
+#undef CLOSURE_PARAMS_4
+#undef CLOSURE_PLACEHOLDERS_4
+#undef CLOSURE_PARAMS_TYPE_5
+#undef CLOSURE_PARAMS_5
+#undef CLOSURE_PLACEHOLDERS_5
+#undef CLOSURE_PARAMS_TYPE_6
+#undef CLOSURE_PARAMS_6
+#undef CLOSURE_PLACEHOLDERS_6
+#undef CLOSURE_EXPAND
+#undef CLOSURE_DECLARE
diff --git a/src/core/helper.hpp b/src/core/helper.hpp
index 89496271..b8b25cde 100644
--- a/src/core/helper.hpp
+++ b/src/core/helper.hpp
@@ -21,6 +21,7 @@
#pragma once
#include "RefCounter.hpp"
#include "IntrusivePtr.hpp"
+#include "closure.hpp"
#include "../math/vector.hpp"
#include "../math/Rect.hpp"
#include "../math/Matrix.hpp"
diff --git a/src/ui/Button.cpp b/src/ui/Button.cpp
index 12325f4a..ed8ebbaf 100644
--- a/src/ui/Button.cpp
+++ b/src/ui/Button.cpp
@@ -30,10 +30,10 @@ namespace easy2d
, click_callback_(nullptr)
, status_(Status::Normal)
{
- AddListener(MouseEvent::Hover, std::bind(&Button::UpdateStatus, this, std::placeholders::_1));
- AddListener(MouseEvent::Out, std::bind(&Button::UpdateStatus, this, std::placeholders::_1));
- AddListener(MouseEvent::Down, std::bind(&Button::UpdateStatus, this, std::placeholders::_1));
- AddListener(MouseEvent::Up, std::bind(&Button::UpdateStatus, this, std::placeholders::_1));
+ AddListener(MouseEvent::Hover, Closure(this, &Button::UpdateStatus));
+ AddListener(MouseEvent::Out, Closure(this, &Button::UpdateStatus));
+ AddListener(MouseEvent::Down, Closure(this, &Button::UpdateStatus));
+ AddListener(MouseEvent::Up, Closure(this, &Button::UpdateStatus));
}
Button::Button(const Callback& click)