Source · fairmq/tools/
InstanceLimit.hpublic header
Defines struct fair::mq::tools::InstanceLimiter.
1/********************************************************************************2 * Copyright (C) 2019 GSI Helmholtzzentrum fuer Schwerionenforschung GmbH *3 * *4 * This software is distributed under the terms of the *5 * GNU Lesser General Public Licence (LGPL) version 3, *6 * copied verbatim in the file "LICENSE" *7 ********************************************************************************/8 9#ifndef FAIR_MQ_TOOLS_INSTANCELIMIT_H10#define FAIR_MQ_TOOLS_INSTANCELIMIT_H11 12#include "Strings.h"13 14namespace fair::mq::tools15{16 17template<typename Tag, int Max>18struct InstanceLimiter19{20 InstanceLimiter() { Increment(); }21 explicit InstanceLimiter(const InstanceLimiter&) = delete;22 explicit InstanceLimiter(InstanceLimiter&&) = delete;23 InstanceLimiter& operator=(const InstanceLimiter&) = delete;24 InstanceLimiter& operator=(InstanceLimiter&&) = delete;25 ~InstanceLimiter() { Decrement(); }26 auto GetCount() -> int { return fCount; }27 28 private:29 auto Increment() -> void30 {31 if (fCount < Max) {32 ++fCount;33 } else {34 throw std::runtime_error(35 ToString("More than ", Max, " instances of ", Tag(), " in parallel not supported"));36 }37 }38 39 auto Decrement() -> void40 {41 if (fCount > 0) {42 --fCount;43 }44 }45 46 static int fCount; // NOLINT(cppcoreguidelines-avoid-non-const-global-variables)47};48 49template<typename Tag, int Max>50int InstanceLimiter<Tag, Max>::fCount(0); // NOLINT(cppcoreguidelines-avoid-non-const-global-variables)51 52} // namespace fair::mq::tools53 54#endif /* FAIR_MQ_TOOLS_INSTANCELIMIT_H */