scoped: eliminate the raii header
This commit is contained in:
parent
710a99b865
commit
e9d5909f21
@ -341,7 +341,6 @@ list (
|
||||
"${CMAKE_CURRENT_SOURCE_DIR}/preprocessor.hpp"
|
||||
quaternion.cpp
|
||||
quaternion.hpp
|
||||
raii.hpp
|
||||
rand/lcg.cpp
|
||||
rand/lcg.hpp
|
||||
rand/xorshift.cpp
|
||||
|
@ -17,7 +17,7 @@
|
||||
#include "queue.hpp"
|
||||
|
||||
#include "../parse.hpp"
|
||||
#include "../raii.hpp"
|
||||
#include "../scoped.hpp"
|
||||
|
||||
using util::job::queue;
|
||||
|
||||
@ -100,7 +100,7 @@ queue::loop ()
|
||||
if (m_stopping)
|
||||
return;
|
||||
|
||||
util::scoped_counter running_count (m_running);
|
||||
util::scoped::increment running_count (m_running);
|
||||
|
||||
CHECK (!m_tasks.pending->empty ());
|
||||
|
||||
@ -111,7 +111,7 @@ queue::loop ()
|
||||
return res;
|
||||
} ();
|
||||
|
||||
util::scoped_function cleanup ([&, this] () {
|
||||
util::scoped::function cleanup ([&, this] () {
|
||||
while (!m_tasks.finishing.push (todo))
|
||||
;
|
||||
m_doomed.release ();
|
||||
|
@ -19,8 +19,8 @@
|
||||
#include "../../debug.hpp"
|
||||
#include "../../maths.hpp"
|
||||
#include "../../posix/except.hpp"
|
||||
#include "../../raii.hpp"
|
||||
#include "../../random.hpp"
|
||||
#include "../../scoped.hpp"
|
||||
#include "../system.hpp"
|
||||
|
||||
#include <unistd.h>
|
||||
@ -81,7 +81,9 @@ circular<ValueT>::circular (size_t bytes)
|
||||
|
||||
// setup a desctructor for the shm data. mmap retains a reference, so do
|
||||
// this whether we succeed or fail in the next phase.
|
||||
util::scoped_function raii ([&name] (void) { shm_unlink (name.c_str ()); });
|
||||
util::scoped::function unlink_callback ([&name] (void) {
|
||||
shm_unlink (name.c_str ());
|
||||
});
|
||||
|
||||
// embiggen to the desired size
|
||||
if (ftruncate (fd, bytes))
|
||||
@ -97,7 +99,9 @@ circular<ValueT>::circular (size_t bytes)
|
||||
posix::error::throw_code ();
|
||||
|
||||
// preemptively setup an unmapping object in case the remapping fails
|
||||
util::scoped_function unmapper ([this, bytes] (void) { munmap (m_begin, bytes); });
|
||||
util::scoped::function unmapper (
|
||||
[this, bytes] (void) noexcept { munmap (m_begin, bytes); }
|
||||
);
|
||||
|
||||
// overwrite the map with two adjacent copies of the memory object. this
|
||||
// must be a shared mapping for the values to propogate across segments.
|
||||
@ -111,7 +115,7 @@ circular<ValueT>::circular (size_t bytes)
|
||||
posix::error::throw_code ();
|
||||
|
||||
// all went well, disarm the failsafe
|
||||
unmapper.clear ();
|
||||
unmapper.disable ();
|
||||
}
|
||||
|
||||
|
||||
|
60
raii.hpp
60
raii.hpp
@ -1,60 +0,0 @@
|
||||
/*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*
|
||||
* Copyright 2012 Danny Robson <danny@nerdcruft.net>
|
||||
*/
|
||||
|
||||
#ifndef __UTIL_RAII_HPP
|
||||
#define __UTIL_RAII_HPP
|
||||
|
||||
#include "preprocessor.hpp"
|
||||
|
||||
#include <functional>
|
||||
|
||||
/// Defines a translation-unit-unique variable useful for unnamed scoped variables
|
||||
#define raii PASTE(__unique_, __COUNTER__)
|
||||
|
||||
namespace util {
|
||||
/// Increments a counter for the lifetime of the object
|
||||
template <typename T>
|
||||
struct scoped_counter {
|
||||
explicit scoped_counter (T &_counter):
|
||||
counter (_counter)
|
||||
{ ++counter; }
|
||||
|
||||
~scoped_counter ()
|
||||
{ --counter; }
|
||||
|
||||
T &counter;
|
||||
};
|
||||
|
||||
|
||||
/// Executes a function upon object destruction
|
||||
struct scoped_function {
|
||||
explicit scoped_function (std::function<void(void)> &&_func):
|
||||
func (std::move (_func))
|
||||
{ ; }
|
||||
|
||||
~scoped_function ()
|
||||
{ func (); }
|
||||
|
||||
void clear (void)
|
||||
{
|
||||
func = [] (void) { ; };
|
||||
}
|
||||
|
||||
std::function<void(void)> func;
|
||||
};
|
||||
}
|
||||
|
||||
#endif
|
68
scoped.hpp
68
scoped.hpp
@ -11,7 +11,7 @@
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*
|
||||
* Copyright 2018, Danny Robson <danny@nerdcruft.net>
|
||||
* Copyright 2012-2018, Danny Robson <danny@nerdcruft.net>
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
@ -24,7 +24,7 @@ namespace util::scoped {
|
||||
template <typename ValueT>
|
||||
class restore {
|
||||
public:
|
||||
restore (ValueT &_target):
|
||||
explicit restore (ValueT &_target):
|
||||
m_target (_target),
|
||||
m_value (_target)
|
||||
{ ; }
|
||||
@ -43,4 +43,68 @@ namespace util::scoped {
|
||||
//-------------------------------------------------------------------------
|
||||
template <typename ValueT>
|
||||
restore (ValueT&) -> restore<ValueT>;
|
||||
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////
|
||||
/// increments the referenced variable at construction time, and
|
||||
/// decrements at destruction time.
|
||||
template <typename ValueT>
|
||||
class increment {
|
||||
public:
|
||||
explicit increment (ValueT &_target):
|
||||
m_target (_target)
|
||||
{
|
||||
++m_target;
|
||||
}
|
||||
|
||||
|
||||
~increment ()
|
||||
{
|
||||
--m_target;
|
||||
}
|
||||
|
||||
private:
|
||||
ValueT &m_target;
|
||||
};
|
||||
|
||||
|
||||
//-------------------------------------------------------------------------
|
||||
template <typename ValueT>
|
||||
increment (ValueT&) -> increment<ValueT>;
|
||||
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////
|
||||
/// calls a function with the supplied arguments at destruction time
|
||||
template <typename FuncT, typename ...Args>
|
||||
class function {
|
||||
public:
|
||||
explicit function (FuncT &&_function, Args&& ..._args)
|
||||
noexcept (
|
||||
std::is_nothrow_move_constructible_v<FuncT> &&
|
||||
std::is_nothrow_move_constructible_v<std::tuple<Args...>>
|
||||
):
|
||||
m_function (std::move (_function)),
|
||||
m_args (std::forward<Args> (_args)...)
|
||||
{ ; }
|
||||
|
||||
|
||||
void disable (void) { m_enabled = false; }
|
||||
|
||||
|
||||
~function ()
|
||||
{
|
||||
if (m_enabled)
|
||||
std::apply (std::move (m_function), std::move (m_args));
|
||||
}
|
||||
|
||||
private:
|
||||
bool m_enabled = true;
|
||||
FuncT m_function;
|
||||
std::tuple<Args...> m_args;
|
||||
};
|
||||
|
||||
|
||||
//-------------------------------------------------------------------------
|
||||
template <typename FuncT, typename ...Args>
|
||||
function (FuncT &&, Args&&...) -> function<FuncT&&,Args&&...>;
|
||||
};
|
||||
|
Loading…
Reference in New Issue
Block a user