add: noncopyable.hpp

This commit is contained in:
Haibo 2018-11-17 23:24:16 +08:00 committed by Nomango
parent 9bd216dc92
commit 18990c3ee6
33 changed files with 80 additions and 106 deletions

View File

@ -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<spAction>
{
E2D_DISABLE_COPY(Action);
friend class ActionManager;
friend class Loop;
friend class Sequence;

View File

@ -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<spAction>;
public:
@ -115,8 +111,6 @@ namespace easy2d
class Spawn
: public Action
{
E2D_DISABLE_COPY(Spawn);
using Actions = std::vector<spAction>;
public:

View File

@ -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 /* 延迟时长(秒) */

View File

@ -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();

View File

@ -28,8 +28,6 @@ namespace easy2d
class CallFunc
: public Action
{
E2D_DISABLE_COPY(CallFunc);
typedef std::function<void()> Callback;
public:

View File

@ -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();

View File

@ -46,9 +46,8 @@ namespace easy2d
class Game
: protected Noncopyable
{
E2D_DISABLE_COPY(Game);
public:
Game();

View File

@ -191,4 +191,5 @@ namespace easy2d
{
return bitmap_;
}
}

View File

@ -28,8 +28,6 @@ namespace easy2d
class Image
: public RefCounter
{
E2D_DISABLE_COPY(Image);
public:
Image();

View File

@ -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);

View File

@ -29,8 +29,6 @@ namespace easy2d
class Music
: public RefCounter
{
E2D_DISABLE_COPY(Music);
public:
Music();

View File

@ -44,8 +44,6 @@ namespace easy2d
friend class Transition;
friend class intrusive::List<spNode>;
E2D_DISABLE_COPY(Node);
using Nodes = std::vector<spNode>;
using Children = intrusive::List<spNode>;

View File

@ -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_;
};

View File

@ -27,8 +27,6 @@ namespace easy2d
class Scene
: public Node
{
E2D_DISABLE_COPY(Scene);
public:
Scene();

View File

@ -19,34 +19,29 @@
// THE SOFTWARE.
#pragma once
#include "noncopyable.hpp"
#include <memory>
namespace easy2d
{
template <typename T>
class ISingleton
: protected Noncopyable
{
public:
static inline T* Instance();
static inline T* Instance()
{
static std::unique_ptr<T> 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<typename T>
inline T* easy2d::ISingleton<T>::Instance()
{
static std::unique_ptr<T> instance_;
if (!instance_)
instance_.reset(new (std::nothrow) T);
return instance_.get();
}
}
// Class that will implement the singleton mode,

View File

@ -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_)

View File

@ -28,8 +28,6 @@ namespace easy2d
class Sprite
: public Node
{
E2D_DISABLE_COPY(Sprite);
public:
Sprite();

View File

@ -29,8 +29,6 @@ namespace easy2d
class Text
: public Node
{
E2D_DISABLE_COPY(Text);
public:
Text();

View File

@ -21,14 +21,14 @@
#pragma once
#include "macros.h"
#include "Singleton.hpp"
#include "noncopyable.hpp"
#include <xaudio2.h>
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);

View File

@ -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

37
core/base/noncopyable.hpp Normal file
View File

@ -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;
};
}

View File

@ -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);

View File

@ -27,11 +27,10 @@ namespace easy2d
class WindowImpl
: protected Noncopyable
{
E2D_DECLARE_SINGLETON(WindowImpl);
E2D_DISABLE_COPY(WindowImpl);
public:
void Init(
String title,

View File

@ -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"

View File

@ -29,8 +29,6 @@ namespace easy2d
class Button
: public Node
{
E2D_DISABLE_COPY(Button);
using Callback = std::function<void()>;
public:

View File

@ -29,8 +29,6 @@ namespace easy2d
class Menu
: public Node
{
E2D_DISABLE_COPY(Menu);
public:
Menu();

View File

@ -26,9 +26,8 @@ namespace easy2d
{
// 音乐播放器
class Player
: protected Noncopyable
{
E2D_DISABLE_COPY(Player);
public:
Player();

View File

@ -44,6 +44,7 @@
<ClInclude Include="..\..\core\base\MouseEvent.h" />
<ClInclude Include="..\..\core\base\Music.h" />
<ClInclude Include="..\..\core\base\Node.h" />
<ClInclude Include="..\..\core\base\noncopyable.hpp" />
<ClInclude Include="..\..\core\base\Point.hpp" />
<ClInclude Include="..\..\core\base\Rect.hpp" />
<ClInclude Include="..\..\core\base\RefCounter.hpp" />

View File

@ -164,6 +164,9 @@
<ClInclude Include="..\..\core\base\d2dres.hpp">
<Filter>base</Filter>
</ClInclude>
<ClInclude Include="..\..\core\base\noncopyable.hpp">
<Filter>base</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<Filter Include="base">

View File

@ -44,6 +44,7 @@
<ClInclude Include="..\..\core\base\MouseEvent.h" />
<ClInclude Include="..\..\core\base\Music.h" />
<ClInclude Include="..\..\core\base\Node.h" />
<ClInclude Include="..\..\core\base\noncopyable.hpp" />
<ClInclude Include="..\..\core\base\Point.hpp" />
<ClInclude Include="..\..\core\base\Rect.hpp" />
<ClInclude Include="..\..\core\base\RefCounter.hpp" />

View File

@ -164,6 +164,9 @@
<ClInclude Include="..\..\core\base\d2dres.hpp">
<Filter>base</Filter>
</ClInclude>
<ClInclude Include="..\..\core\base\noncopyable.hpp">
<Filter>base</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<Filter Include="base">

View File

@ -44,6 +44,7 @@
<ClInclude Include="..\..\core\base\MouseEvent.h" />
<ClInclude Include="..\..\core\base\Music.h" />
<ClInclude Include="..\..\core\base\Node.h" />
<ClInclude Include="..\..\core\base\noncopyable.hpp" />
<ClInclude Include="..\..\core\base\Point.hpp" />
<ClInclude Include="..\..\core\base\Rect.hpp" />
<ClInclude Include="..\..\core\base\RefCounter.hpp" />

View File

@ -164,6 +164,9 @@
<ClInclude Include="..\..\core\base\d2dres.hpp">
<Filter>base</Filter>
</ClInclude>
<ClInclude Include="..\..\core\base\noncopyable.hpp">
<Filter>base</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<Filter Include="base">