Skip to content

Commit 07a1aa7

Browse files
[libdevcore] adds Utils.h with small helpers
1 parent 0e47543 commit 07a1aa7

File tree

2 files changed

+45
-0
lines changed

2 files changed

+45
-0
lines changed

libdevcore/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ set(sources
2323
SwarmHash.h
2424
UTF8.cpp
2525
UTF8.h
26+
Utils.h
2627
vector_ref.h
2728
Visitor.h
2829
Whiskers.cpp

libdevcore/Utils.h

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*
2+
This file is part of solidity.
3+
4+
solidity is free software: you can redistribute it and/or modify
5+
it under the terms of the GNU General Public License as published by
6+
the Free Software Foundation, either version 3 of the License, or
7+
(at your option) any later version.
8+
9+
solidity is distributed in the hope that it will be useful,
10+
but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
GNU General Public License for more details.
13+
14+
You should have received a copy of the GNU General Public License
15+
along with solidity. If not, see <http://www.gnu.org/licenses/>.
16+
*/
17+
18+
namespace dev
19+
{
20+
21+
/// Increments @p incrementable and ensures it is getting decremented upon destruction of the returned value.
22+
///
23+
/// Use this to ensure scoped increments/decrements in your code.
24+
template <typename Incrementable>
25+
auto inline scopedIncrement(Incrementable& incrementable)
26+
{
27+
struct ScopedIncrement {
28+
Incrementable& value;
29+
~ScopedIncrement() { --value; }
30+
};
31+
return ScopedIncrement{ ++incrementable };
32+
}
33+
34+
template <typename Callable>
35+
auto inline atScopeExit(Callable callable)
36+
{
37+
struct ExitCode {
38+
T leave;
39+
~ExitCode() { leave(); }
40+
};
41+
return ExitCode{ std::move(leave) };
42+
}
43+
44+
} // namespace dev

0 commit comments

Comments
 (0)