-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added utilities for simplified declaration of unique_resource.
A new resource_deleter template allows to generate a deleter function object from a reference to function. A new default_resource_value template allows to automatically generate resource traits from the default resource value, provided that no other resource values are invalid. The idea for a more compact unique_resource declaration was given by Janko Dedic in his review.
- Loading branch information
Showing
7 changed files
with
272 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
/* | ||
* Distributed under the Boost Software License, Version 1.0. | ||
* (See accompanying file LICENSE_1_0.txt or copy at | ||
* https://www.boost.org/LICENSE_1_0.txt) | ||
* | ||
* Copyright (c) 2024 Andrey Semashev | ||
*/ | ||
/*! | ||
* \file scope/default_resource_value.hpp | ||
* | ||
* This header contains definition of \c default_resource_value template. | ||
*/ | ||
|
||
#ifndef BOOST_SCOPE_DEFAULT_RESOURCE_VALUE_HPP_INCLUDED_ | ||
#define BOOST_SCOPE_DEFAULT_RESOURCE_VALUE_HPP_INCLUDED_ | ||
|
||
#include <type_traits> | ||
#include <boost/scope/detail/config.hpp> | ||
#include <boost/scope/detail/header.hpp> | ||
|
||
#ifdef BOOST_HAS_PRAGMA_ONCE | ||
#pragma once | ||
#endif | ||
|
||
namespace boost { | ||
namespace scope { | ||
|
||
#if !defined(BOOST_SCOPE_NO_CXX17_NONTYPE_TEMPLATE_PARAMETER_AUTO) | ||
|
||
/*! | ||
* \brief Simple resource traits for a single default resource value. | ||
* | ||
* This class template generates resource traits for `unique_resource` that specify | ||
* a single invalid resource value, which is also the default resource value, equal to | ||
* \c DefaultResourceValue. That is, any resource value other than \c DefaultResourceValue | ||
* is considered an allocated resource that needs to be freed. | ||
* | ||
* In order for the generated resource traits to enable optimized implementation of | ||
* `unique_resource`, the resource type must support non-throwing construction and assignment | ||
* from, and comparison for (in)equality with \c DefaultResourceValue. | ||
*/ | ||
template< auto DefaultResourceValue > | ||
struct default_resource_value | ||
{ | ||
//! Resource type | ||
typedef decltype(DefaultResourceValue) resource_type; | ||
|
||
//! Returns the default resource value | ||
static resource_type make_default() noexcept(std::is_nothrow_move_constructible< resource_type >::value) | ||
{ | ||
return DefaultResourceValue; | ||
} | ||
|
||
//! Tests if \a res is an allocated resource value (i.e. not the default) | ||
static bool is_allocated(resource_type const& res) noexcept(noexcept(res != DefaultResourceValue)) | ||
{ | ||
return res != DefaultResourceValue; | ||
} | ||
}; | ||
|
||
#endif // !defined(BOOST_SCOPE_NO_CXX17_NONTYPE_TEMPLATE_PARAMETER_AUTO) | ||
|
||
} // namespace scope | ||
} // namespace boost | ||
|
||
#include <boost/scope/detail/footer.hpp> | ||
|
||
#endif // BOOST_SCOPE_DEFAULT_RESOURCE_VALUE_HPP_INCLUDED_ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
/* | ||
* Distributed under the Boost Software License, Version 1.0. | ||
* (See accompanying file LICENSE_1_0.txt or copy at | ||
* https://www.boost.org/LICENSE_1_0.txt) | ||
* | ||
* Copyright (c) 2024 Andrey Semashev | ||
*/ | ||
/*! | ||
* \file scope/resource_deleter.hpp | ||
* | ||
* This header contains definition of \c resource_deleter template. | ||
*/ | ||
|
||
#ifndef BOOST_SCOPE_RESOURCE_DELETER_HPP_INCLUDED_ | ||
#define BOOST_SCOPE_RESOURCE_DELETER_HPP_INCLUDED_ | ||
|
||
#include <boost/scope/detail/config.hpp> | ||
#include <boost/scope/detail/header.hpp> | ||
|
||
#ifdef BOOST_HAS_PRAGMA_ONCE | ||
#pragma once | ||
#endif | ||
|
||
namespace boost { | ||
namespace scope { | ||
|
||
#if !defined(BOOST_SCOPE_NO_CXX17_NONTYPE_TEMPLATE_PARAMETER_AUTO) | ||
|
||
/*! | ||
* \brief Simple resource deleter wrapper. | ||
* | ||
* This wrapper generates a resource deleter function object that calls \c Deleter | ||
* to free the resource. \c Deleter must be callable with the single argument of | ||
* the resource type, and its returned value must be ignorable. The call must not throw | ||
* exceptions. | ||
* | ||
* Typically, this wrapper is used to generate resource deleter function objects from | ||
* plain functions. | ||
*/ | ||
template< auto Deleter > | ||
struct resource_deleter | ||
{ | ||
//! Function object return type | ||
typedef void result_type; | ||
|
||
//! Invokes \c Deleter on \a res | ||
template< typename Resource > | ||
result_type operator() (Resource&& res) const noexcept | ||
{ | ||
Deleter(static_cast< Resource&& >(res)); | ||
} | ||
}; | ||
|
||
#endif // !defined(BOOST_SCOPE_NO_CXX17_NONTYPE_TEMPLATE_PARAMETER_AUTO) | ||
|
||
} // namespace scope | ||
} // namespace boost | ||
|
||
#include <boost/scope/detail/footer.hpp> | ||
|
||
#endif // BOOST_SCOPE_RESOURCE_DELETER_HPP_INCLUDED_ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters