helics  3.3.0
CoreFactory.hpp
1 /*
2 Copyright (c) 2017-2022,
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(std::string_view 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(std::string_view 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
48  defineCoreBuilder(std::shared_ptr<CoreBuilder> cb, std::string_view coreTypeName, int code);
49 
51  template<class CoreTYPE>
52  std::shared_ptr<CoreBuilder> addCoreType(std::string_view coreTypeName, int code)
53  {
54  auto bld = std::make_shared<CoreTypeBuilder<CoreTYPE>>();
55  std::shared_ptr<CoreBuilder> cbld = std::static_pointer_cast<CoreBuilder>(bld);
56  defineCoreBuilder(cbld, coreTypeName, code);
57  return cbld;
58  }
59 
61  std::vector<std::string> getAvailableCoreTypes();
62 
68  std::shared_ptr<Core>
69  create(CoreType type, std::string_view coreName, std::string_view configureString);
75  std::shared_ptr<Core> create(CoreType type, std::string_view configureString);
76 
82  std::shared_ptr<Core> create(CoreType type, int argc, char* argv[]);
83 
90  std::shared_ptr<Core> create(std::vector<std::string> args);
91 
96  std::shared_ptr<Core> create(CoreType type, std::vector<std::string> args);
97 
105  std::shared_ptr<Core> create(int argc, char* argv[]);
106 
114  std::shared_ptr<Core> create(CoreType type, std::string_view coreName, int argc, char* argv[]);
115 
122  std::shared_ptr<Core>
123  create(CoreType type, std::string_view coreName, std::vector<std::string> args);
124 
127  std::shared_ptr<Core>
128  FindOrCreate(CoreType type, std::string_view coreName, std::string_view configureString);
129 
132  std::shared_ptr<Core>
133  FindOrCreate(CoreType type, std::string_view coreName, int argc, char* argv[]);
134 
137  std::shared_ptr<Core>
138  FindOrCreate(CoreType type, std::string_view coreName, std::vector<std::string> args);
140  std::shared_ptr<Core> findJoinableCoreOfType(CoreType type);
141 
145  std::shared_ptr<Core> findCore(std::string_view name);
146 
148  std::shared_ptr<Core> getEmptyCore();
149 
152 
159  bool registerCore(const std::shared_ptr<Core>& core, CoreType type);
160 
164  void unregisterCore(std::string_view name);
165 
167  void addAssociatedCoreType(std::string_view name, CoreType type);
168 
174  size_t cleanUpCores();
181  size_t cleanUpCores(std::chrono::milliseconds delay);
182 
186  bool copyCoreIdentifier(std::string_view copyFromName, std::string_view copyToName);
187 
190 
192  void terminateAllCores();
194  void abortAllCores(int errorCode, std::string_view errorString);
195 
197  size_t getCoreCount();
198 } // namespace CoreFactory
199 
200 } // namespace helics
helics::CoreFactory::findCore
std::shared_ptr< Core > findCore(std::string_view name)
Definition: CoreFactory.cpp:304
helics::CoreFactory::addCoreType
std::shared_ptr< CoreBuilder > addCoreType(std::string_view coreTypeName, int code)
Definition: CoreFactory.hpp:52
helics::CoreFactory::registerCore
bool registerCore(const std::shared_ptr< Core > &core, CoreType type)
Definition: CoreFactory.cpp:334
helics::CoreType
CoreType
Definition: CoreTypes.hpp:36
helics::CoreFactory::getAvailableCoreTypes
std::vector< std::string > getAvailableCoreTypes()
Definition: CoreFactory.cpp:93
helics::CoreFactory::findJoinableCoreOfType
std::shared_ptr< Core > findJoinableCoreOfType(CoreType type)
Definition: CoreFactory.cpp:309
helics::CommonCore
Definition: CommonCore.hpp:74
helics::CoreFactory::addAssociatedCoreType
void addAssociatedCoreType(std::string_view name, CoreType type)
Definition: CoreFactory.cpp:399
helics::CoreFactory::abortAllCores
void abortAllCores(int errorCode, std::string_view errorString)
Definition: CoreFactory.cpp:368
helics::helicsCLI11App
Definition: helicsCLI11.hpp:42
helics::Core
Definition: core/Core.hpp:41
helics::CoreType::UNRECOGNIZED
@ UNRECOGNIZED
unknown
helics::RegistrationFailure
Definition: core-exceptions.hpp:85
helics::CoreFactory::CoreTypeBuilder
Definition: CoreFactory.hpp:34
helics::CoreFactory::terminateAllCores
void terminateAllCores()
Definition: CoreFactory.cpp:359
helics::CoreFactory::getCoreCount
size_t getCoreCount()
Definition: CoreFactory.cpp:383
helics::CoreFactory::FindOrCreate
std::shared_ptr< Core > FindOrCreate(CoreType type, std::string_view coreName, std::vector< std::string > args)
Definition: CoreFactory.cpp:219
helics::CoreFactory::copyCoreIdentifier
bool copyCoreIdentifier(std::string_view copyFromName, std::string_view copyToName)
Definition: CoreFactory.cpp:387
helics::CoreFactory::displayHelp
void displayHelp(CoreType type)
Definition: CoreFactory.cpp:407
helics::CoreFactory::unregisterCore
void unregisterCore(std::string_view name)
Definition: CoreFactory.cpp:392
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:349
helics::CoreFactory::getEmptyCore
std::shared_ptr< Core > getEmptyCore()
Definition: CoreFactory.cpp:119
helics::CommonCore::processDisconnect
virtual void processDisconnect(bool skipUnregister=false) override final
Definition: CommonCore.cpp:200
core-exceptions.hpp
helics::gLocalCoreId
constexpr LocalFederateId gLocalCoreId(-259)
helics::EmptyCore
Definition: EmptyCore.hpp:19
helics::CoreFactory::getEmptyCorePtr
Core * getEmptyCorePtr()
Definition: CoreFactory.cpp:124
CoreTypes.hpp
helics::CoreFactory::CoreBuilder
Definition: CoreFactory.hpp:27
helics::HelicsException
Definition: core-exceptions.hpp:18