From ff2f13c99f24de35d9452e372825b056748d9ca8 Mon Sep 17 00:00:00 2001 From: Gary Talent Date: Sat, 3 Jul 2021 18:30:15 -0500 Subject: [PATCH] [ox/std] Add defer --- deps/ox/src/ox/std/CMakeLists.txt | 1 + deps/ox/src/ox/std/defer.hpp | 37 +++++++++++++++++++++++++++++++ deps/ox/src/ox/std/std.hpp | 1 + 3 files changed, 39 insertions(+) create mode 100644 deps/ox/src/ox/std/defer.hpp diff --git a/deps/ox/src/ox/std/CMakeLists.txt b/deps/ox/src/ox/std/CMakeLists.txt index f6acb3836..aca844e43 100644 --- a/deps/ox/src/ox/std/CMakeLists.txt +++ b/deps/ox/src/ox/std/CMakeLists.txt @@ -23,6 +23,7 @@ add_library( buffer.cpp buildinfo.cpp byteswap.cpp + defer.hpp fmt.cpp heapmgr.cpp memops.cpp diff --git a/deps/ox/src/ox/std/defer.hpp b/deps/ox/src/ox/std/defer.hpp new file mode 100644 index 000000000..d8ac1ce37 --- /dev/null +++ b/deps/ox/src/ox/std/defer.hpp @@ -0,0 +1,37 @@ +/* + * Copyright 2015 - 2021 gary@drinkingtea.net + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + */ + +#pragma once + +#include "error.hpp" + +namespace ox { + +template +class Defer { + private: + T m_deferredFunc; + + public: + Defer(T deferredFunc) { + m_deferredFunc = deferredFunc; + } + + Defer(const Defer&) = delete; + + ~Defer() { + m_deferredFunc(); + } + + Defer &operator=(const Defer&) = delete; + +}; + +} + +#define oxDefer Defer oxConcat(oxDefer_, __LINE__) = [&] \ No newline at end of file diff --git a/deps/ox/src/ox/std/std.hpp b/deps/ox/src/ox/std/std.hpp index 073fffc75..b0e800aa9 100644 --- a/deps/ox/src/ox/std/std.hpp +++ b/deps/ox/src/ox/std/std.hpp @@ -12,6 +12,7 @@ #include "bit.hpp" #include "bstring.hpp" #include "byteswap.hpp" +#include "defer.hpp" #include "defines.hpp" #include "error.hpp" #include "fmt.hpp"