Skip to content

Commit

Permalink
OrcLib: Utils: Guard: rename ScopeGuard to Scope
Browse files Browse the repository at this point in the history
  • Loading branch information
fabienfl-orc committed Jan 26, 2021
1 parent 22f666a commit 1dbf892
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 15 deletions.
2 changes: 1 addition & 1 deletion src/OrcCommand/GetSectors_Run.cpp
Expand Up @@ -117,7 +117,7 @@ std::wstring Main::getBootDiskName()
return {};
}

Guard::ScopeGuard sg([&hVolume]() {
Guard::Scope sg([&hVolume]() {
if (hVolume != INVALID_HANDLE_VALUE)
{
CloseHandle(hVolume);
Expand Down
4 changes: 2 additions & 2 deletions src/OrcCommand/UtilitiesMain.h
Expand Up @@ -302,11 +302,11 @@ class ORCLIB_API UtilitiesMain

HRESULT ForEachOutput(const OutputSpec& output, std::function<HRESULT(const OutputPair& out)> aCallback)
{
Guard::ScopeGuard sg([&output, this] { CloseAll(output); });
Guard::Scope sg([&output, this] { CloseAll(output); });

for (auto& item : m_outputs)
{
Guard::ScopeGuard forGuard([&output, &item, this]() { CloseOne(output, item); });
Guard::Scope forGuard([&output, &item, this]() { CloseOne(output, item); });

HRESULT hr = E_FAIL;
// Actually enumerate objects here
Expand Down
29 changes: 17 additions & 12 deletions src/OrcLib/Utils/Guard.h
Expand Up @@ -17,15 +17,15 @@ namespace Orc {
namespace Guard {

template <typename T>
class ScopeGuard
class Scope final
{
public:
explicit ScopeGuard(T&& handler)
explicit Scope(T&& handler)
: handler_(std::forward<T>(handler))
, cancelled_(false)
{
}
~ScopeGuard()
~Scope()
{
if (cancelled_)
{
Expand All @@ -35,15 +35,20 @@ class ScopeGuard
{
handler_();
}
catch (const std::system_error& e)
{
Log::Warn("Guard::Scope's handler has thrown an exception: {}", e.code());
}
catch (const std::exception& e)
{
std::cerr << e.what() << std::endl;
std::cerr << "Guard::Scope's handler has thrown exception: " << e.what() << std::endl;
Log::Warn("Guard::Scope's handler has thrown exception: {}", e.what());
}
}

ScopeGuard(ScopeGuard&&) = default;
ScopeGuard(const ScopeGuard&) = delete;
void operator=(const ScopeGuard&) = delete;
Scope(Scope&&) = default;
Scope(const Scope&) = delete;
void operator=(const Scope&) = delete;

void Cancel() { cancelled_ = true; }

Expand All @@ -53,12 +58,12 @@ class ScopeGuard
};

template <typename T>
ScopeGuard<T> CreateScopeGuard(T&& handler)
Scope<T> CreateScopeGuard(T&& handler)
{
return ScopeGuard<T>(std::forward<T>(handler));
return Scope<T>(std::forward<T>(handler));
}

class FileHandle
class FileHandle final
{
public:
FileHandle(HANDLE handle = INVALID_HANDLE_VALUE)
Expand Down Expand Up @@ -86,7 +91,7 @@ class FileHandle
std::shared_ptr<void> m_handle;
};

class Handle
class Handle final
{
public:
Handle(HANDLE handle = nullptr)
Expand All @@ -103,7 +108,7 @@ class Handle

if (CloseHandle(handle) == FALSE)
{
Log::Warn("Failed on CloseHandle: {}", GetLastError());
Log::Warn("Failed on CloseHandle [{}]", LastWin32Error());
}
}

Expand Down

0 comments on commit 1dbf892

Please sign in to comment.