Skip to content
Permalink
Browse files

Add the Component.takeVariableMethods() cellml#214

  • Loading branch information...
TedAhmadi committed Jan 30, 2019
1 parent a51fa1d commit 2af14fab630a9b0b6fda2c02eaa051ef3c492d57
Showing with 105 additions and 0 deletions.
  1. +29 −0 src/api/libcellml/component.h
  2. +27 −0 src/component.cpp
  3. +49 −0 tests/variable/variable.cpp
@@ -175,6 +175,35 @@ class LIBCELLML_EXPORT Component: public ComponentEntity, public ImportedEntity
*/
VariablePtr getVariable(const std::string &name) const;

/**
* @brief Take a variable at index.
*
* Remove the variable at the given index from this component and
* Returns a reference to a variable at the index @p index for this
* component. If the index is not valid a @c nullptr is returned, the valid
* range for the index is [0, \#variables).
*
* @param index The index of the variable to return.
*
* @return A reference to the variable at the given index on success, @c nullptr otherwise.
*/
VariablePtr takeVariable(size_t index);

/**
* @brief Take a variable with the given name @p name.
*
* Remove the variable with the given name from this component and
* Returns a reference to a variable with the name @p name for this
* component. If the name is not found a @c nullptr is returned.
*
* @overload
*
* @param name The name of the variable to return.
*
* @return A reference to the Variable with the given name on success, @c nullptr otherwise.
*/
VariablePtr takeVariable(const std::string &name);

/**
* @brief Get the number of variables in the component.
*
@@ -198,6 +198,33 @@ VariablePtr Component::getVariable(const std::string &name) const
return variable;
}

VariablePtr Component::takeVariable(size_t index)
{
VariablePtr variable = nullptr;
variable = getVariable(index);
removeVariable(index);
//if (index < mPimpl->mVariables.size()) {
//variable = mPimpl->mVariables.at(index);
// mPimpl->mVariables.erase(mPimpl->mVariables.begin() + index);
//}

return variable;
}

VariablePtr Component::takeVariable(const std::string &name)
{
VariablePtr variable = nullptr;
variable = getVariable(name);
removeVariable(name);
//auto result = mPimpl->findVariable(name);
//if (result != mPimpl->mVariables.end()) {
// variable = *result;
// mPimpl->mVariables.erase(result);
//}

return variable;
}

size_t Component::variableCount() const
{
return mPimpl->mVariables.size();
@@ -438,6 +438,55 @@ TEST(Variable, getVariableMethods) {
EXPECT_EQ(nullptr, static_cast<const libcellml::Component>(c).getVariable("doesntexist"));
}


TEST(Variable, takeVariableMethods) {
const std::string in = "valid_name";
libcellml::Component c;
c.setName(in);

libcellml::VariablePtr v1 = std::make_shared<libcellml::Variable>();
v1->setName("variable1");
c.addVariable(v1);
libcellml::VariablePtr v2 = std::make_shared<libcellml::Variable>();
v2->setName("variable2");
c.addVariable(v2);
libcellml::VariablePtr v3 = std::make_shared<libcellml::Variable>();
v3->setName("variable3");
c.addVariable(v3);
libcellml::VariablePtr v4 = std::make_shared<libcellml::Variable>();
v4->setName("variable4");
c.addVariable(v4);

// Take by index
libcellml::VariablePtr vMethod2 = c.takeVariable(0);
std::string a2 = vMethod2->getName();
EXPECT_EQ("variable1", a2);
libcellml::VariablePtr nMethod2 = c.getVariable(0);
std::string n2 = nMethod2->getName();
EXPECT_EQ("variable2", n2);
libcellml::VariablePtr vMethod3 = c.takeVariable(0);
std::string na2 = vMethod3->getName();
EXPECT_EQ("variable2", na2);
libcellml::VariablePtr nMethod3 = c.getVariable(0);
std::string nn2 = nMethod3->getName();
EXPECT_EQ("variable3", nn2);

// Take by string
libcellml::VariablePtr vMethod1 = c.takeVariable("variable3");
std::string a1 = vMethod1->getName();
EXPECT_EQ("variable3", a1);

// Get invalid index
EXPECT_EQ(nullptr, static_cast< libcellml::Component>(c).takeVariable(-3));
EXPECT_EQ(nullptr, c.takeVariable(7));

// Get non-existent variable by string
EXPECT_EQ(nullptr, c.takeVariable("notreal"));
EXPECT_EQ(nullptr, static_cast< libcellml::Component>(c).takeVariable("doesntexist"));
}



TEST(Variable, modelWithComponentWithVariableWithValidName) {
const std::string in = "valid_name";
const std::string e =

0 comments on commit 2af14fa

Please sign in to comment.
You can’t perform that action at this time.