LIBEUNOMIA
読み取り中…
検索中…
一致する文字列を見つけられません
scopeguard.h
[詳解]
1/*
2 * Copyright 2024 oZ/acy (名賀月晃嗣) <acy@hiemalis.org>
3 *
4 * Redistribution and use in source and binary forms,
5 * with or without modification,
6 * are permitted provided that the following conditions are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright notice,
9 * this list of conditions and the following disclaimer.
10 *
11 * 2. Redistributions in binary form must reproduce the above copyright notice,
12 * this list of conditions and the following disclaimer in the documentation
13 * and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
16 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
17 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
18 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
19 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
20 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 *
27 */
36#ifndef INCLUDE_GUARD_EUNOMIA_SCOPE_GUARD_H
37#define INCLUDE_GUARD_EUNOMIA_SCOPE_GUARD_H
38
39#include <utility>
40
41
42namespace eunomia
43{
44template <typename Func> class ScopeGuard;
45
46template <typename Func>
47ScopeGuard<Func> makeScopeGuard(Func&&);
48
52template <typename Func>
54{
55private:
56 Func func_;
57 bool active_;
58
59 ScopeGuard() = delete;
60 ScopeGuard(const ScopeGuard&) = delete;
61 ScopeGuard& operator=(const ScopeGuard&) = delete;
62 // ムーヴ構築子、ムーヴ代入は、
63 // デストラクタのユーザ宣言により暗默的には提供されないので、deleteするまでもない。
64
65 explicit ScopeGuard(Func&& f) : func_(std::forward<Func>(f)), active_(true) {}
66
67public:
69 {
70 if (active_)
71 func_();
72 }
73
78 void dismiss() noexcept { active_ = false; }
79
81};
82
83
88template <typename Func>
89inline
91{
92 return ScopeGuard<Func>(std::forward<Func>(f));
93}
94
95
96}// end of namespace eunomia
97
98
99#endif // INCLUDE_GUARD_EUNOMIA_SCOPE_GUARD_H
畫像バッファ基底クラステンプレート
Definition imagebuffer.h:84
スコープガードクラステンプレート
Definition scopeguard.h:54
void dismiss() noexcept
解任
Definition scopeguard.h:78
~ScopeGuard()
Definition scopeguard.h:68
Definition colour.h:49
ScopeGuard< Func > makeScopeGuard(Func &&)
スコープガード作成
Definition scopeguard.h:90