Utility/Icon: Make Icon a RefCounted object

This commit is contained in:
Lynix 2017-01-19 21:59:34 +01:00
parent 439f8d0033
commit e4b6f8e126
5 changed files with 67 additions and 42 deletions

View File

@ -8,6 +8,7 @@
#define NAZARA_ICON_HPP #define NAZARA_ICON_HPP
#include <Nazara/Prerequesites.hpp> #include <Nazara/Prerequesites.hpp>
#include <Nazara/Core/ObjectRef.hpp>
#include <Nazara/Math/Vector2.hpp> #include <Nazara/Math/Vector2.hpp>
#include <Nazara/Utility/Config.hpp> #include <Nazara/Utility/Config.hpp>
@ -16,22 +17,29 @@ namespace Nz
class Image; class Image;
class IconImpl; class IconImpl;
class NAZARA_UTILITY_API Icon class Icon;
using IconRef = ObjectRef<Icon>;
class NAZARA_UTILITY_API Icon : public RefCounted
{ {
friend class WindowImpl; friend class WindowImpl;
public: public:
Icon(); inline Icon();
~Icon(); inline explicit Icon(const Image& icon);
inline ~Icon();
bool Create(const Image& icon); bool Create(const Image& icon);
void Destroy(); void Destroy();
bool IsValid() const; inline bool IsValid() const;
private: private:
IconImpl* m_impl; IconImpl* m_impl;
}; };
} }
#include <Nazara/Utility/Icon.inl>
#endif // NAZARA_ICON_HPP #endif // NAZARA_ICON_HPP

View File

@ -0,0 +1,42 @@
// Copyright (C) 2015 Jérôme Leclercq
// This file is part of the "Nazara Engine - Utility module"
// For conditions of distribution and use, see copyright notice in Config.hpp
#include <Nazara/Utility/Cursor.hpp>
#include <Nazara/Core/ErrorFlags.hpp>
#include <Nazara/Utility/Debug.hpp>
namespace Nz
{
Icon::Icon() :
m_impl(nullptr)
{
}
inline Icon::Icon(const Image& icon)
{
ErrorFlags flags(ErrorFlag_ThrowException, true);
Create(icon);
}
Icon::~Icon()
{
Destroy();
}
bool Icon::IsValid() const
{
return m_impl != nullptr;
}
template<typename... Args>
IconRef Icon::New(Args&&... args)
{
std::unique_ptr<Icon> object(new Icon(std::forward<Args>(args)...));
object->SetPersistent(false);
return object.release();
}
}
#include <Nazara/Utility/DebugOff.hpp>

View File

@ -15,18 +15,18 @@
#include <Nazara/Core/String.hpp> #include <Nazara/Core/String.hpp>
#include <Nazara/Math/Vector2.hpp> #include <Nazara/Math/Vector2.hpp>
#include <Nazara/Utility/Config.hpp> #include <Nazara/Utility/Config.hpp>
#include <Nazara/Utility/Cursor.hpp>
#include <Nazara/Utility/CursorController.hpp> #include <Nazara/Utility/CursorController.hpp>
#include <Nazara/Utility/Enums.hpp> #include <Nazara/Utility/Enums.hpp>
#include <Nazara/Utility/EventHandler.hpp> #include <Nazara/Utility/EventHandler.hpp>
#include <Nazara/Utility/Icon.hpp>
#include <Nazara/Utility/VideoMode.hpp> #include <Nazara/Utility/VideoMode.hpp>
#include <Nazara/Utility/WindowHandle.hpp> #include <Nazara/Utility/WindowHandle.hpp>
#include <queue> #include <queue>
namespace Nz namespace Nz
{ {
class Cursor;
class Image; class Image;
class Icon;
class WindowImpl; class WindowImpl;
class NAZARA_UTILITY_API Window class NAZARA_UTILITY_API Window
@ -86,7 +86,7 @@ namespace Nz
inline void SetCursor(SystemCursor systemCursor); inline void SetCursor(SystemCursor systemCursor);
void SetEventListener(bool listener); void SetEventListener(bool listener);
void SetFocus(); void SetFocus();
void SetIcon(const Icon& icon); void SetIcon(IconRef icon);
void SetMaximumSize(const Vector2i& maxSize); void SetMaximumSize(const Vector2i& maxSize);
void SetMaximumSize(int width, int height); void SetMaximumSize(int width, int height);
void SetMinimumSize(const Vector2i& minSize); void SetMinimumSize(const Vector2i& minSize);
@ -126,6 +126,7 @@ namespace Nz
CursorController m_cursorController; CursorController m_cursorController;
CursorRef m_cursor; CursorRef m_cursor;
EventHandler m_eventHandler; EventHandler m_eventHandler;
IconRef m_icon;
Mutex m_eventMutex; Mutex m_eventMutex;
Mutex m_eventConditionMutex; Mutex m_eventConditionMutex;
bool m_asyncWindow; bool m_asyncWindow;

View File

@ -16,30 +16,19 @@
namespace Nz namespace Nz
{ {
Icon::Icon() :
m_impl(nullptr)
{
}
Icon::~Icon()
{
Destroy();
}
bool Icon::Create(const Image& icon) bool Icon::Create(const Image& icon)
{ {
Destroy(); Destroy();
m_impl = new IconImpl; std::unique_ptr<IconImpl> impl(new IconImpl);
if (!m_impl->Create(icon)) if (!impl->Create(icon))
{ {
NazaraError("Failed to create icon implementation"); NazaraError("Failed to create icon implementation");
delete m_impl;
m_impl = nullptr;
return false; return false;
} }
m_impl = impl.release();
return true; return true;
} }
@ -53,9 +42,4 @@ namespace Nz
m_impl = nullptr; m_impl = nullptr;
} }
} }
bool Icon::IsValid() const
{
return m_impl != nullptr;
}
} }

View File

@ -408,23 +408,13 @@ namespace Nz
m_impl->SetFocus(); m_impl->SetFocus();
} }
void Window::SetIcon(const Icon& icon) void Window::SetIcon(IconRef icon)
{ {
#if NAZARA_UTILITY_SAFE NazaraAssert(m_impl, "Window not created");
if (!m_impl) NazaraAssert(icon && icon.IsValid(), "Invalid icon");
{
NazaraError("Window not created");
return;
}
if (!icon.IsValid()) m_icon = std::move(icon);
{ m_impl->SetIcon(*m_icon);
NazaraError("Icon is not valid");
return;
}
#endif
m_impl->SetIcon(icon);
} }
void Window::SetMaximumSize(const Vector2i& maxSize) void Window::SetMaximumSize(const Vector2i& maxSize)