Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ using namespace fmu4cpp;
class BouncingBall : public fmu_base {

public:
BouncingBall(const std::string &instanceName, const std::string &resources)
: fmu_base(instanceName, resources) {
explicit BouncingBall(const fmu_data data)
: fmu_base(std::move(data)) {

register_variable(
real(
Expand Down
4 changes: 2 additions & 2 deletions export/examples/BouncingBall/bouncing_ball.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ using namespace fmu4cpp;
class BouncingBall : public fmu_base {

public:
BouncingBall(const std::string &instanceName, const std::filesystem::path &resources)
: fmu_base(instanceName, resources) {
explicit BouncingBall(const fmu_data &data)
: fmu_base(data) {

register_variable(
real(
Expand Down
8 changes: 4 additions & 4 deletions export/examples/Resource/resource.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,18 @@
#include <fmu4cpp/fmu_base.hpp>

#include <fstream>
#include <utility>

using namespace fmu4cpp;


class Resource : public fmu_base {

public:
Resource(const std::string &instanceName, const std::filesystem::path &resources)
: fmu_base(instanceName, resources) {
explicit Resource(fmu_data data)
: fmu_base(std::move(data)) {

std::ifstream ifs(resources / "file.txt");
std::ifstream ifs(resourceLocation() / "file.txt");

std::getline(ifs, content_);

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

private:
std::string content_;

};

model_info fmu4cpp::get_model_info() {
Expand Down
4 changes: 2 additions & 2 deletions export/examples/SimplePendulum/simple_pendulum.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ constexpr double pi = 3.14159265358979323846;

class SimplePendulum : public fmu_base {
public:
SimplePendulum(const std::string &instanceName, const std::filesystem::path &resources)
: fmu_base(instanceName, resources) {
explicit SimplePendulum(const fmu_data &data)
: fmu_base(data) {

register_variable(real("angle", &angle_)
.setCausality(causality_t::OUTPUT)
Expand Down
36 changes: 17 additions & 19 deletions export/include/fmu4cpp/fmu_base.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,29 +16,34 @@

#include <filesystem>

#define FMU4CPP_INSTANTIATE(MODELCLASS) \
std::unique_ptr<fmu_base> fmu4cpp::createInstance(const std::string &instanceName, \
const std::filesystem::path &fmuResourceLocation) { \
return std::make_unique<MODELCLASS>(instanceName, fmuResourceLocation); \
#define FMU4CPP_INSTANTIATE(MODELCLASS) \
std::unique_ptr<fmu4cpp::fmu_base> fmu4cpp::createInstance(const fmu4cpp::fmu_data &data) { \
return std::make_unique<MODELCLASS>(data); \
}

namespace fmu4cpp {

struct fmu_data {
logger *fmiLogger;
std::string instance_name;
std::filesystem::path resourceLocation;
};

class fmu_base {

public:
fmu_base(std::string instance_name, std::filesystem::path resourceLocation)
: instanceName_(std::move(instance_name)), resourceLocation_(std::move(resourceLocation)) {}
explicit fmu_base(fmu_data data)
: data_(std::move(data)) {}

fmu_base(const fmu_base &) = delete;
fmu_base(const fmu_base &&) = delete;

[[nodiscard]] std::string instanceName() const {
return instanceName_;
return data_.instance_name;
}

[[nodiscard]] const std::filesystem::path &resourceLocation() const {
return resourceLocation_;
return data_.resourceLocation;
}

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

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

void __set_logger(logger *logger) {
logger_ = logger;
}

void log(const fmi2Status s, const std::string &message) {
if (logger_) {
logger_->log(s, message);
if (data_.fmiLogger) {
data_.fmiLogger->log(s, message);
}
}

Expand Down Expand Up @@ -199,12 +200,9 @@ namespace fmu4cpp {


private:
logger *logger_ = nullptr;
fmu_data data_;
size_t numVariables_{1};

std::string instanceName_;
std::filesystem::path resourceLocation_;

std::vector<IntVariable> integers_;
std::vector<RealVariable> reals_;
std::vector<BoolVariable> booleans_;
Expand All @@ -215,7 +213,7 @@ namespace fmu4cpp {

model_info get_model_info();

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

}// namespace fmu4cpp

Expand Down
80 changes: 41 additions & 39 deletions export/src/fmu4cpp/fmi2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,14 @@ namespace {
// A struct that holds all the data for one model instance.
struct Component {

Component(std::unique_ptr<fmu4cpp::fmu_base> slave, const fmi2CallbackFunctions &callbackFunctions)
Component(std::unique_ptr<fmu4cpp::fmu_base> slave, std::unique_ptr<fmu4cpp::logger> logger)
: lastSuccessfulTime{std::numeric_limits<double>::quiet_NaN()},
slave(std::move(slave)),
logger(callbackFunctions, this->slave->instanceName()) {

this->slave->__set_logger(&logger);
}
logger(std::move(logger)) {}

double lastSuccessfulTime;
std::unique_ptr<fmu4cpp::fmu_base> slave;
fmu4cpp::logger logger;
std::unique_ptr<fmu4cpp::logger> logger;
};

}// namespace
Expand All @@ -42,7 +39,11 @@ const char *fmi2GetVersion(void) {
}

FMI2_Export void write_description(const char *location) {
const auto instance = fmu4cpp::createInstance("", "");
const auto instance = fmu4cpp::createInstance({
nullptr,
"",
"",
});
const auto xml = instance->make_description();
std::ofstream of(location);
of << xml;
Expand Down Expand Up @@ -79,17 +80,18 @@ fmi2Component fmi2Instantiate(fmi2String instanceName,
resources.replace(0, 6 - magic, "");
}

auto slave = fmu4cpp::createInstance(instanceName, resources);
std::unique_ptr<fmu4cpp::logger> logger = std::make_unique<fmu4cpp::logger>(*functions, instanceName);

auto slave = fmu4cpp::createInstance({logger.get(), instanceName, resources});
const auto guid = slave->guid();
if (guid != fmuGUID) {
std::cerr << "[fmu4cpp] Error. Wrong guid!" << std::endl;
fmu4cpp::logger l(*functions, instanceName);
l.log(fmi2Fatal, "Error. Wrong guid!");
logger->log(fmi2Fatal, "Error. Wrong guid!");
return nullptr;
}

auto c = std::make_unique<Component>(std::move(slave), *functions);
c->logger.setDebugLogging(loggingOn);
auto c = std::make_unique<Component>(std::move(slave), std::move(logger));
c->logger->setDebugLogging(loggingOn);
return c.release();
}

Expand All @@ -110,10 +112,10 @@ fmi2Status fmi2SetupExperiment(fmi2Component c,
component->slave->setup_experiment(startTime, stop, tol);
return fmi2OK;
} catch (const fmu4cpp::fatal_error &ex) {
component->logger.log(fmi2Fatal, ex.what());
component->logger->log(fmi2Fatal, ex.what());
return fmi2Fatal;
} catch (const std::exception &ex) {
component->logger.log(fmi2Error, ex.what());
component->logger->log(fmi2Error, ex.what());
return fmi2Error;
}
}
Expand All @@ -125,10 +127,10 @@ fmi2Status fmi2EnterInitializationMode(fmi2Component c) {
component->slave->enter_initialisation_mode();
return fmi2OK;
} catch (const fmu4cpp::fatal_error &ex) {
component->logger.log(fmi2Fatal, ex.what());
component->logger->log(fmi2Fatal, ex.what());
return fmi2Fatal;
} catch (const std::exception &ex) {
component->logger.log(fmi2Error, ex.what());
component->logger->log(fmi2Error, ex.what());
return fmi2Error;
}
}
Expand All @@ -139,10 +141,10 @@ fmi2Status fmi2ExitInitializationMode(fmi2Component c) {
component->slave->exit_initialisation_mode();
return fmi2OK;
} catch (const fmu4cpp::fatal_error &ex) {
component->logger.log(fmi2Fatal, ex.what());
component->logger->log(fmi2Fatal, ex.what());
return fmi2Fatal;
} catch (const std::exception &ex) {
component->logger.log(fmi2Error, ex.what());
component->logger->log(fmi2Error, ex.what());
return fmi2Error;
}
}
Expand All @@ -153,10 +155,10 @@ fmi2Status fmi2Terminate(fmi2Component c) {
component->slave->terminate();
return fmi2OK;
} catch (const fmu4cpp::fatal_error &ex) {
component->logger.log(fmi2Fatal, ex.what());
component->logger->log(fmi2Fatal, ex.what());
return fmi2Fatal;
} catch (const std::exception &ex) {
component->logger.log(fmi2Error, ex.what());
component->logger->log(fmi2Error, ex.what());
return fmi2Error;
}
}
Expand All @@ -176,10 +178,10 @@ fmi2Status fmi2DoStep(

return fmi2Discard;
} catch (const fmu4cpp::fatal_error &ex) {
component->logger.log(fmi2Fatal, ex.what());
component->logger->log(fmi2Fatal, ex.what());
return fmi2Fatal;
} catch (const std::exception &ex) {
component->logger.log(fmi2Error, ex.what());
component->logger->log(fmi2Error, ex.what());
return fmi2Error;
}
}
Expand All @@ -205,10 +207,10 @@ fmi2Status fmi2GetInteger(
component->slave->get_integer(vr, nvr, value);
return fmi2OK;
} catch (const fmu4cpp::fatal_error &ex) {
component->logger.log(fmi2Fatal, ex.what());
component->logger->log(fmi2Fatal, ex.what());
return fmi2Fatal;
} catch (const std::exception &ex) {
component->logger.log(fmi2Error, ex.what());
component->logger->log(fmi2Error, ex.what());
return fmi2Error;
}
}
Expand All @@ -224,10 +226,10 @@ fmi2Status fmi2GetReal(
component->slave->get_real(vr, nvr, value);
return fmi2OK;
} catch (const fmu4cpp::fatal_error &ex) {
component->logger.log(fmi2Fatal, ex.what());
component->logger->log(fmi2Fatal, ex.what());
return fmi2Fatal;
} catch (const std::exception &ex) {
component->logger.log(fmi2Error, ex.what());
component->logger->log(fmi2Error, ex.what());
return fmi2Error;
}
}
Expand All @@ -243,10 +245,10 @@ fmi2Status fmi2GetBoolean(
component->slave->get_boolean(vr, nvr, value);
return fmi2OK;
} catch (const fmu4cpp::fatal_error &ex) {
component->logger.log(fmi2Fatal, ex.what());
component->logger->log(fmi2Fatal, ex.what());
return fmi2Fatal;
} catch (const std::exception &ex) {
component->logger.log(fmi2Error, ex.what());
component->logger->log(fmi2Error, ex.what());
return fmi2Error;
}
}
Expand All @@ -262,10 +264,10 @@ fmi2Status fmi2GetString(
component->slave->get_string(vr, nvr, value);
return fmi2OK;
} catch (const fmu4cpp::fatal_error &ex) {
component->logger.log(fmi2Fatal, ex.what());
component->logger->log(fmi2Fatal, ex.what());
return fmi2Fatal;
} catch (const std::exception &ex) {
component->logger.log(fmi2Error, ex.what());
component->logger->log(fmi2Error, ex.what());
return fmi2Error;
}
}
Expand All @@ -281,10 +283,10 @@ fmi2Status fmi2SetInteger(
component->slave->set_integer(vr, nvr, value);
return fmi2OK;
} catch (const fmu4cpp::fatal_error &ex) {
component->logger.log(fmi2Fatal, ex.what());
component->logger->log(fmi2Fatal, ex.what());
return fmi2Fatal;
} catch (const std::exception &ex) {
component->logger.log(fmi2Error, ex.what());
component->logger->log(fmi2Error, ex.what());
return fmi2Error;
}
}
Expand All @@ -300,10 +302,10 @@ fmi2Status fmi2SetReal(
component->slave->set_real(vr, nvr, value);
return fmi2OK;
} catch (const fmu4cpp::fatal_error &ex) {
component->logger.log(fmi2Fatal, ex.what());
component->logger->log(fmi2Fatal, ex.what());
return fmi2Fatal;
} catch (const std::exception &ex) {
component->logger.log(fmi2Error, ex.what());
component->logger->log(fmi2Error, ex.what());
return fmi2Error;
}
}
Expand All @@ -319,10 +321,10 @@ fmi2Status fmi2SetBoolean(
component->slave->set_boolean(vr, nvr, value);
return fmi2OK;
} catch (const fmu4cpp::fatal_error &ex) {
component->logger.log(fmi2Fatal, ex.what());
component->logger->log(fmi2Fatal, ex.what());
return fmi2Fatal;
} catch (const std::exception &ex) {
component->logger.log(fmi2Error, ex.what());
component->logger->log(fmi2Error, ex.what());
return fmi2Error;
}
}
Expand All @@ -338,10 +340,10 @@ fmi2Status fmi2SetString(
component->slave->set_string(vr, nvr, value);
return fmi2OK;
} catch (const fmu4cpp::fatal_error &ex) {
component->logger.log(fmi2Fatal, ex.what());
component->logger->log(fmi2Fatal, ex.what());
return fmi2Fatal;
} catch (const std::exception &ex) {
component->logger.log(fmi2Error, ex.what());
component->logger->log(fmi2Error, ex.what());
return fmi2Error;
}
}
Expand Down Expand Up @@ -397,7 +399,7 @@ fmi2Status fmi2SetDebugLogging(fmi2Component c,
const fmi2String /*categories*/[]) {

const auto component = static_cast<Component *>(c);
component->logger.setDebugLogging(loggingOn);
component->logger->setDebugLogging(loggingOn);
return fmi2OK;
}

Expand Down
Loading