Browse Source

Methods implemented.

main
offa 7 years ago
parent
commit
bdb03f8cd7
2 changed files with 58 additions and 2 deletions
  1. +10
    -1
      include/unique_resource.h
  2. +48
    -1
      test/UniqueResourceTest.cpp

+ 10
- 1
include/unique_resource.h View File

&& std::is_copy_assignable<T>::value), && std::is_copy_assignable<T>::value),
T const &, T const &,
T &&>> T &&>>
constexpr U move_assign_if_noexcept(T&& value) noexcept
constexpr U move_assign_if_noexcept(T& value) noexcept
{ {
return std::move(value); return std::move(value);
} }
} }
} }


template<class RR>
void reset(RR&& r)
{
reset();

m_resource = move_assign_if_noexcept(r);
m_execute_on_destruction = true;
}

void release() void release()
{ {
m_execute_on_destruction = false; m_execute_on_destruction = false;

+ 48
- 1
test/UniqueResourceTest.cpp View File



}; };


struct ConditialThrowOnCopyMock
{
explicit ConditialThrowOnCopyMock(Handle h, bool shouldThrow) : m_handle(h), m_shouldThrow(shouldThrow)
{
}

ConditialThrowOnCopyMock(const ConditialThrowOnCopyMock& other) : m_handle(other.m_handle), m_shouldThrow(other.m_shouldThrow)
{
if( m_shouldThrow == true )
{
throw std::exception{};
}
}

ConditialThrowOnCopyMock(ConditialThrowOnCopyMock&&) = default;

ConditialThrowOnCopyMock& operator=(const ConditialThrowOnCopyMock& other)
{
if( &other != this )
{
m_handle = other.m_handle;
m_shouldThrow = other.m_shouldThrow;

if( m_shouldThrow == true )
{
throw std::exception{};
}
}

return *this;
}

ConditialThrowOnCopyMock& operator=(ConditialThrowOnCopyMock&&) = default;

Handle m_handle;
bool m_shouldThrow;
};


CallMock m; CallMock m;


void deleter(Handle h) void deleter(Handle h)
guard.reset(); guard.reset();
} }


// TODO: Reset with new value
TEST_CASE("reset sets new value and calls deleter on previous", "[UniqueResource]")
{
REQUIRE_CALL(m, deleter(3));
REQUIRE_CALL(m, deleter(7));
auto guard = sr::make_unique_resource(Handle{3}, deleter);
guard.reset(Handle{7});
}


TEST_CASE("release disables deleter", "[UniqueResource]") TEST_CASE("release disables deleter", "[UniqueResource]")
{ {
auto guard = sr::make_unique_resource(Handle{3}, deleter); auto guard = sr::make_unique_resource(Handle{3}, deleter);
CHECK(guard.get() == 3); CHECK(guard.get() == 3);
} }



Loading…
Cancel
Save