diff --git a/include/Nazara/Utility/Icon.hpp b/include/Nazara/Utility/Icon.hpp index 4f0397faf..61b68042b 100644 --- a/include/Nazara/Utility/Icon.hpp +++ b/include/Nazara/Utility/Icon.hpp @@ -8,6 +8,7 @@ #define NAZARA_ICON_HPP #include +#include #include #include @@ -16,22 +17,29 @@ namespace Nz class Image; class IconImpl; - class NAZARA_UTILITY_API Icon + class Icon; + + using IconRef = ObjectRef; + + class NAZARA_UTILITY_API Icon : public RefCounted { friend class WindowImpl; public: - Icon(); - ~Icon(); + inline Icon(); + inline explicit Icon(const Image& icon); + inline ~Icon(); bool Create(const Image& icon); void Destroy(); - bool IsValid() const; + inline bool IsValid() const; private: IconImpl* m_impl; }; } +#include + #endif // NAZARA_ICON_HPP diff --git a/include/Nazara/Utility/Icon.inl b/include/Nazara/Utility/Icon.inl new file mode 100644 index 000000000..d7f29eb54 --- /dev/null +++ b/include/Nazara/Utility/Icon.inl @@ -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 +#include +#include + +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 + IconRef Icon::New(Args&&... args) + { + std::unique_ptr object(new Icon(std::forward(args)...)); + object->SetPersistent(false); + + return object.release(); + } +} + +#include diff --git a/include/Nazara/Utility/Window.hpp b/include/Nazara/Utility/Window.hpp index 7360dd13e..657541c93 100644 --- a/include/Nazara/Utility/Window.hpp +++ b/include/Nazara/Utility/Window.hpp @@ -15,18 +15,18 @@ #include #include #include +#include #include #include #include +#include #include #include #include namespace Nz { - class Cursor; class Image; - class Icon; class WindowImpl; class NAZARA_UTILITY_API Window @@ -86,7 +86,7 @@ namespace Nz inline void SetCursor(SystemCursor systemCursor); void SetEventListener(bool listener); void SetFocus(); - void SetIcon(const Icon& icon); + void SetIcon(IconRef icon); void SetMaximumSize(const Vector2i& maxSize); void SetMaximumSize(int width, int height); void SetMinimumSize(const Vector2i& minSize); @@ -126,6 +126,7 @@ namespace Nz CursorController m_cursorController; CursorRef m_cursor; EventHandler m_eventHandler; + IconRef m_icon; Mutex m_eventMutex; Mutex m_eventConditionMutex; bool m_asyncWindow; diff --git a/src/Nazara/Utility/Icon.cpp b/src/Nazara/Utility/Icon.cpp index 0fa4fba11..21a39142f 100644 --- a/src/Nazara/Utility/Icon.cpp +++ b/src/Nazara/Utility/Icon.cpp @@ -16,30 +16,19 @@ namespace Nz { - Icon::Icon() : - m_impl(nullptr) - { - } - - Icon::~Icon() - { - Destroy(); - } - bool Icon::Create(const Image& icon) { Destroy(); - m_impl = new IconImpl; - if (!m_impl->Create(icon)) + std::unique_ptr impl(new IconImpl); + if (!impl->Create(icon)) { NazaraError("Failed to create icon implementation"); - delete m_impl; - m_impl = nullptr; - return false; } + m_impl = impl.release(); + return true; } @@ -53,9 +42,4 @@ namespace Nz m_impl = nullptr; } } - - bool Icon::IsValid() const - { - return m_impl != nullptr; - } } diff --git a/src/Nazara/Utility/Window.cpp b/src/Nazara/Utility/Window.cpp index 7746a4236..a6a6d381d 100644 --- a/src/Nazara/Utility/Window.cpp +++ b/src/Nazara/Utility/Window.cpp @@ -408,23 +408,13 @@ namespace Nz m_impl->SetFocus(); } - void Window::SetIcon(const Icon& icon) + void Window::SetIcon(IconRef icon) { - #if NAZARA_UTILITY_SAFE - if (!m_impl) - { - NazaraError("Window not created"); - return; - } + NazaraAssert(m_impl, "Window not created"); + NazaraAssert(icon && icon.IsValid(), "Invalid icon"); - if (!icon.IsValid()) - { - NazaraError("Icon is not valid"); - return; - } - #endif - - m_impl->SetIcon(icon); + m_icon = std::move(icon); + m_impl->SetIcon(*m_icon); } void Window::SetMaximumSize(const Vector2i& maxSize)