Skip to content

Commit

Permalink
misc
Browse files Browse the repository at this point in the history
  • Loading branch information
markaren committed Nov 7, 2024
1 parent 678bf8f commit 2cdeb8d
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 35 deletions.
6 changes: 3 additions & 3 deletions export/descriptionGenerator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,18 +59,18 @@ typedef void modelDescriptionTYPE(const char *);

int main(int argc, char **argv) {

if (argc != 2) return -1;
if (argc != 2) return 1;

std::string libName = argv[1];
auto handle = load_library(libName);
const auto handle = load_library(libName);

if (!handle) {
const auto err = "Unable to load dynamic library '" + libName + "'! " + getLastError();
std::cerr << err << std::endl;
return -1;
}

auto f = load_function<modelDescriptionTYPE*>(handle, "write_description");
const auto f = load_function<modelDescriptionTYPE*>(handle, "write_description");
f("modelDescription.xml");

free_library(handle);
Expand Down
56 changes: 34 additions & 22 deletions export/src/fmu4cpp/fmi2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,15 +78,15 @@ fmi2Component fmi2Instantiate(fmi2String instanceName,
}

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

auto c = new Component(std::move(slave), *functions);
const auto c = new Component(std::move(slave), *functions);
c->logger.setDebugLogging(loggingOn);
return c;
}
Expand All @@ -103,7 +103,7 @@ fmi2Status fmi2SetupExperiment(fmi2Component c,
if (stopTimeDefined) stop = stopTime;
if (toleranceDefined) tol = tolerance;

auto component = reinterpret_cast<Component *>(c);
const auto component = static_cast<Component *>(c);
try {
component->slave->setup_experiment(startTime, stop, tol);
return fmi2OK;
Expand All @@ -115,7 +115,7 @@ fmi2Status fmi2SetupExperiment(fmi2Component c,
}

fmi2Status fmi2EnterInitializationMode(fmi2Component c) {
auto component = reinterpret_cast<Component *>(c);
const auto component = static_cast<Component *>(c);

try {
component->slave->enter_initialisation_mode();
Expand All @@ -128,7 +128,7 @@ fmi2Status fmi2EnterInitializationMode(fmi2Component c) {
}

fmi2Status fmi2ExitInitializationMode(fmi2Component c) {
auto component = reinterpret_cast<Component *>(c);
const auto component = static_cast<Component *>(c);
try {
component->slave->exit_initialisation_mode();
return fmi2OK;
Expand All @@ -140,7 +140,7 @@ fmi2Status fmi2ExitInitializationMode(fmi2Component c) {
}

fmi2Status fmi2Terminate(fmi2Component c) {
auto component = reinterpret_cast<Component *>(c);
const auto component = static_cast<Component *>(c);
try {
component->slave->terminate();
return fmi2OK;
Expand All @@ -156,9 +156,10 @@ fmi2Status fmi2DoStep(
fmi2Real currentCommunicationPoint,
fmi2Real communicationStepSize,
fmi2Boolean /*noSetFMUStatePriorToCurrentPoint*/) {
auto component = reinterpret_cast<Component *>(c);

const auto component = static_cast<Component *>(c);
try {
double endTime = currentCommunicationPoint;
const double endTime = currentCommunicationPoint;
const auto ok = component->slave->do_step(
currentCommunicationPoint,
communicationStepSize);
Expand All @@ -169,9 +170,9 @@ fmi2Status fmi2DoStep(
component->lastSuccessfulTime = endTime;
return fmi2Discard;
}
} catch (const fmu4cpp::fatal_error &ex) {
} catch (const fmu4cpp::fatal_error &) {
return fmi2Fatal;
} catch (const std::exception &e) {
} catch (const std::exception &) {
return fmi2Error;
}
}
Expand All @@ -181,7 +182,7 @@ fmi2Status fmi2CancelStep(fmi2Component c) {
}

fmi2Status fmi2Reset(fmi2Component c) {
auto component = reinterpret_cast<Component *>(c);
const auto component = static_cast<Component *>(c);
component->slave->reset();
return fmi2OK;
}
Expand All @@ -191,7 +192,8 @@ fmi2Status fmi2GetInteger(
const fmi2ValueReference vr[],
size_t nvr,
fmi2Integer value[]) {
const auto component = reinterpret_cast<Component *>(c);

const auto component = static_cast<Component *>(c);
try {
component->slave->get_integer(vr, nvr, value);
return fmi2OK;
Expand All @@ -207,7 +209,8 @@ fmi2Status fmi2GetReal(
const fmi2ValueReference vr[],
size_t nvr,
fmi2Real value[]) {
const auto component = reinterpret_cast<Component *>(c);

const auto component = static_cast<Component *>(c);
try {
component->slave->get_real(vr, nvr, value);
return fmi2OK;
Expand All @@ -223,7 +226,8 @@ fmi2Status fmi2GetBoolean(
const fmi2ValueReference vr[],
size_t nvr,
fmi2Boolean value[]) {
const auto component = reinterpret_cast<Component *>(c);

const auto component = static_cast<Component *>(c);
try {
component->slave->get_boolean(vr, nvr, value);
return fmi2OK;
Expand All @@ -239,7 +243,8 @@ fmi2Status fmi2GetString(
const fmi2ValueReference vr[],
size_t nvr,
fmi2String value[]) {
const auto component = reinterpret_cast<Component *>(c);

const auto component = static_cast<Component *>(c);
try {
component->slave->get_string(vr, nvr, value);
return fmi2OK;
Expand All @@ -255,7 +260,8 @@ fmi2Status fmi2SetInteger(
const fmi2ValueReference vr[],
size_t nvr,
const fmi2Integer value[]) {
const auto component = reinterpret_cast<Component *>(c);

const auto component = static_cast<Component *>(c);
try {
component->slave->set_integer(vr, nvr, value);
return fmi2OK;
Expand All @@ -271,7 +277,8 @@ fmi2Status fmi2SetReal(
const fmi2ValueReference vr[],
size_t nvr,
const fmi2Real value[]) {
const auto component = reinterpret_cast<Component *>(c);

const auto component = static_cast<Component *>(c);
try {
component->slave->set_real(vr, nvr, value);
return fmi2OK;
Expand All @@ -287,7 +294,8 @@ fmi2Status fmi2SetBoolean(
const fmi2ValueReference vr[],
size_t nvr,
const fmi2Boolean value[]) {
const auto component = reinterpret_cast<Component *>(c);

const auto component = static_cast<Component *>(c);
try {
component->slave->set_boolean(vr, nvr, value);
return fmi2OK;
Expand All @@ -303,7 +311,8 @@ fmi2Status fmi2SetString(
const fmi2ValueReference vr[],
size_t nvr,
const fmi2String value[]) {
const auto component = reinterpret_cast<Component *>(c);

const auto component = static_cast<Component *>(c);
try {
component->slave->set_string(vr, nvr, value);
return fmi2OK;
Expand All @@ -326,7 +335,8 @@ fmi2Status fmi2GetRealStatus(
fmi2Component c,
const fmi2StatusKind s,
fmi2Real *value) {
const auto component = reinterpret_cast<Component *>(c);

const auto component = static_cast<Component *>(c);
if (s == fmi2LastSuccessfulTime) {
*value = component->lastSuccessfulTime;
return fmi2OK;
Expand Down Expand Up @@ -362,7 +372,8 @@ fmi2Status fmi2SetDebugLogging(fmi2Component c,
fmi2Boolean loggingOn,
size_t nCategories,
const fmi2String categories[]) {
auto component = reinterpret_cast<Component *>(c);

const auto component = static_cast<Component *>(c);
component->logger.setDebugLogging(loggingOn);
return fmi2OK;
}
Expand Down Expand Up @@ -422,7 +433,8 @@ fmi2Status fmi2FreeFMUstate(fmi2Component, fmi2FMUstate *) {
}

void fmi2FreeInstance(fmi2Component c) {
auto component = reinterpret_cast<Component *>(c);
const auto component = static_cast<Component *>(c);
delete component;
}

}
2 changes: 1 addition & 1 deletion export/src/fmu4cpp/hash.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
//https://stackoverflow.com/questions/66764096/calculating-stdhash-using-different-compilers
inline uint64_t fnv1a(std::string const &text) {

if (sizeof(void *) == 4) {
if constexpr (sizeof(void *) == 4) {
// 32-bit environment
uint32_t constexpr fnv_prime = 16777619U;
uint32_t constexpr fnv_offset_basis = 2166136261U;
Expand Down
21 changes: 12 additions & 9 deletions export/src/fmu4cpp/time.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,27 @@
#include <string>

namespace fmu4cpp {
std::string now() {
inline std::string now() {
time_t now = time(nullptr);
char buf[42];
constexpr size_t buf_size = 42;
char buf[buf_size];
tm now_tm{};

#ifdef _MSC_VER
auto err = gmtime_s(&now_tm, &now);
if (!err) {
if (const auto err = gmtime_s(&now_tm, &now); !err) {
std::strftime(buf, sizeof(buf), "%FT%TZ", &now_tm);
return buf;
} else {
return "1970-01-01T00:00:00Z";
}


#else
gmtime_r(&now, &now_tm);
std::strftime(buf, sizeof(buf), "%FT%TZ", &now_tm);
return buf;
if (gmtime_r(&now, &now_tm)) {
std::strftime(buf, sizeof(buf), "%FT%TZ", &now_tm);
return buf;
}
#endif

return "1970-01-01T00:00:00Z";// fallback
}

}// namespace fmu4cpp
Expand Down

0 comments on commit 2cdeb8d

Please sign in to comment.