• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

Open-Sn / opensn / 17170369288

22 Aug 2025 06:44PM UTC coverage: 74.605% (+0.2%) from 74.434%
17170369288

push

github

web-flow
Merge pull request #721 from andrsd/issue/557

Renaming `SteadyStateSolver` `SteadyStateSourceSolver`

13 of 14 new or added lines in 3 files covered. (92.86%)

239 existing lines in 24 files now uncovered.

17550 of 23524 relevant lines covered (74.6%)

45237948.15 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

93.75
/framework/object_factory.h
1
// SPDX-FileCopyrightText: 2024 The OpenSn Authors <https://open-sn.github.io/opensn/>
2
// SPDX-License-Identifier: MIT
3

4
#pragma once
5

6
#include "framework/parameters/input_parameters.h"
7
#include "framework/logging/log_exceptions.h"
8
#include "framework/utils/utils.h"
9
#include <memory>
10

11
/**
12
 * Macro for registering an object within the ObjectFactory singleton.
13
 * \param namespace_name Name of the namespace within which the object is.
14
 * \param object_name Name of the object in the registry.
15
 * Example:
16
 * \code
17
 * OpenSnRegisterObjectInNamespace(kaka, Zorba);
18
 * \endcode
19
 * \note Remember to include the header "framework/object_factory.h".*/
20
#define OpenSnRegisterObjectInNamespace(namespace_name, object_name)                               \
21
  static char OpenSnJoinWords(unique_var_name_object_##object_name##_, __COUNTER__) =              \
22
    opensn::ObjectFactory::AddObjectToRegistry<object_name>(#namespace_name, #object_name)
23

24
#define OpenSnRegisterObject(object_name)                                                          \
25
  static char OpenSnJoinWords(unique_var_name_object_##object_name##_, __COUNTER__) =              \
26
    opensn::ObjectFactory::AddObjectToRegistry<object_name>(#object_name)
27

28
/**
29
 * Macro for registering an object alias within the ObjectFactory
30
 *
31
 * \param namespace_name Namespace name
32
 * \param alias Name of the object
33
 * \param object_name C++ class name to register.
34
 *
35
 * \note This will register a C++ class `object_name` such that it will show up as
36
 * `namespace_name.alias`
37
 */
38
#define OpenSnRegisterObjectAliasInNamespace(namespace_name, alias, object_name)                   \
39
  static char OpenSnJoinWords(unique_var_name_object_##object_name##_, __COUNTER__) =              \
40
    opensn::ObjectFactory::AddObjectToRegistry<object_name>(#namespace_name, #object_name)
41

42
/**
43
 * Macro for registering an object (parameters only) within the
44
 * ObjectFactory singleton.
45
 * \param namespace_name Name of the namespace within which the object is.
46
 * \param object_name Name of the object in the registry.
47
 * Example:
48
 * \code
49
 * OpenSnRegisterObjectParametersOnlyInNamespace(kaka, Zorba);
50
 * \endcode
51
 *
52
 * \note Remember to include the header "framework/object_factory.h"*/
53
#define OpenSnRegisterObjectParametersOnlyInNamespace(namespace_name, object_name)                 \
54
  static char OpenSnJoinWords(unique_var_name_object_##object_name##_, __COUNTER__) =              \
55
    opensn::ObjectFactory::AddObjectToRegistryParamsOnly<object_name>(#namespace_name,             \
56
                                                                      #object_name)
57

58
#define OpenSnRegisterObjectParametersOnly(object_name)                                            \
59
  static char OpenSnJoinWords(unique_var_name_object_##object_name##_, __COUNTER__) =              \
60
    opensn::ObjectFactory::AddObjectToRegistryParamsOnly<object_name>(#object_name)
61

62
namespace opensn
63
{
64

65
/// Singleton object for handling the registration and making of `Object`s.
66
class ObjectFactory
67
{
68
public:
69
  using ObjectGetInParamsFunc = InputParameters (*)();
70

71
  /// Structure storing the entities necessary for creating an object
72
  struct ObjectRegistryEntry
73
  {
74
    ObjectGetInParamsFunc get_in_params_func = nullptr;
75
  };
76

77
  // Deleted copy, move constructors and copy assignment operator
78
  ObjectFactory(const ObjectFactory&) = delete;
79
  ObjectFactory(const ObjectFactory&&) = delete;
80
  ObjectFactory& operator=(const ObjectFactory&) = delete;
81

82
  /// Access to the singleton
83
  static ObjectFactory& GetInstance() noexcept;
84

85
  /// Returns a constant reference to the object registry.
86
  const std::map<std::string, ObjectRegistryEntry>& GetRegistry() const;
87

88
  /// Checks if the object registry has a specific text key.
89
  bool RegistryHasKey(const std::string& key) const;
90

91
  template <typename T>
92
  static char AddObjectToRegistry(const std::string& namespace_name, const std::string& object_name)
9,600✔
93
  {
94
    return AddObjectToRegistry<T>(namespace_name + "::" + object_name);
9,600✔
95
  }
96

97
  template <typename T>
98
  static char AddObjectToRegistry(const std::string& object_name)
9,600✔
99
  {
100
    auto& object_maker = GetInstance();
9,600✔
101

102
    object_maker.AssertRegistryKeyAvailable(object_name, __PRETTY_FUNCTION__);
9,600✔
103

104
    ObjectRegistryEntry reg_entry;
105
    reg_entry.get_in_params_func = &CallGetInputParamsFunction<T>;
9,600✔
106
    object_maker.object_registry_.insert(std::make_pair(object_name, reg_entry));
9,600✔
107

108
    return 0;
9,600✔
109
  }
110

111
  template <typename T>
112
  static char AddObjectToRegistryParamsOnly(const std::string& namespace_name,
768✔
113
                                            const std::string& object_name)
114
  {
115
    return AddObjectToRegistryParamsOnly<T>(namespace_name + "::" + object_name);
768✔
116
  }
117

118
  template <typename T>
119
  static char AddObjectToRegistryParamsOnly(const std::string& object_name)
768✔
120
  {
121
    auto& object_maker = GetInstance();
768✔
122

123
    object_maker.AssertRegistryKeyAvailable(object_name, __PRETTY_FUNCTION__);
768✔
124

125
    ObjectRegistryEntry reg_entry;
126
    reg_entry.get_in_params_func = &CallGetInputParamsFunction<T>;
768✔
127
    object_maker.object_registry_.insert(std::make_pair(object_name, reg_entry));
768✔
128

129
    return 0;
768✔
130
  }
131

132
  static char AddSyntaxBlockToRegistry(const std::string& namespace_name,
133
                                       const std::string& block_name,
134
                                       ObjectGetInParamsFunc syntax_function)
135
  {
136
    return AddSyntaxBlockToRegistry(namespace_name + "::" + block_name, syntax_function);
137
  }
138

139
  static char AddSyntaxBlockToRegistry(const std::string& block_name,
140
                                       ObjectGetInParamsFunc syntax_function)
141
  {
142
    auto& object_maker = GetInstance();
143

144
    object_maker.AssertRegistryKeyAvailable(block_name, __PRETTY_FUNCTION__);
145

146
    ObjectRegistryEntry reg_entry;
147
    reg_entry.get_in_params_func = syntax_function;
148
    object_maker.object_registry_.insert(std::make_pair(block_name, reg_entry));
149

150
    return 0;
151
  }
152

153
  template <class TYPE>
154
  std::shared_ptr<TYPE> Create(const std::string& type, const ParameterBlock& params) const
2,068✔
155
  {
156
    if (object_registry_.count(type) == 0)
2,068✔
UNCOV
157
      throw std::logic_error("No registered type \"" + type + "\" found.");
×
158

159
    auto object_entry = object_registry_.at(type);
2,068✔
160
    if (not object_entry.get_in_params_func)
2,068✔
UNCOV
161
      throw std::runtime_error(
×
162
        "Object is not constructable since it has no registered constructor");
163

164
    auto input_params = object_entry.get_in_params_func();
2,068✔
165
    input_params.SetObjectType(type);
2,068✔
166
    input_params.SetErrorOriginScope(type);
2,068✔
167
    input_params.AssignParameters(params);
2,068✔
168
    auto obj = std::make_shared<TYPE>(input_params);
2,066✔
169
    return obj;
2,066✔
170
  }
2,068✔
171

172
  /// Returns the input parameters of a registered object.
173
  InputParameters GetRegisteredObjectParameters(const std::string& type) const;
174

175
  /// Dumps the object registry to stdout.
176
  void DumpRegister() const;
177

178
private:
179
  std::map<std::string, ObjectRegistryEntry> object_registry_;
180

181
  /// Private constructor because this is a singleton.
182
  ObjectFactory() = default;
183

184
  /// Utility redirection to call an object's static `GetInputParameters` function.
185
  template <typename T>
186
  static InputParameters CallGetInputParamsFunction()
2,068✔
187
  {
188
    return T::GetInputParameters();
2,068✔
189
  }
384✔
190

191
  /// Utility redirection to call an object's constructor with a specified list of input parameters.
192
  template <typename T, typename base_T>
193
  static std::shared_ptr<base_T> CallObjectConstructor(const InputParameters& params)
194
  {
195
    static_assert(std::is_base_of_v<base_T, T>, "Is not a base");
196
    return std::make_shared<T>(params);
197
  }
198

199
  /// Checks that the registry key is available and throws a `std::logical_error` if it is not.
200
  void AssertRegistryKeyAvailable(const std::string& key,
201
                                  const std::string& calling_function) const;
202
};
203

204
} // namespace opensn
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc