neoGFX
Cross-platform C++ app/game engine
Loading...
Searching...
No Matches
scoped.hpp
Go to the documentation of this file.
1// scoped.hpp
2/*
3 * Copyright (c) 2007 Leigh Johnston.
4 *
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions are
9 * met:
10 *
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 *
14 * * Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 *
18 * * Neither the name of Leigh Johnston nor the names of any
19 * other contributors to this software may be used to endorse or
20 * promote products derived from this software without specific prior
21 * written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
24 * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
25 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
26 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
27 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
28 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
29 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
30 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
31 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
32 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
33 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34*/
35
36#pragma once
37
38#include <neolib/neolib.hpp>
39#include <atomic>
41
42namespace neolib
43{
45 {
46 public:
47 scoped_flag(bool& aFlag, bool aValue = true) : iFlag{ aFlag }, iSaved{ aFlag }, iIgnore{ false } { iFlag = aValue; }
48 ~scoped_flag() { if (!iIgnore) iFlag = iSaved; }
49 public:
50 bool saved() const { return iSaved; }
51 void ignore() { iIgnore = true; }
52 private:
53 bool& iFlag;
54 bool iSaved;
55 bool iIgnore;
56 };
57
59 {
60 public:
61 scoped_atomic_flag(std::atomic<bool>& aFlag, bool aValue = true) : iFlag{ aFlag }, iSaved{ aFlag }, iIgnore{ false } { iFlag = aValue; }
62 ~scoped_atomic_flag() { if (!iIgnore) iFlag = iSaved; }
63 public:
64 bool saved() const { return iSaved; }
65 void ignore() { iIgnore = true; }
66 private:
67 std::atomic<bool>& iFlag;
68 bool iSaved;
69 bool iIgnore;
70 };
71
72 template <typename T>
74 {
75 public:
76 scoped_counter(T& aCounter) : iCounter{ aCounter }, iIgnore{ false } { ++iCounter; }
77 ~scoped_counter() { if (!iIgnore) --iCounter; }
78 public:
79 void ignore() { iIgnore = true; }
80 private:
81 T& iCounter;
82 bool iIgnore;
83 };
84
85 template <typename T>
87 {
88 public:
89 scoped_pointer(T*& aPointer, T* aValue) : iPointer{ aPointer }, iSaved{ aPointer }, iIgnore{ false } { iPointer = aValue; }
90 ~scoped_pointer() { if (!iIgnore) iPointer = iSaved; }
91 public:
92 T const& saved() const { return iSaved; }
93 void ignore() { iIgnore = true; }
94 private:
95 T*& iPointer;
96 T* iSaved;
97 bool iIgnore;
98 };
99
100 template <typename T>
102 {
103 public:
104 scoped_object(T& aObject, T aValue = {}) : iObject{ aObject }, iSaved{ aObject }, iIgnore{ false } { iObject = aValue; }
105 ~scoped_object() { if (!iIgnore) iObject = iSaved; }
106 public:
107 T const& saved() const { return iSaved; }
108 void ignore() { iIgnore = true; }
109 private:
110 T& iObject;
111 T iSaved;
112 bool iIgnore;
113 };
114
115 template <typename T>
117 {
118 public:
119 scoped_optional(i_optional<abstract_t<T>>& aOptional, T aValue) : iOptional{ aOptional }, iSaved{ aOptional }, iIgnore{ false } { iOptional = aValue; }
120 ~scoped_optional() { if (!iIgnore) iOptional = iSaved; }
121 public:
122 void ignore() { iIgnore = true; }
123 private:
124 i_optional<abstract_t<T>>& iOptional;
125 optional<T> iSaved;
126 bool iIgnore;
127 };
128
129 template <typename T>
131 {
132 public:
133 scoped_optional_if(i_optional<abstract_t<T>>& aOptional, T aValue) : iOptional{ aOptional }, iSaved{ aOptional }, iIgnore{ false } { if (iOptional == std::nullopt) iOptional = aValue; }
134 ~scoped_optional_if() { if (!iIgnore) iOptional = iSaved; }
135 public:
136 void ignore() { iIgnore = true; }
137 private:
138 i_optional<abstract_t<T>>& iOptional;
139 optional<T> iSaved;
140 bool iIgnore;
141 };
142}
scoped_atomic_flag(std::atomic< bool > &aFlag, bool aValue=true)
Definition scoped.hpp:61
scoped_counter(T &aCounter)
Definition scoped.hpp:76
bool saved() const
Definition scoped.hpp:50
scoped_flag(bool &aFlag, bool aValue=true)
Definition scoped.hpp:47
T const & saved() const
Definition scoped.hpp:107
scoped_object(T &aObject, T aValue={})
Definition scoped.hpp:104
scoped_optional_if(i_optional< abstract_t< T > > &aOptional, T aValue)
Definition scoped.hpp:133
scoped_optional(i_optional< abstract_t< T > > &aOptional, T aValue)
Definition scoped.hpp:119
scoped_pointer(T *&aPointer, T *aValue)
Definition scoped.hpp:89
T const & saved() const
Definition scoped.hpp:92
typename detail::abstract_type< T >::type abstract_t
Definition neolib.hpp:178