helics  3.5.2
BrokerFactory.hpp
1 /*
2 Copyright (c) 2017-2024,
3 Battelle Memorial Institute; Lawrence Livermore National Security, LLC; Alliance for Sustainable
4 Energy, LLC. See the top-level NOTICE for additional details. All rights reserved.
5 SPDX-License-Identifier: BSD-3-Clause
6 */
7 #pragma once
8 
9 #include "Broker.hpp"
10 #include "CoreTypes.hpp"
11 
12 #include <chrono>
13 #include <memory>
14 #include <string>
15 #include <type_traits>
16 #include <vector>
17 
18 namespace helics {
22 namespace BrokerFactory {
23 
24  class BrokerBuilder {
25  public:
27  virtual std::shared_ptr<Broker> build(std::string_view name) = 0;
28  };
29 
31  template<class BrokerTYPE>
32  class BrokerTypeBuilder final: public BrokerBuilder {
33  public:
34  static_assert(std::is_base_of<Broker, BrokerTYPE>::value,
35  "Type does not inherit from helics::Core");
36 
37  using broker_build_type = BrokerTYPE;
38  virtual std::shared_ptr<Broker> build(std::string_view name) override
39  {
40  return std::make_shared<BrokerTYPE>(name);
41  }
42  };
43 
45  void defineBrokerBuilder(std::shared_ptr<BrokerBuilder> builder,
46  std::string_view name,
47  int code);
48 
50  template<class BrokerTYPE>
51  std::shared_ptr<BrokerBuilder> addBrokerType(std::string_view brokerTypeName, int code)
52  {
53  auto bld = std::make_shared<BrokerTypeBuilder<BrokerTYPE>>();
54  std::shared_ptr<BrokerBuilder> bbld = std::static_pointer_cast<BrokerBuilder>(bld);
55  defineBrokerBuilder(bbld, brokerTypeName, code);
56  return bbld;
57  }
63  std::shared_ptr<Broker> create(CoreType type, std::string_view configureString);
65  std::shared_ptr<Broker> create(CoreType type, int argc, char* argv[]);
67  std::shared_ptr<Broker> create(CoreType type, std::vector<std::string> args);
68 
69  std::shared_ptr<Broker>
70  create(CoreType type, std::string_view brokerName, std::string_view configureString);
71 
72  std::shared_ptr<Broker>
73  create(CoreType type, std::string_view brokerName, int argc, char* argv[]);
74 
76  std::shared_ptr<Broker>
77  create(CoreType type, std::string_view brokerName, std::vector<std::string> args);
78 
82  std::shared_ptr<Broker> findBroker(std::string_view brokerName);
85  std::shared_ptr<Broker> getConnectedBroker();
89  std::shared_ptr<Broker> getBrokerByIndex(size_t index);
90 
92  std::shared_ptr<Broker> findJoinableBrokerOfType(CoreType type);
93 
95  std::vector<std::shared_ptr<Broker>> getAllBrokers();
96 
98  bool brokersActive();
99 
106  bool registerBroker(const std::shared_ptr<Broker>& broker, CoreType type);
110  void unregisterBroker(std::string_view name);
111 
113  void addAssociatedBrokerType(std::string_view name, CoreType type);
114 
121  size_t cleanUpBrokers();
122 
130  size_t cleanUpBrokers(std::chrono::milliseconds delay);
131 
135  bool copyBrokerIdentifier(std::string_view copyFromName, std::string_view copyToName);
136 
139 
141  void terminateAllBrokers();
143  void abortAllBrokers(int errorCode, std::string_view errorString);
144 } // namespace BrokerFactory
145 } // namespace helics
Definition: BrokerFactory.hpp:24
virtual std::shared_ptr< Broker > build(std::string_view name)=0
Definition: BrokerFactory.hpp:32
virtual std::shared_ptr< Broker > build(std::string_view name) override
Definition: BrokerFactory.hpp:38
std::shared_ptr< BrokerBuilder > addBrokerType(std::string_view brokerTypeName, int code)
Definition: BrokerFactory.hpp:51
std::shared_ptr< Broker > create(CoreType type, std::string_view configureString)
Definition: BrokerFactory.cpp:99
void unregisterBroker(std::string_view name)
Definition: BrokerFactory.cpp:299
void displayHelp(CoreType type)
Definition: BrokerFactory.cpp:316
bool copyBrokerIdentifier(std::string_view copyFromName, std::string_view copyToName)
Definition: BrokerFactory.cpp:294
void abortAllBrokers(int errorCode, std::string_view errorString)
Definition: BrokerFactory.cpp:281
std::vector< std::shared_ptr< Broker > > getAllBrokers()
Definition: BrokerFactory.cpp:214
std::shared_ptr< Broker > getConnectedBroker()
Definition: BrokerFactory.cpp:197
std::shared_ptr< Broker > findBroker(std::string_view brokerName)
Definition: BrokerFactory.cpp:180
std::shared_ptr< Broker > findJoinableBrokerOfType(CoreType type)
Definition: BrokerFactory.cpp:208
void defineBrokerBuilder(std::shared_ptr< BrokerBuilder > builder, std::string_view name, int code)
Definition: BrokerFactory.cpp:83
void terminateAllBrokers()
Definition: BrokerFactory.cpp:272
void addAssociatedBrokerType(std::string_view name, CoreType type)
Definition: BrokerFactory.cpp:307
size_t cleanUpBrokers()
Definition: BrokerFactory.cpp:263
bool brokersActive()
Definition: BrokerFactory.cpp:219
std::shared_ptr< Broker > getBrokerByIndex(size_t index)
Definition: BrokerFactory.cpp:202
bool registerBroker(const std::shared_ptr< Broker > &broker, CoreType type)
Definition: BrokerFactory.cpp:243
the main namespace for the helics co-simulation library User functions will be in the helics namespac...
Definition: AsyncFedCallInfo.hpp:14
CoreType
Definition: CoreTypes.hpp:46
@ UNRECOGNIZED
unknown