FairMQ  1.4.33
C++ Message Queuing Library and Framework
InstanceLimit.h
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_H
10 #define FAIR_MQ_TOOLS_INSTANCELIMIT_H
11 
12 #include "Strings.h"
13 
14 namespace fair::mq::tools
15 {
16 
17 template<typename Tag, int Max>
18 struct InstanceLimiter
19 {
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() -> void
30  {
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() -> void
40  {
41  if (fCount > 0) {
42  --fCount;
43  }
44  }
45 
46  static int fCount;
47 };
48 
49 template<typename Tag, int Max>
50 int InstanceLimiter<Tag, Max>::fCount(0);
51 
52 } // namespace fair::mq::tools
53 
54 #endif /* FAIR_MQ_TOOLS_INSTANCELIMIT_H */
fair::mq::tools::InstanceLimiter
Definition: InstanceLimit.h:25

privacy