From 18990c3ee6024537026bf680c84959cf9b5aa097 Mon Sep 17 00:00:00 2001 From: Haibo Date: Sat, 17 Nov 2018 23:24:16 +0800 Subject: [PATCH] add: noncopyable.hpp --- core/base/Action.hpp | 3 +-- core/base/ActionCombined.h | 6 ----- core/base/ActionFiniteTime.h | 28 -------------------- core/base/Animation.h | 4 --- core/base/CallFunc.h | 2 -- core/base/Canvas.h | 4 --- core/base/Game.h | 3 +-- core/base/Image.cpp | 1 + core/base/Image.h | 2 -- core/base/Input.h | 3 +-- core/base/Music.h | 2 -- core/base/Node.h | 2 -- core/base/RefCounter.hpp | 15 ++++++----- core/base/Scene.h | 2 -- core/base/Singleton.hpp | 25 ++++++++---------- core/base/Sprite.cpp | 4 +-- core/base/Sprite.h | 2 -- core/base/Text.h | 2 -- core/base/audio.h | 7 +++-- core/base/macros.h | 6 ----- core/base/noncopyable.hpp | 37 +++++++++++++++++++++++++++ core/base/render.h | 3 +-- core/base/window.h | 3 +-- core/easy2d.h | 1 + core/ui/Button.h | 2 -- core/ui/Menu.h | 2 -- core/utils/Player.h | 3 +-- 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 +++ 33 files changed, 80 insertions(+), 106 deletions(-) create mode 100644 core/base/noncopyable.hpp diff --git a/core/base/Action.hpp b/core/base/Action.hpp index 20edd883..576c60bd 100644 --- a/core/base/Action.hpp +++ b/core/base/Action.hpp @@ -21,6 +21,7 @@ #pragma once #include "base.hpp" #include "time.h" +#include "noncopyable.hpp" #include "intrusive/List.hpp" namespace easy2d @@ -31,8 +32,6 @@ namespace easy2d : public RefCounter , protected intrusive::ListItem { - E2D_DISABLE_COPY(Action); - friend class ActionManager; friend class Loop; friend class Sequence; diff --git a/core/base/ActionCombined.h b/core/base/ActionCombined.h index ca98c3d6..cc2661ba 100644 --- a/core/base/ActionCombined.h +++ b/core/base/ActionCombined.h @@ -27,8 +27,6 @@ namespace easy2d class Loop : public Action { - E2D_DISABLE_COPY(Loop); - public: explicit Loop( spAction const& action, /* 执行循环的动作 */ @@ -66,8 +64,6 @@ namespace easy2d class Sequence : public Action { - E2D_DISABLE_COPY(Sequence); - using Actions = std::vector; public: @@ -115,8 +111,6 @@ namespace easy2d class Spawn : public Action { - E2D_DISABLE_COPY(Spawn); - using Actions = std::vector; public: diff --git a/core/base/ActionFiniteTime.h b/core/base/ActionFiniteTime.h index c051f7fb..3678b2d2 100644 --- a/core/base/ActionFiniteTime.h +++ b/core/base/ActionFiniteTime.h @@ -28,8 +28,6 @@ namespace easy2d class FiniteTimeAction : public Action { - E2D_DISABLE_COPY(FiniteTimeAction); - public: // 创建特定时长的持续动作 explicit FiniteTimeAction( @@ -56,8 +54,6 @@ namespace easy2d class MoveBy : public FiniteTimeAction { - E2D_DISABLE_COPY(MoveBy); - public: explicit MoveBy( Duration const& duration, /* 持续时长 */ @@ -88,8 +84,6 @@ namespace easy2d class MoveTo : public MoveBy { - E2D_DISABLE_COPY(MoveTo); - public: explicit MoveTo( Duration const& duration, /* 持续时长 */ @@ -119,8 +113,6 @@ namespace easy2d class JumpBy : public FiniteTimeAction { - E2D_DISABLE_COPY(JumpBy); - public: explicit JumpBy( Duration const& duration, /* 持续时长 */ @@ -155,8 +147,6 @@ namespace easy2d class JumpTo : public JumpBy { - E2D_DISABLE_COPY(JumpTo); - public: explicit JumpTo( Duration const& duration, /* 持续时长 */ @@ -188,8 +178,6 @@ namespace easy2d class ScaleBy : public FiniteTimeAction { - E2D_DISABLE_COPY(ScaleBy); - public: explicit ScaleBy( Duration const& duration, /* 持续时长 */ @@ -227,8 +215,6 @@ namespace easy2d class ScaleTo : public ScaleBy { - E2D_DISABLE_COPY(ScaleTo); - public: explicit ScaleTo( Duration const& duration, /* 持续时长 */ @@ -265,8 +251,6 @@ namespace easy2d class OpacityBy : public FiniteTimeAction { - E2D_DISABLE_COPY(OpacityBy); - public: explicit OpacityBy( Duration const& duration, /* 持续时长 */ @@ -296,8 +280,6 @@ namespace easy2d class OpacityTo : public OpacityBy { - E2D_DISABLE_COPY(OpacityTo); - public: explicit OpacityTo( Duration const& duration, /* 持续时长 */ @@ -327,8 +309,6 @@ namespace easy2d class FadeIn : public OpacityTo { - E2D_DISABLE_COPY(FadeIn); - public: // 创建淡入动作 explicit FadeIn( @@ -341,8 +321,6 @@ namespace easy2d class FadeOut : public OpacityTo { - E2D_DISABLE_COPY(FadeOut); - public: // 创建淡出动作 explicit FadeOut( @@ -355,8 +333,6 @@ namespace easy2d class RotateBy : public FiniteTimeAction { - E2D_DISABLE_COPY(RotateBy); - public: explicit RotateBy( Duration const& duration, /* 持续时长 */ @@ -386,8 +362,6 @@ namespace easy2d class RotateTo : public RotateBy { - E2D_DISABLE_COPY(RotateTo); - public: explicit RotateTo( Duration const& duration, /* 持续时长 */ @@ -417,8 +391,6 @@ namespace easy2d class Delay : public Action { - E2D_DISABLE_COPY(Delay); - public: explicit Delay( Duration const& duration /* 延迟时长(秒) */ diff --git a/core/base/Animation.h b/core/base/Animation.h index 96d8c207..80ddfbfa 100644 --- a/core/base/Animation.h +++ b/core/base/Animation.h @@ -27,8 +27,6 @@ namespace easy2d class Animation : public RefCounter { - E2D_DISABLE_COPY(Animation); - using Images = std::vector< spImage >; public: @@ -86,8 +84,6 @@ namespace easy2d class Animate : public Action { - E2D_DISABLE_COPY(Animate); - public: Animate(); diff --git a/core/base/CallFunc.h b/core/base/CallFunc.h index 856b67c3..d2c4752c 100644 --- a/core/base/CallFunc.h +++ b/core/base/CallFunc.h @@ -28,8 +28,6 @@ namespace easy2d class CallFunc : public Action { - E2D_DISABLE_COPY(CallFunc); - typedef std::function Callback; public: diff --git a/core/base/Canvas.h b/core/base/Canvas.h index 7943a6bf..38217ee1 100644 --- a/core/base/Canvas.h +++ b/core/base/Canvas.h @@ -29,8 +29,6 @@ namespace easy2d class CanvasBrush : public RefCounter { - E2D_DISABLE_COPY(CanvasBrush); - public: CanvasBrush(); @@ -138,8 +136,6 @@ namespace easy2d class Canvas : public Node { - E2D_DISABLE_COPY(Canvas); - public: Canvas(); diff --git a/core/base/Game.h b/core/base/Game.h index 23ab7a21..4bd5b16e 100644 --- a/core/base/Game.h +++ b/core/base/Game.h @@ -46,9 +46,8 @@ namespace easy2d class Game + : protected Noncopyable { - E2D_DISABLE_COPY(Game); - public: Game(); diff --git a/core/base/Image.cpp b/core/base/Image.cpp index 26c77d74..28525c83 100644 --- a/core/base/Image.cpp +++ b/core/base/Image.cpp @@ -191,4 +191,5 @@ namespace easy2d { return bitmap_; } + } \ No newline at end of file diff --git a/core/base/Image.h b/core/base/Image.h index 5a0384d3..d7d40881 100644 --- a/core/base/Image.h +++ b/core/base/Image.h @@ -28,8 +28,6 @@ namespace easy2d class Image : public RefCounter { - E2D_DISABLE_COPY(Image); - public: Image(); diff --git a/core/base/Input.h b/core/base/Input.h index e7996944..5908da10 100644 --- a/core/base/Input.h +++ b/core/base/Input.h @@ -27,11 +27,10 @@ namespace easy2d namespace devices { class InputDevice + : protected Noncopyable { E2D_DECLARE_SINGLETON(InputDevice); - E2D_DISABLE_COPY(InputDevice); - public: void Init(bool debug); diff --git a/core/base/Music.h b/core/base/Music.h index 19c5caaf..225e8d5b 100644 --- a/core/base/Music.h +++ b/core/base/Music.h @@ -29,8 +29,6 @@ namespace easy2d class Music : public RefCounter { - E2D_DISABLE_COPY(Music); - public: Music(); diff --git a/core/base/Node.h b/core/base/Node.h index 0f203703..e1cee6fd 100644 --- a/core/base/Node.h +++ b/core/base/Node.h @@ -44,8 +44,6 @@ namespace easy2d friend class Transition; friend class intrusive::List; - E2D_DISABLE_COPY(Node); - using Nodes = std::vector; using Children = intrusive::List; diff --git a/core/base/RefCounter.hpp b/core/base/RefCounter.hpp index 3e23915f..6c3681e2 100644 --- a/core/base/RefCounter.hpp +++ b/core/base/RefCounter.hpp @@ -20,18 +20,14 @@ #pragma once #include "macros.h" +#include "noncopyable.hpp" namespace easy2d { class RefCounter + : protected Noncopyable { - E2D_DISABLE_COPY(RefCounter); - public: - RefCounter() : ref_count_(0) {} - - virtual ~RefCounter() {} - // 增加引用计数 inline void Retain() { ++ref_count_; } @@ -45,7 +41,12 @@ namespace easy2d // 获取引用计数 inline long GetRefCount() const { return ref_count_; } - private: + protected: + RefCounter() : ref_count_(0) {} + + ~RefCounter() {} + + protected: long ref_count_; }; diff --git a/core/base/Scene.h b/core/base/Scene.h index cbad061a..e7ebefbe 100644 --- a/core/base/Scene.h +++ b/core/base/Scene.h @@ -27,8 +27,6 @@ namespace easy2d class Scene : public Node { - E2D_DISABLE_COPY(Scene); - public: Scene(); diff --git a/core/base/Singleton.hpp b/core/base/Singleton.hpp index 866b162e..18d03bcb 100644 --- a/core/base/Singleton.hpp +++ b/core/base/Singleton.hpp @@ -19,34 +19,29 @@ // THE SOFTWARE. #pragma once +#include "noncopyable.hpp" #include namespace easy2d { template class ISingleton + : protected Noncopyable { public: - static inline T* Instance(); + static inline T* Instance() + { + static std::unique_ptr instance_; + if (!instance_) + instance_.reset(new (std::nothrow) T); + return instance_.get(); + } private: - ISingleton() {} + ISingleton() = default; ~ISingleton() {} - - ISingleton(const ISingleton&) = delete; - - ISingleton & operator= (const ISingleton &) = delete; }; - - template - inline T* easy2d::ISingleton::Instance() - { - static std::unique_ptr instance_; - if (!instance_) - instance_.reset(new (std::nothrow) T); - return instance_.get(); - } } // Class that will implement the singleton mode, diff --git a/core/base/Sprite.cpp b/core/base/Sprite.cpp index 736f5b42..ce3abb28 100644 --- a/core/base/Sprite.cpp +++ b/core/base/Sprite.cpp @@ -81,7 +81,7 @@ namespace easy2d { if (!image_) { - image_ = new (std::nothrow) Image(); + image_ = new (std::nothrow) Image; } if (image_) @@ -99,7 +99,7 @@ namespace easy2d { if (!image_) { - image_ = new (std::nothrow) Image(); + image_ = new (std::nothrow) Image; } if (image_) diff --git a/core/base/Sprite.h b/core/base/Sprite.h index 545ff33c..a50e19cd 100644 --- a/core/base/Sprite.h +++ b/core/base/Sprite.h @@ -28,8 +28,6 @@ namespace easy2d class Sprite : public Node { - E2D_DISABLE_COPY(Sprite); - public: Sprite(); diff --git a/core/base/Text.h b/core/base/Text.h index a544c5a9..1aa2769f 100644 --- a/core/base/Text.h +++ b/core/base/Text.h @@ -29,8 +29,6 @@ namespace easy2d class Text : public Node { - E2D_DISABLE_COPY(Text); - public: Text(); diff --git a/core/base/audio.h b/core/base/audio.h index 63841a1f..c5b12246 100644 --- a/core/base/audio.h +++ b/core/base/audio.h @@ -21,14 +21,14 @@ #pragma once #include "macros.h" #include "Singleton.hpp" +#include "noncopyable.hpp" #include namespace easy2d { class Voice + : protected Noncopyable { - E2D_DISABLE_COPY(Voice); - public: Voice(); @@ -75,11 +75,10 @@ namespace easy2d namespace devices { class AudioDevice + : protected Noncopyable { E2D_DECLARE_SINGLETON(AudioDevice); - E2D_DISABLE_COPY(AudioDevice); - public: void Init(bool debug); diff --git a/core/base/macros.h b/core/base/macros.h index 0057a3c3..0f45d509 100644 --- a/core/base/macros.h +++ b/core/base/macros.h @@ -97,9 +97,3 @@ # define E2D_NOEXCEPT throw() # define E2D_CONSTEXPR const #endif - - -#define E2D_DISABLE_COPY(Class) \ - private: \ - Class(const Class &) = delete; \ - Class & operator= (const Class &) = delete diff --git a/core/base/noncopyable.hpp b/core/base/noncopyable.hpp new file mode 100644 index 00000000..17496ea7 --- /dev/null +++ b/core/base/noncopyable.hpp @@ -0,0 +1,37 @@ +// 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 + +namespace easy2d +{ + class Noncopyable + { + protected: + Noncopyable() = default; + + ~Noncopyable() {}; + + private: + Noncopyable(const Noncopyable&) = delete; + + Noncopyable& operator=(const Noncopyable&) = delete; + }; +} diff --git a/core/base/render.h b/core/base/render.h index e9d6f13a..ab61b75c 100644 --- a/core/base/render.h +++ b/core/base/render.h @@ -47,11 +47,10 @@ namespace easy2d class GraphicsDevice + : protected Noncopyable { E2D_DECLARE_SINGLETON(GraphicsDevice); - E2D_DISABLE_COPY(GraphicsDevice); - public: void Init(HWND hwnd, bool debug); diff --git a/core/base/window.h b/core/base/window.h index a8b53de0..2ee6ac30 100644 --- a/core/base/window.h +++ b/core/base/window.h @@ -27,11 +27,10 @@ namespace easy2d class WindowImpl + : protected Noncopyable { E2D_DECLARE_SINGLETON(WindowImpl); - E2D_DISABLE_COPY(WindowImpl); - public: void Init( String title, diff --git a/core/easy2d.h b/core/easy2d.h index 7bf24867..6417ddd7 100644 --- a/core/easy2d.h +++ b/core/easy2d.h @@ -49,6 +49,7 @@ #include "base/Transform.hpp" #include "base/TextStyle.hpp" +#include "base/noncopyable.hpp" #include "base/intrusive/SmartPointer.hpp" #include "base/intrusive/List.hpp" diff --git a/core/ui/Button.h b/core/ui/Button.h index f6c6af72..bef26ae1 100644 --- a/core/ui/Button.h +++ b/core/ui/Button.h @@ -29,8 +29,6 @@ namespace easy2d class Button : public Node { - E2D_DISABLE_COPY(Button); - using Callback = std::function; public: diff --git a/core/ui/Menu.h b/core/ui/Menu.h index b3923404..edc097cf 100644 --- a/core/ui/Menu.h +++ b/core/ui/Menu.h @@ -29,8 +29,6 @@ namespace easy2d class Menu : public Node { - E2D_DISABLE_COPY(Menu); - public: Menu(); diff --git a/core/utils/Player.h b/core/utils/Player.h index 5bbb85c6..bcc5050d 100644 --- a/core/utils/Player.h +++ b/core/utils/Player.h @@ -26,9 +26,8 @@ namespace easy2d { // 音乐播放器 class Player + : protected Noncopyable { - E2D_DISABLE_COPY(Player); - public: Player(); diff --git a/project/vs2013/Easy2D.vcxproj b/project/vs2013/Easy2D.vcxproj index 499ec2c7..0efedc1a 100644 --- a/project/vs2013/Easy2D.vcxproj +++ b/project/vs2013/Easy2D.vcxproj @@ -44,6 +44,7 @@ + diff --git a/project/vs2013/Easy2D.vcxproj.filters b/project/vs2013/Easy2D.vcxproj.filters index a67d5849..178bd26f 100644 --- a/project/vs2013/Easy2D.vcxproj.filters +++ b/project/vs2013/Easy2D.vcxproj.filters @@ -164,6 +164,9 @@ base + + base + diff --git a/project/vs2015/Easy2D.vcxproj b/project/vs2015/Easy2D.vcxproj index 30953b10..5b781318 100644 --- a/project/vs2015/Easy2D.vcxproj +++ b/project/vs2015/Easy2D.vcxproj @@ -44,6 +44,7 @@ + diff --git a/project/vs2015/Easy2D.vcxproj.filters b/project/vs2015/Easy2D.vcxproj.filters index a67d5849..178bd26f 100644 --- a/project/vs2015/Easy2D.vcxproj.filters +++ b/project/vs2015/Easy2D.vcxproj.filters @@ -164,6 +164,9 @@ base + + base + diff --git a/project/vs2017/Easy2D.vcxproj b/project/vs2017/Easy2D.vcxproj index 1ea1824c..8ccca401 100644 --- a/project/vs2017/Easy2D.vcxproj +++ b/project/vs2017/Easy2D.vcxproj @@ -44,6 +44,7 @@ + diff --git a/project/vs2017/Easy2D.vcxproj.filters b/project/vs2017/Easy2D.vcxproj.filters index a67d5849..178bd26f 100644 --- a/project/vs2017/Easy2D.vcxproj.filters +++ b/project/vs2017/Easy2D.vcxproj.filters @@ -164,6 +164,9 @@ base + + base +