[ox/std] Add defer

This commit is contained in:
Gary Talent 2021-07-03 18:30:15 -05:00
parent fcdede2064
commit ff2f13c99f
3 changed files with 39 additions and 0 deletions

View File

@ -23,6 +23,7 @@ add_library(
buffer.cpp
buildinfo.cpp
byteswap.cpp
defer.hpp
fmt.cpp
heapmgr.cpp
memops.cpp

37
deps/ox/src/ox/std/defer.hpp vendored Normal file
View File

@ -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<typename T>
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__) = [&]

View File

@ -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"