From ac1b7fa3d4fbf08c74e7bffee5f720c07fec32cf Mon Sep 17 00:00:00 2001 From: Jürgen Knödlseder Date: Sat, 16 Mar 2013 13:51:04 +0100 Subject: [PATCH] Methods set(), append() and insert() now return a pointer to the appended model. --- include/GModels.hpp | 10 +++++----- pyext/GModels.i | 10 +++++----- src/model/GModels.cpp | 176 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++--------------------------------------------------------------------------------- 3 files changed, 105 insertions(+), 91 deletions(-) diff --git a/include/GModels.hpp b/include/GModels.hpp index f05d1a4..f4bba3a 100644 --- a/include/GModels.hpp +++ b/include/GModels.hpp @@ -82,11 +82,11 @@ public: GModels* clone(void) const; int size(void) const { return m_models.size(); } bool isempty(void) const { return m_models.empty(); } - void set(const int& index, const GModel& model); - void set(const std::string& name, const GModel& model); - void append(const GModel& model); - void insert(const int& index, const GModel& model); - void insert(const std::string& name, const GModel& model); + GModel* set(const int& index, const GModel& model); + GModel* set(const std::string& name, const GModel& model); + GModel* append(const GModel& model); + GModel* insert(const int& index, const GModel& model); + GModel* insert(const std::string& name, const GModel& model); void remove(const int& index); void remove(const std::string& name); void reserve(const int& num) { return m_models.reserve(num); } diff --git a/pyext/GModels.i b/pyext/GModels.i index 0ac30a0..24fb1f4 100644 --- a/pyext/GModels.i +++ b/pyext/GModels.i @@ -63,11 +63,11 @@ public: GModels* clone(void) const; int size(void) const; bool isempty(void) const; - void set(const int& index, const GModel& model); - void set(const std::string& name, const GModel& model); - void append(const GModel& model); - void insert(const int& index, const GModel& model); - void insert(const std::string& name, const GModel& model); + GModel* set(const int& index, const GModel& model); + GModel* set(const std::string& name, const GModel& model); + GModel* append(const GModel& model); + GModel* insert(const int& index, const GModel& model); + GModel* insert(const std::string& name, const GModel& model); void remove(const int& index); void remove(const std::string& name); void reserve(const int& num); diff --git a/src/model/GModels.cpp b/src/model/GModels.cpp index 0afad12..5e947a2 100644 --- a/src/model/GModels.cpp +++ b/src/model/GModels.cpp @@ -299,23 +299,98 @@ GModels* GModels::clone(void) const /***********************************************************************//** + * @brief Set model in container + * + * @param[in] index Model index [0,...,size()-1]. + * @param[in] model Model. + * @return Pointer to deep copy of model. + * + * @exception GException::out_of_range + * Model index is out of range. + * + * Set model in the container. A deep copy of the model will be made. + ***************************************************************************/ +GModel* GModels::set(const int& index, const GModel& model) +{ + // Compile option: raise exception if index is out of range + #if defined(G_RANGE_CHECK) + if (index < 0 || index >= size()) { + throw GException::out_of_range(G_SET1, index, 0, size()-1); + } + #endif + + // Delete any existing model + if (m_models[index] != NULL) delete m_models[index]; + + // Assign new model by cloning + m_models[index] = model.clone(); + + // Set parameter pointers + set_pointers(); + + // Return pointer to model + return m_models[index]; +} + + +/***********************************************************************//** + * @brief Set model in container + * + * @param[in] name Model name. + * @param[in] model Model pointer. + * @return Pointer to deep copy of model. + * + * @exception GException::model_not_found + * Model with specified name not found in container. + * + * Set model in the container. A deep copy of the model will be made. + ***************************************************************************/ +GModel* GModels::set(const std::string& name, const GModel& model) +{ + // Get parameter index + int index = get_index(name); + + // Throw exception if parameter name was not found + if (index >= size()) { + throw GException::model_not_found(G_SET2, name); + } + + // Delete any existing model + if (m_models[index] != NULL) delete m_models[index]; + + // Assign new model by cloning + m_models[index] = model.clone(); + + // Set parameter pointers + set_pointers(); + + // Return pointer to model + return m_models[index]; +} + + +/***********************************************************************//** * @brief Append model to container * * @param[in] model Model. + * @return Pointer to deep copy of model. * * Appends model to the container by making a deep copy of the model and * storing its pointer. ***************************************************************************/ -void GModels::append(const GModel& model) +GModel* GModels::append(const GModel& model) { + // Create deep copy of model + GModel* ptr = model.clone(); + // Append deep copy of model - m_models.push_back(model.clone()); + m_models.push_back(ptr); // Set parameter pointers set_pointers(); - // Return - return; + // Return pointer to model + return ptr; } @@ -324,6 +399,7 @@ void GModels::append(const GModel& model) * * @param[in] index Model index [0,...,size()-1]. * @param[in] model Model. + * @return Pointer to deep copy of model. * * @exception GException::out_of_range * Model index is out of range. @@ -331,7 +407,7 @@ void GModels::append(const GModel& model) * Inserts a @p model into the container before the model with the specified * @p index. ***************************************************************************/ -void GModels::insert(const int& index, const GModel& model) +GModel* GModels::insert(const int& index, const GModel& model) { // Compile option: raise exception if index is out of range #if defined(G_RANGE_CHECK) @@ -347,14 +423,17 @@ void GModels::insert(const int& index, const GModel& model) } #endif + // Create deep copy of model + GModel* ptr = model.clone(); + // Inserts deep copy of model - m_models.insert(m_models.begin()+index, model.clone()); + m_models.insert(m_models.begin()+index, ptr); // Set parameter pointers set_pointers(); - // Return - return; + // Return pointer to model + return ptr; } @@ -363,6 +442,7 @@ void GModels::insert(const int& index, const GModel& model) * * @param[in] name Model name. * @param[in] model Model. + * @return Pointer to deep copy of model. * * @exception GException::model_not_found * Model with specified name not found in container. @@ -370,7 +450,7 @@ void GModels::insert(const int& index, const GModel& model) * Inserts a @p model into the container before the model with the specified * @p name. ***************************************************************************/ -void GModels::insert(const std::string& name, const GModel& model) +GModel* GModels::insert(const std::string& name, const GModel& model) { // Get parameter index int index = get_index(name); @@ -380,14 +460,17 @@ void GModels::insert(const std::string& name, const GModel& model) throw GException::model_not_found(G_INSERT2, name); } + // Create deep copy of model + GModel* ptr = model.clone(); + // Inserts deep copy of model - m_models.insert(m_models.begin()+index, model.clone()); + m_models.insert(m_models.begin()+index, ptr); // Set parameter pointers set_pointers(); - // Return - return; + // Return pointer to model + return ptr; } @@ -488,75 +571,6 @@ void GModels::extend(const GModels& models) /***********************************************************************//** - * @brief Set model in container - * - * @param[in] index Model index [0,...,size()-1]. - * @param[in] model Model. - * - * @exception GException::out_of_range - * Model index is out of range. - * - * Set model in the container. A deep copy of the model will be made. - ***************************************************************************/ -void GModels::set(const int& index, const GModel& model) -{ - // Compile option: raise exception if index is out of range - #if defined(G_RANGE_CHECK) - if (index < 0 || index >= size()) { - throw GException::out_of_range(G_SET1, index, 0, size()-1); - } - #endif - - // Delete any existing model - if (m_models[index] != NULL) delete m_models[index]; - - // Assign new model by cloning - m_models[index] = model.clone(); - - // Set parameter pointers - set_pointers(); - - // Return - return; -} - - -/***********************************************************************//** - * @brief Set model in container - * - * @param[in] name Model name. - * @param[in] model Model pointer. - * - * @exception GException::model_not_found - * Model with specified name not found in container. - * - * Set model in the container. A deep copy of the model will be made. - ***************************************************************************/ -void GModels::set(const std::string& name, const GModel& model) -{ - // Get parameter index - int index = get_index(name); - - // Throw exception if parameter name was not found - if (index >= size()) { - throw GException::model_not_found(G_SET2, name); - } - - // Delete any existing model - if (m_models[index] != NULL) delete m_models[index]; - - // Assign new model by cloning - m_models[index] = model.clone(); - - // Set parameter pointers - set_pointers(); - - // Return - return; -} - - -/***********************************************************************//** * @brief Load models from XML file * * @param[in] filename XML filename. -- libgit2 0.21.4