Преглед на файлове

Implemenation of unique_resource started.

main
offa преди 7 години
родител
ревизия
fa67d80289
променени са 3 файла, в които са добавени 98 реда и са изтрити 1 реда
  1. +60
    -0
      include/unique_resource.h
  2. +2
    -1
      test/CMakeLists.txt
  3. +36
    -0
      test/UniqueResourceTest.cpp

+ 60
- 0
include/unique_resource.h Целия файл

@@ -0,0 +1,60 @@
/*
* Scope Guard
* Copyright (C) 2017 offa
*
* This file is part of Scope Guard.
*
* Scope Guard 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.
*
* Scope Guard 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 Scope Guard. If not, see <http://www.gnu.org/licenses/>.
*/

#pragma once

#include <utility>

namespace sr
{
template<class Ressource, class Deleter>
class unique_resource_t
{
public:

explicit unique_resource_t(Ressource&& res, Deleter&& deleter, bool shouldrun = true) noexcept : m_deleter(std::move(deleter))
{
static_cast<void>(res);
static_cast<void>(shouldrun);
}


~unique_resource_t()
{
m_deleter();
}


private:

Deleter m_deleter;
};



template<class Ressource, class Deleter>
unique_resource_t<Ressource, Deleter> unique_resource(Ressource&& res, Deleter d) noexcept
{
return unique_resource_t<Ressource, Deleter>{std::move(res), std::move(d), true};
}

}



+ 2
- 1
test/CMakeLists.txt Целия файл

@@ -15,10 +15,11 @@ endfunction()


add_test_suite(ScopeGuardTest)
add_test_suite(UniqueResourceTest)


add_custom_target(unittest ScopeGuardTest
COMMAND UniqueResourceTest
COMMENT "Running unittests\n\n"
VERBATIM
)

+ 36
- 0
test/UniqueResourceTest.cpp Целия файл

@@ -0,0 +1,36 @@
/*
* Scope Guard
* Copyright (C) 2017 offa
*
* This file is part of Scope Guard.
*
* Scope Guard 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.
*
* Scope Guard 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 Scope Guard. If not, see <http://www.gnu.org/licenses/>.
*/

#include "unique_resource.h"
#include <catch.hpp>

TEST_CASE("deleter called on destruction", "[UniqueResource]")
{
std::size_t calls{0};
constexpr int handle{3};

{
auto guard = sr::unique_resource(handle, [&calls] { ++calls; });
static_cast<void>(guard);
}

REQUIRE(calls == 1);
}


Loading…
Отказ
Запис