diff --git a/CMakeLists.txt b/CMakeLists.txt index 6bc08b4d..da255288 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -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 diff --git a/job/queue.cpp b/job/queue.cpp index c2844ba1..e0abd8c1 100644 --- a/job/queue.cpp +++ b/job/queue.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 (); diff --git a/memory/buffer/circular.cpp b/memory/buffer/circular.cpp index 3642f619..dea60ca3 100644 --- a/memory/buffer/circular.cpp +++ b/memory/buffer/circular.cpp @@ -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 @@ -81,7 +81,9 @@ circular::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::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::circular (size_t bytes) posix::error::throw_code (); // all went well, disarm the failsafe - unmapper.clear (); + unmapper.disable (); } diff --git a/raii.hpp b/raii.hpp deleted file mode 100644 index 2377eb58..00000000 --- a/raii.hpp +++ /dev/null @@ -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 - */ - -#ifndef __UTIL_RAII_HPP -#define __UTIL_RAII_HPP - -#include "preprocessor.hpp" - -#include - -/// 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 - 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 &&_func): - func (std::move (_func)) - { ; } - - ~scoped_function () - { func (); } - - void clear (void) - { - func = [] (void) { ; }; - } - - std::function func; - }; -} - -#endif diff --git a/scoped.hpp b/scoped.hpp index e77eb584..0fb0b824 100644 --- a/scoped.hpp +++ b/scoped.hpp @@ -11,7 +11,7 @@ * See the License for the specific language governing permissions and * limitations under the License. * - * Copyright 2018, Danny Robson + * Copyright 2012-2018, Danny Robson */ #pragma once @@ -24,7 +24,7 @@ namespace util::scoped { template class restore { public: - restore (ValueT &_target): + explicit restore (ValueT &_target): m_target (_target), m_value (_target) { ; } @@ -43,4 +43,68 @@ namespace util::scoped { //------------------------------------------------------------------------- template restore (ValueT&) -> restore; -} + + + /////////////////////////////////////////////////////////////////////////// + /// increments the referenced variable at construction time, and + /// decrements at destruction time. + template + class increment { + public: + explicit increment (ValueT &_target): + m_target (_target) + { + ++m_target; + } + + + ~increment () + { + --m_target; + } + + private: + ValueT &m_target; + }; + + + //------------------------------------------------------------------------- + template + increment (ValueT&) -> increment; + + + /////////////////////////////////////////////////////////////////////////// + /// calls a function with the supplied arguments at destruction time + template + class function { + public: + explicit function (FuncT &&_function, Args&& ..._args) + noexcept ( + std::is_nothrow_move_constructible_v && + std::is_nothrow_move_constructible_v> + ): + m_function (std::move (_function)), + m_args (std::forward (_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 m_args; + }; + + + //------------------------------------------------------------------------- + template + function (FuncT &&, Args&&...) -> function; +};