Skip to content

Commit e0105dc

Browse files
committed
refactor how information is passed to instances
using a struct is more future-proof
1 parent 68408df commit e0105dc

11 files changed

Lines changed: 90 additions & 99 deletions

File tree

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@ using namespace fmu4cpp;
2525
class BouncingBall : public fmu_base {
2626

2727
public:
28-
BouncingBall(const std::string &instanceName, const std::string &resources)
29-
: fmu_base(instanceName, resources) {
28+
explicit BouncingBall(const fmu_data data)
29+
: fmu_base(std::move(data)) {
3030

3131
register_variable(
3232
real(

export/examples/BouncingBall/bouncing_ball.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ using namespace fmu4cpp;
77
class BouncingBall : public fmu_base {
88

99
public:
10-
BouncingBall(const std::string &instanceName, const std::filesystem::path &resources)
11-
: fmu_base(instanceName, resources) {
10+
explicit BouncingBall(const fmu_data &data)
11+
: fmu_base(data) {
1212

1313
register_variable(
1414
real(

export/examples/Resource/resource.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,18 @@
33
#include <fmu4cpp/fmu_base.hpp>
44

55
#include <fstream>
6+
#include <utility>
67

78
using namespace fmu4cpp;
89

910

1011
class Resource : public fmu_base {
1112

1213
public:
13-
Resource(const std::string &instanceName, const std::filesystem::path &resources)
14-
: fmu_base(instanceName, resources) {
14+
explicit Resource(fmu_data data)
15+
: fmu_base(std::move(data)) {
1516

16-
std::ifstream ifs(resources / "file.txt");
17+
std::ifstream ifs(resourceLocation() / "file.txt");
1718

1819
std::getline(ifs, content_);
1920

@@ -38,7 +39,6 @@ class Resource : public fmu_base {
3839

3940
private:
4041
std::string content_;
41-
4242
};
4343

4444
model_info fmu4cpp::get_model_info() {

export/examples/SimplePendulum/simple_pendulum.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ constexpr double pi = 3.14159265358979323846;
99

1010
class SimplePendulum : public fmu_base {
1111
public:
12-
SimplePendulum(const std::string &instanceName, const std::filesystem::path &resources)
13-
: fmu_base(instanceName, resources) {
12+
explicit SimplePendulum(const fmu_data &data)
13+
: fmu_base(data) {
1414

1515
register_variable(real("angle", &angle_)
1616
.setCausality(causality_t::OUTPUT)

export/include/fmu4cpp/fmu_base.hpp

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -16,29 +16,34 @@
1616

1717
#include <filesystem>
1818

19-
#define FMU4CPP_INSTANTIATE(MODELCLASS) \
20-
std::unique_ptr<fmu_base> fmu4cpp::createInstance(const std::string &instanceName, \
21-
const std::filesystem::path &fmuResourceLocation) { \
22-
return std::make_unique<MODELCLASS>(instanceName, fmuResourceLocation); \
19+
#define FMU4CPP_INSTANTIATE(MODELCLASS) \
20+
std::unique_ptr<fmu4cpp::fmu_base> fmu4cpp::createInstance(const fmu4cpp::fmu_data &data) { \
21+
return std::make_unique<MODELCLASS>(data); \
2322
}
2423

2524
namespace fmu4cpp {
2625

26+
struct fmu_data {
27+
logger *logger;
28+
std::string instance_name;
29+
std::filesystem::path resourceLocation;
30+
};
31+
2732
class fmu_base {
2833

2934
public:
30-
fmu_base(std::string instance_name, std::filesystem::path resourceLocation)
31-
: instanceName_(std::move(instance_name)), resourceLocation_(std::move(resourceLocation)) {}
35+
explicit fmu_base(fmu_data data)
36+
: data_(std::move(data)) {}
3237

3338
fmu_base(const fmu_base &) = delete;
3439
fmu_base(const fmu_base &&) = delete;
3540

3641
[[nodiscard]] std::string instanceName() const {
37-
return instanceName_;
42+
return data_.instance_name;
3843
}
3944

4045
[[nodiscard]] const std::filesystem::path &resourceLocation() const {
41-
return resourceLocation_;
46+
return data_.resourceLocation;
4247
}
4348

4449
[[nodiscard]] std::optional<IntVariable> get_int_variable(const std::string &name) const {
@@ -143,13 +148,9 @@ namespace fmu4cpp {
143148

144149
[[nodiscard]] std::string make_description() const;
145150

146-
void __set_logger(logger *logger) {
147-
logger_ = logger;
148-
}
149-
150151
void log(const fmi2Status s, const std::string &message) {
151-
if (logger_) {
152-
logger_->log(s, message);
152+
if (data_.logger) {
153+
data_.logger->log(s, message);
153154
}
154155
}
155156

@@ -199,12 +200,9 @@ namespace fmu4cpp {
199200

200201

201202
private:
202-
logger *logger_ = nullptr;
203+
fmu_data data_;
203204
size_t numVariables_{1};
204205

205-
std::string instanceName_;
206-
std::filesystem::path resourceLocation_;
207-
208206
std::vector<IntVariable> integers_;
209207
std::vector<RealVariable> reals_;
210208
std::vector<BoolVariable> booleans_;
@@ -215,7 +213,7 @@ namespace fmu4cpp {
215213

216214
model_info get_model_info();
217215

218-
std::unique_ptr<fmu_base> createInstance(const std::string &instanceName, const std::filesystem::path &fmuResourceLocation);
216+
std::unique_ptr<fmu_base> createInstance(const fmu_data &data);
219217

220218
}// namespace fmu4cpp
221219

export/src/fmu4cpp/fmi2.cpp

Lines changed: 41 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,14 @@ namespace {
1616
// A struct that holds all the data for one model instance.
1717
struct Component {
1818

19-
Component(std::unique_ptr<fmu4cpp::fmu_base> slave, const fmi2CallbackFunctions &callbackFunctions)
19+
Component(std::unique_ptr<fmu4cpp::fmu_base> slave, std::unique_ptr<fmu4cpp::logger> logger)
2020
: lastSuccessfulTime{std::numeric_limits<double>::quiet_NaN()},
2121
slave(std::move(slave)),
22-
logger(callbackFunctions, this->slave->instanceName()) {
23-
24-
this->slave->__set_logger(&logger);
25-
}
22+
logger(std::move(logger)) {}
2623

2724
double lastSuccessfulTime;
2825
std::unique_ptr<fmu4cpp::fmu_base> slave;
29-
fmu4cpp::logger logger;
26+
std::unique_ptr<fmu4cpp::logger> logger;
3027
};
3128

3229
}// namespace
@@ -42,7 +39,11 @@ const char *fmi2GetVersion(void) {
4239
}
4340

4441
FMI2_Export void write_description(const char *location) {
45-
const auto instance = fmu4cpp::createInstance("", "");
42+
const auto instance = fmu4cpp::createInstance({
43+
nullptr,
44+
"",
45+
"",
46+
});
4647
const auto xml = instance->make_description();
4748
std::ofstream of(location);
4849
of << xml;
@@ -79,17 +80,18 @@ fmi2Component fmi2Instantiate(fmi2String instanceName,
7980
resources.replace(0, 6 - magic, "");
8081
}
8182

82-
auto slave = fmu4cpp::createInstance(instanceName, resources);
83+
std::unique_ptr<fmu4cpp::logger> logger = std::make_unique<fmu4cpp::logger>(*functions, instanceName);
84+
85+
auto slave = fmu4cpp::createInstance({logger.get(), instanceName, resources});
8386
const auto guid = slave->guid();
8487
if (guid != fmuGUID) {
8588
std::cerr << "[fmu4cpp] Error. Wrong guid!" << std::endl;
86-
fmu4cpp::logger l(*functions, instanceName);
87-
l.log(fmi2Fatal, "Error. Wrong guid!");
89+
logger->log(fmi2Fatal, "Error. Wrong guid!");
8890
return nullptr;
8991
}
9092

91-
auto c = std::make_unique<Component>(std::move(slave), *functions);
92-
c->logger.setDebugLogging(loggingOn);
93+
auto c = std::make_unique<Component>(std::move(slave), std::move(logger));
94+
c->logger->setDebugLogging(loggingOn);
9395
return c.release();
9496
}
9597

@@ -110,10 +112,10 @@ fmi2Status fmi2SetupExperiment(fmi2Component c,
110112
component->slave->setup_experiment(startTime, stop, tol);
111113
return fmi2OK;
112114
} catch (const fmu4cpp::fatal_error &ex) {
113-
component->logger.log(fmi2Fatal, ex.what());
115+
component->logger->log(fmi2Fatal, ex.what());
114116
return fmi2Fatal;
115117
} catch (const std::exception &ex) {
116-
component->logger.log(fmi2Error, ex.what());
118+
component->logger->log(fmi2Error, ex.what());
117119
return fmi2Error;
118120
}
119121
}
@@ -125,10 +127,10 @@ fmi2Status fmi2EnterInitializationMode(fmi2Component c) {
125127
component->slave->enter_initialisation_mode();
126128
return fmi2OK;
127129
} catch (const fmu4cpp::fatal_error &ex) {
128-
component->logger.log(fmi2Fatal, ex.what());
130+
component->logger->log(fmi2Fatal, ex.what());
129131
return fmi2Fatal;
130132
} catch (const std::exception &ex) {
131-
component->logger.log(fmi2Error, ex.what());
133+
component->logger->log(fmi2Error, ex.what());
132134
return fmi2Error;
133135
}
134136
}
@@ -139,10 +141,10 @@ fmi2Status fmi2ExitInitializationMode(fmi2Component c) {
139141
component->slave->exit_initialisation_mode();
140142
return fmi2OK;
141143
} catch (const fmu4cpp::fatal_error &ex) {
142-
component->logger.log(fmi2Fatal, ex.what());
144+
component->logger->log(fmi2Fatal, ex.what());
143145
return fmi2Fatal;
144146
} catch (const std::exception &ex) {
145-
component->logger.log(fmi2Error, ex.what());
147+
component->logger->log(fmi2Error, ex.what());
146148
return fmi2Error;
147149
}
148150
}
@@ -153,10 +155,10 @@ fmi2Status fmi2Terminate(fmi2Component c) {
153155
component->slave->terminate();
154156
return fmi2OK;
155157
} catch (const fmu4cpp::fatal_error &ex) {
156-
component->logger.log(fmi2Fatal, ex.what());
158+
component->logger->log(fmi2Fatal, ex.what());
157159
return fmi2Fatal;
158160
} catch (const std::exception &ex) {
159-
component->logger.log(fmi2Error, ex.what());
161+
component->logger->log(fmi2Error, ex.what());
160162
return fmi2Error;
161163
}
162164
}
@@ -176,10 +178,10 @@ fmi2Status fmi2DoStep(
176178

177179
return fmi2Discard;
178180
} catch (const fmu4cpp::fatal_error &ex) {
179-
component->logger.log(fmi2Fatal, ex.what());
181+
component->logger->log(fmi2Fatal, ex.what());
180182
return fmi2Fatal;
181183
} catch (const std::exception &ex) {
182-
component->logger.log(fmi2Error, ex.what());
184+
component->logger->log(fmi2Error, ex.what());
183185
return fmi2Error;
184186
}
185187
}
@@ -205,10 +207,10 @@ fmi2Status fmi2GetInteger(
205207
component->slave->get_integer(vr, nvr, value);
206208
return fmi2OK;
207209
} catch (const fmu4cpp::fatal_error &ex) {
208-
component->logger.log(fmi2Fatal, ex.what());
210+
component->logger->log(fmi2Fatal, ex.what());
209211
return fmi2Fatal;
210212
} catch (const std::exception &ex) {
211-
component->logger.log(fmi2Error, ex.what());
213+
component->logger->log(fmi2Error, ex.what());
212214
return fmi2Error;
213215
}
214216
}
@@ -224,10 +226,10 @@ fmi2Status fmi2GetReal(
224226
component->slave->get_real(vr, nvr, value);
225227
return fmi2OK;
226228
} catch (const fmu4cpp::fatal_error &ex) {
227-
component->logger.log(fmi2Fatal, ex.what());
229+
component->logger->log(fmi2Fatal, ex.what());
228230
return fmi2Fatal;
229231
} catch (const std::exception &ex) {
230-
component->logger.log(fmi2Error, ex.what());
232+
component->logger->log(fmi2Error, ex.what());
231233
return fmi2Error;
232234
}
233235
}
@@ -243,10 +245,10 @@ fmi2Status fmi2GetBoolean(
243245
component->slave->get_boolean(vr, nvr, value);
244246
return fmi2OK;
245247
} catch (const fmu4cpp::fatal_error &ex) {
246-
component->logger.log(fmi2Fatal, ex.what());
248+
component->logger->log(fmi2Fatal, ex.what());
247249
return fmi2Fatal;
248250
} catch (const std::exception &ex) {
249-
component->logger.log(fmi2Error, ex.what());
251+
component->logger->log(fmi2Error, ex.what());
250252
return fmi2Error;
251253
}
252254
}
@@ -262,10 +264,10 @@ fmi2Status fmi2GetString(
262264
component->slave->get_string(vr, nvr, value);
263265
return fmi2OK;
264266
} catch (const fmu4cpp::fatal_error &ex) {
265-
component->logger.log(fmi2Fatal, ex.what());
267+
component->logger->log(fmi2Fatal, ex.what());
266268
return fmi2Fatal;
267269
} catch (const std::exception &ex) {
268-
component->logger.log(fmi2Error, ex.what());
270+
component->logger->log(fmi2Error, ex.what());
269271
return fmi2Error;
270272
}
271273
}
@@ -281,10 +283,10 @@ fmi2Status fmi2SetInteger(
281283
component->slave->set_integer(vr, nvr, value);
282284
return fmi2OK;
283285
} catch (const fmu4cpp::fatal_error &ex) {
284-
component->logger.log(fmi2Fatal, ex.what());
286+
component->logger->log(fmi2Fatal, ex.what());
285287
return fmi2Fatal;
286288
} catch (const std::exception &ex) {
287-
component->logger.log(fmi2Error, ex.what());
289+
component->logger->log(fmi2Error, ex.what());
288290
return fmi2Error;
289291
}
290292
}
@@ -300,10 +302,10 @@ fmi2Status fmi2SetReal(
300302
component->slave->set_real(vr, nvr, value);
301303
return fmi2OK;
302304
} catch (const fmu4cpp::fatal_error &ex) {
303-
component->logger.log(fmi2Fatal, ex.what());
305+
component->logger->log(fmi2Fatal, ex.what());
304306
return fmi2Fatal;
305307
} catch (const std::exception &ex) {
306-
component->logger.log(fmi2Error, ex.what());
308+
component->logger->log(fmi2Error, ex.what());
307309
return fmi2Error;
308310
}
309311
}
@@ -319,10 +321,10 @@ fmi2Status fmi2SetBoolean(
319321
component->slave->set_boolean(vr, nvr, value);
320322
return fmi2OK;
321323
} catch (const fmu4cpp::fatal_error &ex) {
322-
component->logger.log(fmi2Fatal, ex.what());
324+
component->logger->log(fmi2Fatal, ex.what());
323325
return fmi2Fatal;
324326
} catch (const std::exception &ex) {
325-
component->logger.log(fmi2Error, ex.what());
327+
component->logger->log(fmi2Error, ex.what());
326328
return fmi2Error;
327329
}
328330
}
@@ -338,10 +340,10 @@ fmi2Status fmi2SetString(
338340
component->slave->set_string(vr, nvr, value);
339341
return fmi2OK;
340342
} catch (const fmu4cpp::fatal_error &ex) {
341-
component->logger.log(fmi2Fatal, ex.what());
343+
component->logger->log(fmi2Fatal, ex.what());
342344
return fmi2Fatal;
343345
} catch (const std::exception &ex) {
344-
component->logger.log(fmi2Error, ex.what());
346+
component->logger->log(fmi2Error, ex.what());
345347
return fmi2Error;
346348
}
347349
}
@@ -397,7 +399,7 @@ fmi2Status fmi2SetDebugLogging(fmi2Component c,
397399
const fmi2String /*categories*/[]) {
398400

399401
const auto component = static_cast<Component *>(c);
400-
component->logger.setDebugLogging(loggingOn);
402+
component->logger->setDebugLogging(loggingOn);
401403
return fmi2OK;
402404
}
403405

0 commit comments

Comments
 (0)