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

Open-Sn / opensn / 20185909776

12 Dec 2025 06:55PM UTC coverage: 74.333% (+0.3%) from 74.037%
20185909776

push

github

web-flow
Merge pull request #859 from wdhawkins/td_source_driver

Adding time-dependent solver and time-dependent sources

367 of 398 new or added lines in 23 files covered. (92.21%)

113 existing lines in 28 files now uncovered.

18610 of 25036 relevant lines covered (74.33%)

68947552.69 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
  /// Returns a constant reference to the object registry.
83
  const std::map<std::string, ObjectRegistryEntry>& GetRegistry() const;
84

85
  /// Checks if the object registry has a specific text key.
86
  bool RegistryHasKey(const std::string& key) const;
87

88
  template <class TYPE>
89
  std::shared_ptr<TYPE> Create(const std::string& type, const ParameterBlock& params) const
2,729✔
90
  {
91
    if (object_registry_.count(type) == 0)
2,729✔
UNCOV
92
      throw std::logic_error("No registered type \"" + type + "\" found.");
×
93

94
    auto object_entry = object_registry_.at(type);
2,729✔
95
    if (not object_entry.get_in_params_func)
2,729✔
UNCOV
96
      throw std::runtime_error(
×
97
        "Object is not constructable since it has no registered constructor");
98

99
    auto input_params = object_entry.get_in_params_func();
2,729✔
100
    input_params.SetObjectType(type);
2,729✔
101
    input_params.SetErrorOriginScope(type);
2,729✔
102
    input_params.AssignParameters(params);
2,729✔
103
    auto obj = std::make_shared<TYPE>(input_params);
2,727✔
104
    return obj;
2,727✔
105
  }
2,729✔
106

107
  /// Returns the input parameters of a registered object.
108
  InputParameters GetRegisteredObjectParameters(const std::string& type) const;
109

110
  /// Dumps the object registry to stdout.
111
  void DumpRegister() const;
112

113
private:
114
  /// Private constructor because this is a singleton.
115
  ObjectFactory() = default;
116

117
  /// Checks that the registry key is available and throws a `std::logical_error` if it is not.
118
  void AssertRegistryKeyAvailable(const std::string& key,
119
                                  const std::string& calling_function) const;
120

121
  std::map<std::string, ObjectRegistryEntry> object_registry_;
122

123
public:
124
  /// Access to the singleton
125
  static ObjectFactory& GetInstance() noexcept;
126

127
  template <typename T>
128
  static char AddObjectToRegistry(const std::string& namespace_name, const std::string& object_name)
12,480✔
129
  {
130
    return AddObjectToRegistry<T>(namespace_name + "::" + object_name);
12,480✔
131
  }
132

133
  template <typename T>
134
  static char AddObjectToRegistry(const std::string& object_name)
12,480✔
135
  {
136
    auto& object_maker = GetInstance();
12,480✔
137

138
    object_maker.AssertRegistryKeyAvailable(object_name, __PRETTY_FUNCTION__);
12,480✔
139

140
    ObjectRegistryEntry reg_entry;
141
    reg_entry.get_in_params_func = &CallGetInputParamsFunction<T>;
12,480✔
142
    object_maker.object_registry_.insert(std::make_pair(object_name, reg_entry));
12,480✔
143

144
    return 0;
12,480✔
145
  }
146

147
  template <typename T>
148
  static char AddObjectToRegistryParamsOnly(const std::string& namespace_name,
960✔
149
                                            const std::string& object_name)
150
  {
151
    return AddObjectToRegistryParamsOnly<T>(namespace_name + "::" + object_name);
960✔
152
  }
153

154
  template <typename T>
155
  static char AddObjectToRegistryParamsOnly(const std::string& object_name)
960✔
156
  {
157
    auto& object_maker = GetInstance();
960✔
158

159
    object_maker.AssertRegistryKeyAvailable(object_name, __PRETTY_FUNCTION__);
960✔
160

161
    ObjectRegistryEntry reg_entry;
162
    reg_entry.get_in_params_func = &CallGetInputParamsFunction<T>;
960✔
163
    object_maker.object_registry_.insert(std::make_pair(object_name, reg_entry));
960✔
164

165
    return 0;
960✔
166
  }
167

168
  static char AddSyntaxBlockToRegistry(const std::string& namespace_name,
169
                                       const std::string& block_name,
170
                                       ObjectGetInParamsFunc syntax_function)
171
  {
172
    return AddSyntaxBlockToRegistry(namespace_name + "::" + block_name, syntax_function);
173
  }
174

175
  static char AddSyntaxBlockToRegistry(const std::string& block_name,
176
                                       ObjectGetInParamsFunc syntax_function)
177
  {
178
    auto& object_maker = GetInstance();
179

180
    object_maker.AssertRegistryKeyAvailable(block_name, __PRETTY_FUNCTION__);
181

182
    ObjectRegistryEntry reg_entry;
183
    reg_entry.get_in_params_func = syntax_function;
184
    object_maker.object_registry_.insert(std::make_pair(block_name, reg_entry));
185

186
    return 0;
187
  }
188

189
private:
190
  /// Utility redirection to call an object's static `GetInputParameters` function.
191
  template <typename T>
192
  static InputParameters CallGetInputParamsFunction()
2,729✔
193
  {
194
    return T::GetInputParameters();
2,729✔
195
  }
480✔
196

197
  /// Utility redirection to call an object's constructor with a specified list of input parameters.
198
  template <typename T, typename base_T>
199
  static std::shared_ptr<base_T> CallObjectConstructor(const InputParameters& params)
200
  {
201
    static_assert(std::is_base_of_v<base_T, T>, "Is not a base");
202
    return std::make_shared<T>(params);
203
  }
204
};
205

206
} // 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