Cbeam
Loading...
Searching...
No Matches
scoped_set.hpp
Go to the documentation of this file.
1/*
2Copyright (c) 2025 acrion innovations GmbH
3Authors: Stefan Zipproth, s.zipproth@acrion.ch
4
5This file is part of Cbeam, see https://github.com/acrion/cbeam and https://cbeam.org
6
7Cbeam is offered under a commercial and under the AGPL license.
8For commercial licensing, contact us at https://acrion.ch/sales. For AGPL licensing, see below.
9
10AGPL licensing:
11
12Cbeam is free software: you can redistribute it and/or modify
13it under the terms of the GNU Affero General Public License as published by
14the Free Software Foundation, either version 3 of the License, or
15(at your option) any later version.
16
17Cbeam is distributed in the hope that it will be useful,
18but WITHOUT ANY WARRANTY; without even the implied warranty of
19MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20GNU Affero General Public License for more details.
21
22You should have received a copy of the GNU Affero General Public License
23along with Cbeam. If not, see <https://www.gnu.org/licenses/>.
24*/
25
26#pragma once
27
28#include <atomic> // for std::atomic
29
30namespace cbeam::lifecycle
31{
38 template <typename T>
40 {
41 using type = T;
42 };
43
44 template <typename T>
45 struct extract_atomic_value_type<std::atomic<T>>
46 {
47 using type = T;
48 };
49
59 template <typename T>
61 {
62 public:
64
73 scoped_set(T& instance, ValueType newVal)
74 : _instance(instance)
75 , _old_value(instance)
76 {
77 _instance = newVal;
78 }
79
83 virtual ~scoped_set() noexcept
84 {
85 _instance = _old_value;
86 }
87
88 private:
89 T& _instance;
90 ValueType _old_value;
91 };
92}
typename extract_atomic_value_type< T >::type ValueType
Definition scoped_set.hpp:63
virtual ~scoped_set() noexcept
Destructor that restores the original value of the managed variable.
Definition scoped_set.hpp:83
scoped_set(T &instance, ValueType newVal)
Constructs a scoped_set and assigns newVal to instance.
Definition scoped_set.hpp:73
Manages the lifecycle of singletons, item registries, and scoped variables. This namespace introduces...
Definition item_registry.hpp:38
Extracts the underlying value type T from either T or std::atomic<T>.
Definition scoped_set.hpp:40
T type
Definition scoped_set.hpp:41