LeechCraft  0.6.70-15082-g543737046d
Modular cross-platform feature rich live environment.
util.h
Go to the documentation of this file.
1 /**********************************************************************
2  * LeechCraft - modular cross-platform feature rich internet client.
3  * Copyright (C) 2006-2014 Georg Rudoy
4  *
5  * Distributed under the Boost Software License, Version 1.0.
6  * (See accompanying file LICENSE or copy at https://www.boost.org/LICENSE_1_0.txt)
7  **********************************************************************/
8 
9 #pragma once
10 
11 #include <memory>
12 #include <functional>
13 #include <stdexcept>
14 
15 namespace LC
16 {
17 namespace Util
18 {
19  namespace detail
20  {
21  using DefaultScopeGuardDeleter = std::function<void ()>;
22 
23  class [[nodiscard]] SharedScopeGuard
24  {
25  std::shared_ptr<void> Guard_;
26  public:
27  template<typename F>
28  SharedScopeGuard (const F& f)
29  : Guard_ { nullptr, [f] (void*) { f (); } }
30  {
31  }
32 
33  SharedScopeGuard () = delete;
34 
35  SharedScopeGuard (const SharedScopeGuard&) = default;
36  SharedScopeGuard (SharedScopeGuard&&) = default;
37 
38  SharedScopeGuard& operator= (const SharedScopeGuard&) = default;
39  SharedScopeGuard& operator= (SharedScopeGuard&&) = default;
40  };
41 
42  template<typename F>
43  class [[nodiscard]] ScopeGuard
44  {
45  F F_;
46  bool Perform_ = true;
47  public:
48  ScopeGuard () noexcept
49  : F_ {}
50  , Perform_ { false }
51  {
52  }
53 
54  ScopeGuard (const F& f) noexcept
55  : F_ { f }
56  {
57  }
58 
59  ScopeGuard (const ScopeGuard&) = delete;
60  ScopeGuard& operator= (const ScopeGuard&) = delete;
61 
62  ScopeGuard& operator= (ScopeGuard&& other)
63  {
64  if (Perform_)
65  F_ ();
66 
67  F_ = other.F_;
68  Perform_ = other.Perform_;
69  other.Perform_ = false;
70  return *this;
71  }
72 
73  ScopeGuard (ScopeGuard&& other) noexcept
74  : F_ { other.F_ }
75  , Perform_ { other.Perform_ }
76  {
77  other.Perform_ = false;
78  }
79 
81  {
82  if (Perform_)
83  F_ ();
84  }
85 
86  void Dismiss () noexcept
87  {
88  Perform_ = false;
89  }
90 
92  {
93  Dismiss ();
95  }
96 
98  {
99  return EraseType ();
100  }
101 
102  SharedScopeGuard Shared ()
103  {
104  if (!Perform_)
105  throw std::logic_error { "this scope guard has already been converted to a shared one" };
106 
107  Perform_ = false;
108  return { F_ };
109  }
110  };
111  }
112 
113  using DefaultScopeGuard = detail::ScopeGuard<detail::DefaultScopeGuardDeleter>;
114 
135  template<typename F>
136  [[nodiscard]] detail::ScopeGuard<F> MakeScopeGuard (const F& f)
137  {
138  return { f };
139  }
140 }
141 }
LC::Util::DefaultScopeGuard
detail::ScopeGuard< detail::DefaultScopeGuardDeleter > DefaultScopeGuard
Definition: util.h:125
LC::Util::detail::SharedScopeGuard
Definition: util.h:41
LC::Util::MakeScopeGuard
detail::ScopeGuard< F > MakeScopeGuard(const F &f)
Returns an object performing passed function on scope exit.
Definition: util.h:148
LC::Util::detail::ScopeGuard
Definition: util.h:61
LC
Definition: constants.h:14
LC::Util::oral::sph::f
constexpr detail::ExprTree< detail::ExprType::LeafStaticPlaceholder, detail::MemberPtrs< Ptr > > f
Definition: oral.h:952
LC::Util::detail::DefaultScopeGuardDeleter
std::function< void()> DefaultScopeGuardDeleter
Definition: util.h:39