Selaa lähdekoodia

Methods implemented.

main
offa 7 vuotta sitten
vanhempi
commit
bdb03f8cd7
2 muutettua tiedostoa jossa 58 lisäystä ja 2 poistoa
  1. +10
    -1
      include/unique_resource.h
  2. +48
    -1
      test/UniqueResourceTest.cpp

+ 10
- 1
include/unique_resource.h Näytä tiedosto

@@ -40,7 +40,7 @@ namespace sr
&& std::is_copy_assignable<T>::value),
T const &,
T &&>>
constexpr U move_assign_if_noexcept(T&& value) noexcept
constexpr U move_assign_if_noexcept(T& value) noexcept
{
return std::move(value);
}
@@ -121,6 +121,15 @@ namespace sr
}
}

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

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

void release()
{
m_execute_on_destruction = false;

+ 48
- 1
test/UniqueResourceTest.cpp Näytä tiedosto

@@ -78,6 +78,45 @@ namespace

};

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;

void deleter(Handle h)
@@ -161,7 +200,13 @@ TEST_CASE("reset does not call deleter if released", "[UniqueResource]")
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]")
{
@@ -176,3 +221,5 @@ TEST_CASE("get returns resource", "[UniqueResource]")
auto guard = sr::make_unique_resource(Handle{3}, deleter);
CHECK(guard.get() == 3);
}



Loading…
Peruuta
Tallenna