| build/* |
| cmake_minimum_required(VERSION 3.2) | |||||
| project(scoped-ressource VERSION 0.0.1) | |||||
| message(STATUS "~~~ ${PROJECT_NAME} v${PROJECT_VERSION} ~~~") | |||||
| option(UNITTEST "Build Unit Tests" ON) | |||||
| add_compile_options(-Wall | |||||
| -Wextra | |||||
| -pedantic | |||||
| -Werror | |||||
| -Wshadow | |||||
| -Wold-style-cast | |||||
| ) | |||||
| set(CMAKE_CXX_STANDARD 14) | |||||
| set(CMAKE_CXX_STANDARD_REQUIRED ON) | |||||
| set(CMAKE_CXX_EXTENSIONS OFF) | |||||
| include_directories("include") | |||||
| if( UNITTEST ) | |||||
| enable_testing() | |||||
| add_subdirectory("test") | |||||
| endif() | |||||
| # Scope Guard | |||||
| ## License | |||||
| **GNU General Public License (GPL)** | |||||
| This program is free software: you can redistribute it and/or modify | |||||
| it under the terms of the GNU General Public License as published by | |||||
| the Free Software Foundation, either version 3 of the License, or | |||||
| (at your option) any later version. | |||||
| This program is distributed in the hope that it will be useful, | |||||
| but WITHOUT ANY WARRANTY; without even the implied warranty of | |||||
| MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |||||
| GNU General Public License for more details. | |||||
| You should have received a copy of the GNU General Public License | |||||
| along with this program. If not, see <http://www.gnu.org/licenses/>. | |||||
| #pragma once | |||||
| #include <utility> | |||||
| namespace guards | |||||
| { | |||||
| template<class Deleter> | |||||
| class scope_guard_t | |||||
| { | |||||
| public: | |||||
| explicit scope_guard_t(Deleter&& deleter) noexcept : m_deleter(std::move(deleter)) | |||||
| { | |||||
| } | |||||
| scope_guard_t(scope_guard_t&&)= default; | |||||
| scope_guard_t(const scope_guard_t&) = delete; | |||||
| ~scope_guard_t() | |||||
| { | |||||
| m_deleter(); | |||||
| } | |||||
| scope_guard_t& operator=(const scope_guard_t&) = delete; | |||||
| scope_guard_t& operator=(scope_guard_t&&) = delete; | |||||
| private: | |||||
| Deleter m_deleter; | |||||
| }; | |||||
| template<class Deleter> | |||||
| scope_guard_t<Deleter> scope_guard(Deleter&& deleter) noexcept | |||||
| { | |||||
| return scope_guard_t<Deleter>(std::move(deleter)); | |||||
| } | |||||
| } | |||||
| add_library(testmain TestMain.cpp) | |||||
| target_include_directories(testmain SYSTEM PUBLIC "catch/") | |||||
| function(add_test_suite name) | |||||
| add_executable(${name} ${name}.cpp) | |||||
| target_link_libraries(${name} testmain) | |||||
| add_test(NAME ${name} COMMAND ${name}) | |||||
| endfunction() | |||||
| add_test_suite(ScopeGuardTest) | |||||
| add_custom_target(unittest ScopeGuardTest | |||||
| COMMENT "Running unittests\n\n" | |||||
| VERBATIM | |||||
| ) | |||||
| #include "scope_guard.h" | |||||
| #include <catch.hpp> | |||||
| TEST_CASE("deleter called on destruction", "[ScopeGuard]") | |||||
| { | |||||
| bool executed = false; | |||||
| { | |||||
| auto guard = scope_guard([&executed] { executed = true; }); | |||||
| static_cast<void>(guard); | |||||
| } | |||||
| REQUIRE(executed == true); | |||||
| } | |||||
| #define CATCH_CONFIG_MAIN | |||||
| #include <catch.hpp> |