mirror of
https://github.com/SFML/SFML.git
synced 2025-01-19 07:45:13 +08:00
Add move semantics to sf::Context
This commit is contained in:
parent
cc5dc0f08c
commit
7f5d87dcd5
@ -84,6 +84,18 @@ public:
|
|||||||
////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////
|
||||||
Context& operator=(const Context&) = delete;
|
Context& operator=(const Context&) = delete;
|
||||||
|
|
||||||
|
////////////////////////////////////////////////////////////
|
||||||
|
/// \brief Move constructor
|
||||||
|
///
|
||||||
|
////////////////////////////////////////////////////////////
|
||||||
|
Context(Context&& context) noexcept;
|
||||||
|
|
||||||
|
////////////////////////////////////////////////////////////
|
||||||
|
/// \brief Move assignment
|
||||||
|
///
|
||||||
|
////////////////////////////////////////////////////////////
|
||||||
|
Context& operator=(Context&& context) noexcept;
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////
|
||||||
/// \brief Activate or deactivate explicitly the context
|
/// \brief Activate or deactivate explicitly the context
|
||||||
///
|
///
|
||||||
|
@ -49,19 +49,37 @@ namespace sf
|
|||||||
Context::Context() : m_context(priv::GlContext::create())
|
Context::Context() : m_context(priv::GlContext::create())
|
||||||
{
|
{
|
||||||
if (!setActive(true))
|
if (!setActive(true))
|
||||||
{
|
|
||||||
err() << "Failed to set context as active during construction" << std::endl;
|
err() << "Failed to set context as active during construction" << std::endl;
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////
|
||||||
Context::~Context()
|
Context::~Context()
|
||||||
{
|
{
|
||||||
if (!setActive(false))
|
if (m_context && !setActive(false))
|
||||||
{
|
|
||||||
err() << "Failed to set context as inactive during destruction" << std::endl;
|
err() << "Failed to set context as inactive during destruction" << std::endl;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
////////////////////////////////////////////////////////////
|
||||||
|
Context::Context(Context&& context) noexcept : m_context(std::move(context.m_context))
|
||||||
|
{
|
||||||
|
if (&context == ContextImpl::currentContext)
|
||||||
|
ContextImpl::currentContext = this;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
////////////////////////////////////////////////////////////
|
||||||
|
Context& Context::operator=(Context&& context) noexcept
|
||||||
|
{
|
||||||
|
if (this == &context)
|
||||||
|
return *this;
|
||||||
|
|
||||||
|
m_context = std::move(context.m_context);
|
||||||
|
if (&context == ContextImpl::currentContext)
|
||||||
|
ContextImpl::currentContext = this;
|
||||||
|
|
||||||
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -125,9 +143,7 @@ Context::Context(const ContextSettings& settings, const Vector2u& size) :
|
|||||||
m_context(priv::GlContext::create(settings, size))
|
m_context(priv::GlContext::create(settings, size))
|
||||||
{
|
{
|
||||||
if (!setActive(true))
|
if (!setActive(true))
|
||||||
{
|
|
||||||
err() << "Failed to set context as active during construction" << std::endl;
|
err() << "Failed to set context as active during construction" << std::endl;
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
} // namespace sf
|
} // namespace sf
|
||||||
|
@ -21,8 +21,8 @@ TEST_CASE("[Window] sf::Context", runDisplayTests())
|
|||||||
{
|
{
|
||||||
STATIC_CHECK(!std::is_copy_constructible_v<sf::Context>);
|
STATIC_CHECK(!std::is_copy_constructible_v<sf::Context>);
|
||||||
STATIC_CHECK(!std::is_copy_assignable_v<sf::Context>);
|
STATIC_CHECK(!std::is_copy_assignable_v<sf::Context>);
|
||||||
STATIC_CHECK(!std::is_nothrow_move_constructible_v<sf::Context>);
|
STATIC_CHECK(std::is_nothrow_move_constructible_v<sf::Context>);
|
||||||
STATIC_CHECK(!std::is_nothrow_move_assignable_v<sf::Context>);
|
STATIC_CHECK(std::is_nothrow_move_assignable_v<sf::Context>);
|
||||||
}
|
}
|
||||||
|
|
||||||
SECTION("Construction")
|
SECTION("Construction")
|
||||||
@ -33,6 +33,65 @@ TEST_CASE("[Window] sf::Context", runDisplayTests())
|
|||||||
CHECK(sf::Context::getActiveContextId() != 0);
|
CHECK(sf::Context::getActiveContextId() != 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
SECTION("Move semantics")
|
||||||
|
{
|
||||||
|
SECTION("Construction")
|
||||||
|
{
|
||||||
|
SECTION("From active context")
|
||||||
|
{
|
||||||
|
sf::Context movedContext;
|
||||||
|
const sf::Context context(std::move(movedContext));
|
||||||
|
CHECK(context.getSettings().majorVersion > 0);
|
||||||
|
CHECK(sf::Context::getActiveContext() == &context);
|
||||||
|
CHECK(sf::Context::getActiveContextId() != 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
SECTION("From inactive context")
|
||||||
|
{
|
||||||
|
sf::Context movedContext;
|
||||||
|
CHECK(movedContext.setActive(false));
|
||||||
|
CHECK(sf::Context::getActiveContext() == nullptr);
|
||||||
|
CHECK(sf::Context::getActiveContextId() == 0);
|
||||||
|
|
||||||
|
const sf::Context context(std::move(movedContext));
|
||||||
|
CHECK(context.getSettings().majorVersion > 0);
|
||||||
|
CHECK(sf::Context::getActiveContext() == nullptr);
|
||||||
|
CHECK(sf::Context::getActiveContextId() == 0);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
SECTION("Assignment")
|
||||||
|
{
|
||||||
|
SECTION("From active context")
|
||||||
|
{
|
||||||
|
sf::Context movedContext;
|
||||||
|
sf::Context context;
|
||||||
|
CHECK(movedContext.setActive(true));
|
||||||
|
CHECK(sf::Context::getActiveContext() == &movedContext);
|
||||||
|
CHECK(sf::Context::getActiveContextId() != 0);
|
||||||
|
|
||||||
|
context = std::move(movedContext);
|
||||||
|
CHECK(context.getSettings().majorVersion > 0);
|
||||||
|
CHECK(sf::Context::getActiveContext() == &context);
|
||||||
|
CHECK(sf::Context::getActiveContextId() != 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
SECTION("From inactive context")
|
||||||
|
{
|
||||||
|
sf::Context movedContext;
|
||||||
|
CHECK(movedContext.setActive(false));
|
||||||
|
CHECK(sf::Context::getActiveContext() == nullptr);
|
||||||
|
CHECK(sf::Context::getActiveContextId() == 0);
|
||||||
|
|
||||||
|
sf::Context context;
|
||||||
|
context = std::move(movedContext);
|
||||||
|
CHECK(context.getSettings().majorVersion > 0);
|
||||||
|
CHECK(sf::Context::getActiveContext() == nullptr);
|
||||||
|
CHECK(sf::Context::getActiveContextId() == 0);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
SECTION("setActive()")
|
SECTION("setActive()")
|
||||||
{
|
{
|
||||||
sf::Context context;
|
sf::Context context;
|
||||||
|
Loading…
Reference in New Issue
Block a user