stlab 2.3.0
Modern, modular C++ algorithms, data structures, and concurrency primitives
Loading...
Searching...
No Matches
scope.hpp
Go to the documentation of this file.
1/*
2 Copyright 2015 Adobe
3 Distributed under the Boost Software License, Version 1.0.
4 (See accompanying file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
5*/
6
7/**************************************************************************************************/
8
9#ifndef STLAB_SCOPE_HPP
10#define STLAB_SCOPE_HPP
11
30
31/**************************************************************************************************/
32
33#include <stlab/config.hpp>
34
35#include <mutex>
36#include <tuple>
37#include <utility>
38
39/**************************************************************************************************/
40
41namespace stlab {
42STLAB_VERSION_NAMESPACE_BEGIN()
43
44
48
49/**************************************************************************************************/
50
51namespace detail {
52
53template <typename T, typename Tuple, size_t... S>
54auto scope_call(Tuple&& t, std::index_sequence<S...>) {
55 T scoped(std::forward<std::tuple_element_t<S, Tuple>>(std::get<S>(t))...);
56 (void)scoped;
57
58 // call the function
59 constexpr size_t last_index = std::tuple_size_v<Tuple> - 1;
60 return std::forward<std::tuple_element_t<last_index, Tuple>>(std::get<last_index>(t))();
61}
62
63} // namespace detail
64
65/**************************************************************************************************/
66
70template <typename T, typename... Args>
71inline auto scope(Args&&... args) {
72 return detail::scope_call<T>(std::forward_as_tuple(std::forward<Args>(args)...),
73 std::make_index_sequence<sizeof...(args) - 1>());
74}
75
78template <typename T, typename F>
79inline auto scope(std::mutex& m, F&& f) {
80 T scoped(m);
81 return std::forward<F>(f)();
82}
83
84/**************************************************************************************************/
85
87
88STLAB_VERSION_NAMESPACE_END()
89} // namespace stlab
90
91/**************************************************************************************************/
92
93#endif
94
95/**************************************************************************************************/
auto scope(Args &&... args)
Scopes the lifetime of an instance of T. All but the last arguments construct T; the last argument is...
Definition scope.hpp:71
Definition reverse.hpp:28