Add a guard_value utility for scoped temporary assignment
This commit is contained in:
parent
72cede2c8a
commit
508bc2519e
4 changed files with 47 additions and 0 deletions
|
@ -4135,6 +4135,7 @@
|
|||
<ClInclude Include="..\..\src\utils\parse_network_address.hpp" />
|
||||
<ClInclude Include="..\..\src\utils\reference_counter.hpp" />
|
||||
<ClInclude Include="..\..\src\utils\scope_exit.hpp" />
|
||||
<ClInclude Include="..\..\src\utils\guard_value.hpp" />
|
||||
<ClInclude Include="..\..\src\variable.hpp" />
|
||||
<ClInclude Include="..\..\src\variable_info.hpp" />
|
||||
<ClInclude Include="..\..\src\variable_info_detail.hpp" />
|
||||
|
|
|
@ -3032,6 +3032,7 @@
|
|||
<Filter>Desktop</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="..\..\src\utils\scope_exit.hpp" />
|
||||
<ClInclude Include="..\..\src\utils\guard_value.hpp" />
|
||||
<ClInclude Include="..\..\src\serialization\schema\key.hpp">
|
||||
<Filter>Serialization\Schema</Filter>
|
||||
</ClInclude>
|
||||
|
|
|
@ -2119,6 +2119,7 @@
|
|||
9197979A261A6FCA001E8133 /* wml_equivalence.hpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.h; path = wml_equivalence.hpp; sourceTree = "<group>"; };
|
||||
9197979B261A6FCA001E8133 /* wml_equivalence.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = wml_equivalence.cpp; sourceTree = "<group>"; };
|
||||
919797A6261A6FDB001E8133 /* test_macro_define.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = test_macro_define.cpp; sourceTree = "<group>"; };
|
||||
919797DF2623A28D001E8133 /* guard_value.hpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.h; path = guard_value.hpp; sourceTree = "<group>"; };
|
||||
919B37F71BAF789D00E0094C /* synced_user_choice.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = synced_user_choice.cpp; sourceTree = "<group>"; };
|
||||
919B37F91BAF78AB00E0094C /* synced_user_choice.hpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.h; path = synced_user_choice.hpp; sourceTree = "<group>"; };
|
||||
91A214E41CAD618700927AEA /* gettext.hpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.h; path = gettext.hpp; sourceTree = "<group>"; };
|
||||
|
@ -4217,6 +4218,7 @@
|
|||
91C55DA21CC078820040012E /* context_free_grammar_generator.cpp */,
|
||||
91C55DA31CC078820040012E /* context_free_grammar_generator.hpp */,
|
||||
EC5012711E06185600C4DFC6 /* general.hpp */,
|
||||
919797DF2623A28D001E8133 /* guard_value.hpp */,
|
||||
46DF5BCC1F46173700BE6D24 /* irdya_datetime.cpp */,
|
||||
46DF5BCB1F46173700BE6D24 /* irdya_datetime.hpp */,
|
||||
911F471B1CAE5A7E00F47035 /* iterable_pair.hpp */,
|
||||
|
|
43
src/utils/guard_value.hpp
Normal file
43
src/utils/guard_value.hpp
Normal file
|
@ -0,0 +1,43 @@
|
|||
/*
|
||||
Copyright (C) 2003 - 2018 the Battle for Wesnoth Project https://www.wesnoth.org/
|
||||
|
||||
This program is free software; you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation; either version 2 of the License, or
|
||||
(at your option) any later version.
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY.
|
||||
|
||||
See the COPYING file for more details.
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
namespace utils {
|
||||
|
||||
/**
|
||||
* Data-based RAII scope guard.
|
||||
* Assigns a value to a specific location, then restores the old value once it goes out of scope.
|
||||
*/
|
||||
template<typename T>
|
||||
class guard_value {
|
||||
T* ref_;
|
||||
T old_val_;
|
||||
public:
|
||||
/**
|
||||
* @param ref The memory location being guarded
|
||||
* @param new_val The new value to temporarily assign to that location
|
||||
*/
|
||||
guard_value(T& ref, T new_val)
|
||||
: ref_(&ref)
|
||||
, old_val_(ref)
|
||||
{
|
||||
ref = new_val;
|
||||
}
|
||||
~guard_value()
|
||||
{
|
||||
*ref_ = old_val_;
|
||||
}
|
||||
};
|
||||
|
||||
}
|
Loading…
Add table
Reference in a new issue