Eclipse SUMO - Simulation of Urban MObility
ScopedLocker.h
Go to the documentation of this file.
1/****************************************************************************/
2// Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.org/sumo
3// Copyright (C) 2018-2022 German Aerospace Center (DLR) and others.
4// This program and the accompanying materials are made available under the
5// terms of the Eclipse Public License 2.0 which is available at
6// https://www.eclipse.org/legal/epl-2.0/
7// This Source Code may also be made available under the following Secondary
8// Licenses when the conditions for such availability set forth in the Eclipse
9// Public License 2.0 are satisfied: GNU General Public License, version 2
10// or later which is available at
11// https://www.gnu.org/licenses/old-licenses/gpl-2.0-standalone.html
12// SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-or-later
13/****************************************************************************/
18// A scoped lock which only triggers on condition
19/****************************************************************************/
20#pragma once
21#include <config.h>
22
23
24// ===========================================================================
25// class declarations
26// ===========================================================================
27namespace FX {
28class FXMutex;
29}
30
31
32// ===========================================================================
33// class definitions
34// ===========================================================================
39template<typename T = FX::FXMutex, bool IGNORE_COND = false>
41public:
42
43#ifdef _MSC_VER
44#pragma warning(push)
45// ignore constant conditional expression (C4127) warnings
46#pragma warning(disable: 4127)
47#endif
49 ScopedLocker(T& m, const bool condition = true)
50 : myMutex(m), myCondition(condition) {
51 if (IGNORE_COND || condition) {
52 m.lock();
53 }
54 }
55
58 if (IGNORE_COND || myCondition) {
59 myMutex.unlock();
60 }
61 }
62#ifdef _MSC_VER
63#pragma warning(pop)
64#endif
65
66private:
68 const bool myCondition;
69
70private:
72};
A scoped lock which only triggers on condition.
Definition: ScopedLocker.h:40
~ScopedLocker()
Destroy and unlock associated mutex.
Definition: ScopedLocker.h:57
ScopedLocker(T &m, const bool condition=true)
Construct & lock associated mutex if the condition is true.
Definition: ScopedLocker.h:49
ScopedLocker & operator=(const ScopedLocker &)=delete
const bool myCondition
Definition: ScopedLocker.h:68
Definition: GUI.h:31