Skip to content

Commit

Permalink
Add a helper for generic scope guards.
Browse files Browse the repository at this point in the history
  • Loading branch information
dvander committed Aug 2, 2021
1 parent 9472470 commit 636074b
Show file tree
Hide file tree
Showing 3 changed files with 113 additions and 1 deletion.
46 changes: 45 additions & 1 deletion amtl/am-raii.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// vim: set sts=8 ts=2 sw=2 tw=99 et:
// vim: set sts=4 ts=8 sw=4 tw=99 et:
//
// Copyright (C) 2013-2014, David Anderson and AlliedModders LLC
// All rights reserved.
Expand Down Expand Up @@ -30,6 +30,8 @@
#ifndef _include_amtl_am_raii_h_
#define _include_amtl_am_raii_h_

#include <utility>

namespace ke {

template <typename T>
Expand Down Expand Up @@ -71,6 +73,48 @@ class StackLinked
T* prev_;
};

template <typename T>
class ScopeGuard
{
public:
ScopeGuard(T&& callback) noexcept
: callback_(std::forward<T>(callback)),
cancelled_(false)
{}
ScopeGuard(ScopeGuard&& other) noexcept
: callback_(std::move(other.callback_)),
cancelled_(other.cancelled_)
{
other.cancel();
}

~ScopeGuard() {
if (!cancelled_)
callback_();
}

void cancel() {
cancelled_ = true;
}

private:
ScopeGuard() = delete;
ScopeGuard(const ScopeGuard&) = delete;

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

private:
T callback_;
bool cancelled_;
};

template <typename T>
ScopeGuard<T> MakeScopeGuard(T&& callback)
{
return ScopeGuard<T>(std::forward<T>(callback));
}

template <typename T>
T
ReturnAndVoid(T& t)
Expand Down
1 change: 1 addition & 0 deletions tests/AMBuild.tests
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ binary.sources += [
'test-inlinelist.cpp',
'test-priority-queue.cpp',
'test-refcounting.cpp',
'test-raii.cpp',
'test-string.cpp',
'test-system.cpp',
'test-threadlocal-threaded.cpp',
Expand Down
67 changes: 67 additions & 0 deletions tests/test-raii.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
// vim: set sts=4 ts=8 sw=4 tw=99 et:
//
// Copyright (C) 2021 AlliedModders LLC
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
//
// * Redistributions of source code must retain the above copyright notice, this
// list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above copyright notice,
// this list of conditions and the following disclaimer in the documentation
// and/or other materials provided with the distribution.
// * Neither the name of AlliedModders LLC nor the names of its contributors
// may be used to endorse or promote products derived from this software
// without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
// POSSIBILITY OF SUCH DAMAGE.

#include <amtl/am-raii.h>
#include <gtest/gtest.h>
#include "runner.h"

using namespace ke;

TEST(ScopeGuard, Invoke)
{
int invokes = 0;

{
auto guard1 = MakeScopeGuard([&]() -> void {
invokes++;
});

{
auto guard2 = MakeScopeGuard([&]() -> void {
invokes++;
});
}
ASSERT_EQ(invokes, 1);
}
ASSERT_EQ(invokes, 2);
}

TEST(ScopeGuard, Cancel)
{
int invokes = 0;

{
auto guard1 = MakeScopeGuard([&]() -> void {
invokes++;
});

guard1.cancel();
}
ASSERT_EQ(invokes, 0);
}

0 comments on commit 636074b

Please sign in to comment.