helics  3.0.1
CoreFactory.hpp
1 /*
2 Copyright (c) 2017-2021,
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 "Core.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 {
25 namespace CoreFactory {
27  class CoreBuilder {
28  public:
29  virtual std::shared_ptr<Core> build(const std::string& name) = 0;
30  };
31 
33  template<class CoreTYPE>
34  class CoreTypeBuilder final: public CoreBuilder {
35  public:
36  static_assert(std::is_base_of<Core, CoreTYPE>::value,
37  "Type does not inherit from helics::Core");
38 
39  using core_build_type = CoreTYPE;
40  virtual std::shared_ptr<Core> build(const std::string& name) override
41  {
42  return std::make_shared<CoreTYPE>(name);
43  }
44  };
45 
46  //** define a new Core Builder from the builder give a name and build code*/
47  void defineCoreBuilder(std::shared_ptr<CoreBuilder> cb,
48  const std::string& coreTypeName,
49  int code);
50 
52  template<class CoreTYPE>
53  std::shared_ptr<CoreBuilder> addCoreType(const std::string& coreTypeName, int code)
54  {
55  auto bld = std::make_shared<CoreTypeBuilder<CoreTYPE>>();
56  std::shared_ptr<CoreBuilder> cbld = std::static_pointer_cast<CoreBuilder>(bld);
57  defineCoreBuilder(cbld, coreTypeName, code);
58  return cbld;
59  }
60 
66  std::shared_ptr<Core>
67  create(CoreType type, const std::string& coreName, const std::string& configureString);
73  std::shared_ptr<Core> create(CoreType type, const std::string& configureString);
74 
80  std::shared_ptr<Core> create(CoreType type, int argc, char* argv[]);
81 
88  std::shared_ptr<Core> create(std::vector<std::string> args);
89 
94  std::shared_ptr<Core> create(CoreType type, std::vector<std::string> args);
95 
103  std::shared_ptr<Core> create(int argc, char* argv[]);
104 
112  std::shared_ptr<Core>
113  create(CoreType type, const std::string& coreName, int argc, char* argv[]);
114 
121  std::shared_ptr<Core>
122  create(CoreType type, const std::string& coreName, std::vector<std::string> args);
123 
126  std::shared_ptr<Core> FindOrCreate(CoreType type,
127  const std::string& coreName,
128  const std::string& configureString);
129 
132  std::shared_ptr<Core>
133  FindOrCreate(CoreType type, const std::string& coreName, int argc, char* argv[]);
134 
137  std::shared_ptr<Core>
138  FindOrCreate(CoreType type, const std::string& coreName, std::vector<std::string> args);
140  std::shared_ptr<Core> findJoinableCoreOfType(CoreType type);
141 
145  std::shared_ptr<Core> findCore(const std::string& name);
146 
148  std::shared_ptr<Core> getEmptyCore();
149 
156  bool registerCore(const std::shared_ptr<Core>& core, CoreType type);
157 
161  void unregisterCore(const std::string& name);
162 
164  void addAssociatedCoreType(const std::string& name, CoreType type);
165 
171  size_t cleanUpCores();
178  size_t cleanUpCores(std::chrono::milliseconds delay);
179 
183  bool copyCoreIdentifier(const std::string& copyFromName, const std::string& copyToName);
184 
187 
189  void terminateAllCores();
191  void abortAllCores(int errorCode, const std::string& errorString);
192 
194  size_t getCoreCount();
195 } // namespace CoreFactory
196 
197 } // namespace helics
helics::CoreFactory::registerCore
bool registerCore(const std::shared_ptr< Core > &core, CoreType type)
Definition: CoreFactory.cpp:305
helics::CoreType
CoreType
Definition: CoreTypes.hpp:36
helics::CoreFactory::findJoinableCoreOfType
std::shared_ptr< Core > findJoinableCoreOfType(CoreType type)
Definition: CoreFactory.cpp:280
helics::CommonCore
Definition: CommonCore.hpp:73
helics::helicsCLI11App
Definition: helicsCLI11.hpp:41
helics::CoreFactory::FindOrCreate
std::shared_ptr< Core > FindOrCreate(CoreType type, const std::string &coreName, std::vector< std::string > args)
Definition: CoreFactory.cpp:189
helics::CoreType::UNRECOGNIZED
@ UNRECOGNIZED
unknown
helics::RegistrationFailure
Definition: core-exceptions.hpp:84
helics::CoreFactory::CoreTypeBuilder
Definition: CoreFactory.hpp:34
helics::CoreFactory::terminateAllCores
void terminateAllCores()
Definition: CoreFactory.cpp:330
helics::CoreFactory::getCoreCount
size_t getCoreCount()
Definition: CoreFactory.cpp:349
helics::CoreFactory::unregisterCore
void unregisterCore(const std::string &name)
Definition: CoreFactory.cpp:358
helics::CoreFactory::displayHelp
void displayHelp(CoreType type)
Definition: CoreFactory.cpp:373
helics::CoreFactory::findCore
std::shared_ptr< Core > findCore(const std::string &name)
Definition: CoreFactory.cpp:275
helics
the main namespace for the helics co-simulation library User functions will be in the helics namespac...
Definition: AsyncFedCallInfo.hpp:14
helics::CoreFactory::cleanUpCores
size_t cleanUpCores()
Definition: CoreFactory.cpp:320
helics::CoreFactory::abortAllCores
void abortAllCores(int errorCode, const std::string &errorString)
Definition: CoreFactory.cpp:339
helics::CoreFactory::getEmptyCore
std::shared_ptr< Core > getEmptyCore()
Definition: CoreFactory.cpp:95
helics::CoreFactory::addCoreType
std::shared_ptr< CoreBuilder > addCoreType(const std::string &coreTypeName, int code)
Definition: CoreFactory.hpp:53
helics::CommonCore::processDisconnect
virtual void processDisconnect(bool skipUnregister=false) override final
Definition: CommonCore.cpp:192
core-exceptions.hpp
helics::CoreFactory::copyCoreIdentifier
bool copyCoreIdentifier(const std::string &copyFromName, const std::string &copyToName)
Definition: CoreFactory.cpp:353
helics::CoreFactory::addAssociatedCoreType
void addAssociatedCoreType(const std::string &name, CoreType type)
Definition: CoreFactory.cpp:365
helics::gLocalCoreId
constexpr LocalFederateId gLocalCoreId(-259)
CoreTypes.hpp
helics::CoreFactory::CoreBuilder
Definition: CoreFactory.hpp:27
helics::HelicsException
Definition: core-exceptions.hpp:18