/****************************************************************************/1// Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo2// Copyright (C) 2018-2025 German Aerospace Center (DLR) and others.3// This program and the accompanying materials are made available under the4// terms of the Eclipse Public License 2.0 which is available at5// https://www.eclipse.org/legal/epl-2.0/6// This Source Code may also be made available under the following Secondary7// Licenses when the conditions for such availability set forth in the Eclipse8// Public License 2.0 are satisfied: GNU General Public License, version 29// or later which is available at10// https://www.gnu.org/licenses/old-licenses/gpl-2.0-standalone.html11// SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-or-later12/****************************************************************************/13/// @file ScopedLocker.h14/// @author Michael Behrisch15/// @date 2018-11-1416///17// A scoped lock which only triggers on condition18/****************************************************************************/19#pragma once20#include <config.h>212223// ===========================================================================24// class declarations25// ===========================================================================26namespace FX {27class FXMutex;28}293031// ===========================================================================32// class definitions33// ===========================================================================34/**35* @class ScopedLocker36* @brief A scoped lock which only triggers on condition37*/38template<typename T = FX::FXMutex, bool IGNORE_COND = false>39class ScopedLocker {4041public:42#ifdef _MSC_VER43#pragma warning(push)44#pragma warning(disable: 4127) // mask warning about constant condition45#endif46/// Construct & lock associated mutex if the condition is true47ScopedLocker(T& m, const bool condition = true)48: myMutex(m), myCondition(condition) {49if (IGNORE_COND || condition) {50m.lock();51}52}5354/// Destroy and unlock associated mutex55~ScopedLocker() {56if (IGNORE_COND || myCondition) {57myMutex.unlock();58}59}60#ifdef _MSC_VER61#pragma warning(pop)62#endif6364private:65T& myMutex;66const bool myCondition;6768private:69ScopedLocker& operator=(const ScopedLocker&) = delete;70};717273