From 23ddc11676a64ec5211f1c3812eebf60f7b5450e Mon Sep 17 00:00:00 2001 From: eidekrist Date: Tue, 23 Jun 2020 14:37:54 +0200 Subject: [PATCH 1/4] #602 Replace get_simulator() with get_model_description() --- include/cosim/execution.hpp | 4 ++-- src/cosim/execution.cpp | 9 ++++----- 2 files changed, 6 insertions(+), 7 deletions(-) diff --git a/include/cosim/execution.hpp b/include/cosim/execution.hpp index 5af4b5ff7..d9443170b 100644 --- a/include/cosim/execution.hpp +++ b/include/cosim/execution.hpp @@ -297,8 +297,8 @@ class execution /// Returns the current real time factor target double get_real_time_factor_target() const; - /// Returns the simulator with the given index - std::shared_ptr get_simulator(simulator_index index) const; + /// Returns the model description for a simulator with the given index + const model_description& get_model_description(simulator_index index) const; /// Returns a map of currently modified variables std::vector get_modified_variables() const; diff --git a/src/cosim/execution.cpp b/src/cosim/execution.cpp index 6bebcf0df..078d87802 100644 --- a/src/cosim/execution.cpp +++ b/src/cosim/execution.cpp @@ -192,9 +192,9 @@ class execution::impl return timer_.get_real_time_factor_target(); } - std::shared_ptr get_simulator(simulator_index index) const + const model_description& get_model_description(simulator_index index) const { - return simulators_.at(index); + return simulators_.at(index)->model_description(); } std::vector get_modified_variables() const @@ -444,10 +444,9 @@ double execution::get_real_time_factor_target() const return pimpl_->get_real_time_factor_target(); } -std::shared_ptr execution::get_simulator( - simulator_index index) const +const model_description& execution::get_model_description(simulator_index index) const { - return pimpl_->get_simulator(index); + return pimpl_->get_model_description(index); } std::vector execution::get_modified_variables() const From 1ce83fd065444a8307f3153128522f8a39243ae7 Mon Sep 17 00:00:00 2001 From: eidekrist Date: Tue, 23 Jun 2020 14:38:55 +0200 Subject: [PATCH 2/4] Const ref observable.name() and observable.model_description() --- include/cosim/observer/observer.hpp | 4 ++-- src/cosim/observer/file_observer.cpp | 11 +++++++---- src/cosim/slave_simulator.cpp | 8 ++++---- src/cosim/slave_simulator.hpp | 4 ++-- 4 files changed, 15 insertions(+), 12 deletions(-) diff --git a/include/cosim/observer/observer.hpp b/include/cosim/observer/observer.hpp index 3ca9179ae..3388580e9 100644 --- a/include/cosim/observer/observer.hpp +++ b/include/cosim/observer/observer.hpp @@ -26,10 +26,10 @@ class observable { public: /// Returns the entity's name. - virtual std::string name() const = 0; + virtual const std::string& name() const = 0; /// Returns a description of the entity. - virtual cosim::model_description model_description() const = 0; + virtual const cosim::model_description& model_description() const = 0; /** * Exposes a variable for retrieval with `get_xxx()`. diff --git a/src/cosim/observer/file_observer.cpp b/src/cosim/observer/file_observer.cpp index 2f21e940b..d3d481db9 100644 --- a/src/cosim/observer/file_observer.cpp +++ b/src/cosim/observer/file_observer.cpp @@ -164,7 +164,8 @@ class file_observer::slave_value_writer void initialize_default() { if (!timeStampedFileNames_) { - const auto filePath = logDir_ / observable_->name().append(".csv"); + auto name = observable_->name(); + const auto filePath = logDir_ / name.append(".csv"); clear_file_contents_if_exists(filePath, fsw_); } @@ -179,7 +180,8 @@ class file_observer::slave_value_writer void initialize_config(const std::vector& variables) { if (!timeStampedFileNames_) { - const auto filePath = logDir_ / observable_->name().append(".csv"); + auto name = observable_->name(); + const auto filePath = logDir_ / name.append(".csv"); clear_file_contents_if_exists(filePath, fsw_); } @@ -191,11 +193,12 @@ class file_observer::slave_value_writer void create_log_file() { std::string filename; + auto name = observable_->name(); if (!timeStampedFileNames_) { - filename = observable_->name().append(".csv"); + filename = name.append(".csv"); } else { auto time_str = format_time(boost::posix_time::microsec_clock::local_time()); - filename = observable_->name().append("_").append(time_str).append(".csv"); + filename = name.append("_").append(time_str).append(".csv"); } const auto filePath = logDir_ / filename; diff --git a/src/cosim/slave_simulator.cpp b/src/cosim/slave_simulator.cpp index bb2d4fd5d..b55468fb6 100644 --- a/src/cosim/slave_simulator.cpp +++ b/src/cosim/slave_simulator.cpp @@ -239,12 +239,12 @@ class slave_simulator::impl impl& operator=(impl&&) noexcept = delete; - std::string name() const + const std::string& name() const { return name_; } - cosim::model_description model_description() const + const cosim::model_description& model_description() const { return modelDescription_; } @@ -561,13 +561,13 @@ slave_simulator::slave_simulator(slave_simulator&&) noexcept = default; slave_simulator& slave_simulator::operator=(slave_simulator&&) noexcept = default; -std::string slave_simulator::name() const +const std::string& slave_simulator::name() const { return pimpl_->name(); } -cosim::model_description slave_simulator::model_description() const +const cosim::model_description& slave_simulator::model_description() const { return pimpl_->model_description(); } diff --git a/src/cosim/slave_simulator.hpp b/src/cosim/slave_simulator.hpp index 3a095f74a..7d16e9333 100644 --- a/src/cosim/slave_simulator.hpp +++ b/src/cosim/slave_simulator.hpp @@ -35,8 +35,8 @@ class slave_simulator : public simulator slave_simulator& operator=(slave_simulator&&) noexcept; // `observable` methods - std::string name() const override; - cosim::model_description model_description() const override; + const std::string& name() const override; + const cosim::model_description& model_description() const override; void expose_for_getting(variable_type type, value_reference ref) override; double get_real(value_reference reference) const override; From e6527527a147eed0a906fe07553cef9abb458a6d Mon Sep 17 00:00:00 2001 From: eidekrist Date: Wed, 1 Jul 2020 10:03:38 +0200 Subject: [PATCH 3/4] Revert "Const ref observable.name() and observable.model_description()" This reverts commit 1ce83fd0 --- include/cosim/observer/observer.hpp | 4 ++-- src/cosim/observer/file_observer.cpp | 11 ++++------- src/cosim/slave_simulator.cpp | 8 ++++---- src/cosim/slave_simulator.hpp | 4 ++-- 4 files changed, 12 insertions(+), 15 deletions(-) diff --git a/include/cosim/observer/observer.hpp b/include/cosim/observer/observer.hpp index 3388580e9..3ca9179ae 100644 --- a/include/cosim/observer/observer.hpp +++ b/include/cosim/observer/observer.hpp @@ -26,10 +26,10 @@ class observable { public: /// Returns the entity's name. - virtual const std::string& name() const = 0; + virtual std::string name() const = 0; /// Returns a description of the entity. - virtual const cosim::model_description& model_description() const = 0; + virtual cosim::model_description model_description() const = 0; /** * Exposes a variable for retrieval with `get_xxx()`. diff --git a/src/cosim/observer/file_observer.cpp b/src/cosim/observer/file_observer.cpp index d3d481db9..2f21e940b 100644 --- a/src/cosim/observer/file_observer.cpp +++ b/src/cosim/observer/file_observer.cpp @@ -164,8 +164,7 @@ class file_observer::slave_value_writer void initialize_default() { if (!timeStampedFileNames_) { - auto name = observable_->name(); - const auto filePath = logDir_ / name.append(".csv"); + const auto filePath = logDir_ / observable_->name().append(".csv"); clear_file_contents_if_exists(filePath, fsw_); } @@ -180,8 +179,7 @@ class file_observer::slave_value_writer void initialize_config(const std::vector& variables) { if (!timeStampedFileNames_) { - auto name = observable_->name(); - const auto filePath = logDir_ / name.append(".csv"); + const auto filePath = logDir_ / observable_->name().append(".csv"); clear_file_contents_if_exists(filePath, fsw_); } @@ -193,12 +191,11 @@ class file_observer::slave_value_writer void create_log_file() { std::string filename; - auto name = observable_->name(); if (!timeStampedFileNames_) { - filename = name.append(".csv"); + filename = observable_->name().append(".csv"); } else { auto time_str = format_time(boost::posix_time::microsec_clock::local_time()); - filename = name.append("_").append(time_str).append(".csv"); + filename = observable_->name().append("_").append(time_str).append(".csv"); } const auto filePath = logDir_ / filename; diff --git a/src/cosim/slave_simulator.cpp b/src/cosim/slave_simulator.cpp index b55468fb6..bb2d4fd5d 100644 --- a/src/cosim/slave_simulator.cpp +++ b/src/cosim/slave_simulator.cpp @@ -239,12 +239,12 @@ class slave_simulator::impl impl& operator=(impl&&) noexcept = delete; - const std::string& name() const + std::string name() const { return name_; } - const cosim::model_description& model_description() const + cosim::model_description model_description() const { return modelDescription_; } @@ -561,13 +561,13 @@ slave_simulator::slave_simulator(slave_simulator&&) noexcept = default; slave_simulator& slave_simulator::operator=(slave_simulator&&) noexcept = default; -const std::string& slave_simulator::name() const +std::string slave_simulator::name() const { return pimpl_->name(); } -const cosim::model_description& slave_simulator::model_description() const +cosim::model_description slave_simulator::model_description() const { return pimpl_->model_description(); } diff --git a/src/cosim/slave_simulator.hpp b/src/cosim/slave_simulator.hpp index 7d16e9333..3a095f74a 100644 --- a/src/cosim/slave_simulator.hpp +++ b/src/cosim/slave_simulator.hpp @@ -35,8 +35,8 @@ class slave_simulator : public simulator slave_simulator& operator=(slave_simulator&&) noexcept; // `observable` methods - const std::string& name() const override; - const cosim::model_description& model_description() const override; + std::string name() const override; + cosim::model_description model_description() const override; void expose_for_getting(variable_type type, value_reference ref) override; double get_real(value_reference reference) const override; From 548bb5af54bc4cf1b023343b8ae321bf87f01ba0 Mon Sep 17 00:00:00 2001 From: eidekrist Date: Wed, 1 Jul 2020 10:07:13 +0200 Subject: [PATCH 4/4] Don't return const reference to model_description object. --- include/cosim/execution.hpp | 2 +- src/cosim/execution.cpp | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/include/cosim/execution.hpp b/include/cosim/execution.hpp index d9443170b..11c7a6956 100644 --- a/include/cosim/execution.hpp +++ b/include/cosim/execution.hpp @@ -298,7 +298,7 @@ class execution double get_real_time_factor_target() const; /// Returns the model description for a simulator with the given index - const model_description& get_model_description(simulator_index index) const; + model_description get_model_description(simulator_index index) const; /// Returns a map of currently modified variables std::vector get_modified_variables() const; diff --git a/src/cosim/execution.cpp b/src/cosim/execution.cpp index 078d87802..c14048534 100644 --- a/src/cosim/execution.cpp +++ b/src/cosim/execution.cpp @@ -192,7 +192,7 @@ class execution::impl return timer_.get_real_time_factor_target(); } - const model_description& get_model_description(simulator_index index) const + model_description get_model_description(simulator_index index) const { return simulators_.at(index)->model_description(); } @@ -444,7 +444,7 @@ double execution::get_real_time_factor_target() const return pimpl_->get_real_time_factor_target(); } -const model_description& execution::get_model_description(simulator_index index) const +model_description execution::get_model_description(simulator_index index) const { return pimpl_->get_model_description(index); }