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>
40
#include <
neolib/core/optional.hpp
>
41
42
namespace
neolib
43
{
44
class
scoped_flag
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
58
class
scoped_atomic_flag
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>
73
class
scoped_counter
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>
86
class
scoped_pointer
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>
101
class
scoped_object
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>
116
class
scoped_optional
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>
130
class
scoped_optional_if
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
}
neolib::i_optional
Definition
i_optional.hpp:58
neolib::optional
Definition
optional.hpp:55
neolib::scoped_atomic_flag
Definition
scoped.hpp:59
neolib::scoped_atomic_flag::ignore
void ignore()
Definition
scoped.hpp:65
neolib::scoped_atomic_flag::~scoped_atomic_flag
~scoped_atomic_flag()
Definition
scoped.hpp:62
neolib::scoped_atomic_flag::saved
bool saved() const
Definition
scoped.hpp:64
neolib::scoped_atomic_flag::scoped_atomic_flag
scoped_atomic_flag(std::atomic< bool > &aFlag, bool aValue=true)
Definition
scoped.hpp:61
neolib::scoped_counter
Definition
scoped.hpp:74
neolib::scoped_counter::~scoped_counter
~scoped_counter()
Definition
scoped.hpp:77
neolib::scoped_counter::scoped_counter
scoped_counter(T &aCounter)
Definition
scoped.hpp:76
neolib::scoped_counter::ignore
void ignore()
Definition
scoped.hpp:79
neolib::scoped_flag
Definition
scoped.hpp:45
neolib::scoped_flag::ignore
void ignore()
Definition
scoped.hpp:51
neolib::scoped_flag::saved
bool saved() const
Definition
scoped.hpp:50
neolib::scoped_flag::~scoped_flag
~scoped_flag()
Definition
scoped.hpp:48
neolib::scoped_flag::scoped_flag
scoped_flag(bool &aFlag, bool aValue=true)
Definition
scoped.hpp:47
neolib::scoped_object
Definition
scoped.hpp:102
neolib::scoped_object::saved
T const & saved() const
Definition
scoped.hpp:107
neolib::scoped_object::~scoped_object
~scoped_object()
Definition
scoped.hpp:105
neolib::scoped_object::scoped_object
scoped_object(T &aObject, T aValue={})
Definition
scoped.hpp:104
neolib::scoped_object::ignore
void ignore()
Definition
scoped.hpp:108
neolib::scoped_optional_if
Definition
scoped.hpp:131
neolib::scoped_optional_if::~scoped_optional_if
~scoped_optional_if()
Definition
scoped.hpp:134
neolib::scoped_optional_if::scoped_optional_if
scoped_optional_if(i_optional< abstract_t< T > > &aOptional, T aValue)
Definition
scoped.hpp:133
neolib::scoped_optional_if::ignore
void ignore()
Definition
scoped.hpp:136
neolib::scoped_optional
Definition
scoped.hpp:117
neolib::scoped_optional::~scoped_optional
~scoped_optional()
Definition
scoped.hpp:120
neolib::scoped_optional::scoped_optional
scoped_optional(i_optional< abstract_t< T > > &aOptional, T aValue)
Definition
scoped.hpp:119
neolib::scoped_optional::ignore
void ignore()
Definition
scoped.hpp:122
neolib::scoped_pointer
Definition
scoped.hpp:87
neolib::scoped_pointer::scoped_pointer
scoped_pointer(T *&aPointer, T *aValue)
Definition
scoped.hpp:89
neolib::scoped_pointer::saved
T const & saved() const
Definition
scoped.hpp:92
neolib::scoped_pointer::ignore
void ignore()
Definition
scoped.hpp:93
neolib::scoped_pointer::~scoped_pointer
~scoped_pointer()
Definition
scoped.hpp:90
neolib
Definition
application.hpp:46
neolib::abstract_t
typename detail::abstract_type< T >::type abstract_t
Definition
neolib.hpp:178
neolib.hpp
optional.hpp
neolib
include
neolib
core
scoped.hpp
Generated by
1.9.8