Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix issues with suffixes in oms::ComRef #968

Merged
merged 4 commits into from
Feb 25, 2021
Merged
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
39 changes: 31 additions & 8 deletions src/OMSimulatorLib/ComRef.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,12 @@ oms::ComRef& oms::ComRef::operator=(const oms::ComRef& copy)

oms::ComRef oms::ComRef::operator+(const oms::ComRef& rhs) const
{
return oms::ComRef(std::string(*this) + "." + std::string(rhs));
if (!this->hasSuffix())
return oms::ComRef(std::string(*this) + "." + std::string(rhs));

ComRef lhs(*this);
lhs.pop_suffix();
return oms::ComRef(std::string(lhs) + "." + std::string(rhs));
}

bool oms::ComRef::isValidIdent(const std::string& ident)
Expand All @@ -95,7 +100,7 @@ bool oms::ComRef::isValidIdent() const

bool oms::ComRef::isEmpty() const
{
return (cref[0] == '\0');
return cref[0] == '\0';
}

bool oms::ComRef::hasSuffix() const
Expand All @@ -109,24 +114,38 @@ bool oms::ComRef::hasSuffix() const

bool oms::ComRef::hasSuffix(const std::string& suffix) const
{
return getSuffix() == suffix;
return this->suffix() == suffix;
}

oms::ComRef oms::ComRef::popSuffix() const
std::string oms::ComRef::pop_suffix()
{
std::string suffix = this->suffix();

for (int i=0; cref[i]; ++i)
{
if (cref[i] == ':')
{
cref[i] = '\0';
oms::ComRef newCref(cref);
oms::ComRef front(cref);
cref[i] = ':';
return newCref;
*this = front;
return suffix;
}
}

return *this;
return suffix;
}

bool oms::ComRef::pop_suffix(const std::string& suffix)
{
if (!this->hasSuffix(suffix))
return false;

this->pop_suffix();
return true;
}

std::string oms::ComRef::getSuffix() const
std::string oms::ComRef::suffix() const
{
for (int i=0; cref[i]; ++i)
if (cref[i] == ':')
Expand Down Expand Up @@ -157,6 +176,8 @@ oms::ComRef oms::ComRef::front() const
cref[i] = '.';
return front;
}
else if (cref[i] == ':')
break;
}

return *this;
Expand All @@ -174,6 +195,8 @@ oms::ComRef oms::ComRef::pop_front()
*this = oms::ComRef(cref + i + 1);
return front;
}
else if (cref[i] == ':')
break;
}

oms::ComRef front(cref);
Expand Down
19 changes: 13 additions & 6 deletions src/OMSimulatorLib/ComRef.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ namespace oms
{
/**
* \brief ComRef - component reference
*
* A component reference is a qualified name of a component. It uses
* '.' as component separator. It may also contain a ':' followed by
* a suffix string which is used to define attributes or filenames.
*/
class ComRef
{
Expand All @@ -51,20 +55,23 @@ namespace oms
// methods to copy the component reference
ComRef(const ComRef& copy);
ComRef& operator=(const ComRef& copy);
ComRef operator+(const ComRef& rhs) const;
ComRef operator+(const ComRef& rhs) const; ///< return ComRef(lhs + rhs) - Obs! lhs will lose its suffix

static bool isValidIdent(const std::string& ident);
bool isValidIdent() const;

bool isEmpty() const;
bool isRootOf(ComRef child) const;

ComRef front() const;
ComRef pop_front();
ComRef front() const; ///< returns the first part of the ComRef (including suffix if its the only part)
ComRef pop_front(); ///< returns the first part of the ComRef and removed it from the current object

std::string suffix() const; ///< returns the suffix as string
std::string pop_suffix(); ///< returns the suffix as string and removes it from the current object
bool pop_suffix(const std::string& suffix);

bool hasSuffix() const; ///< returns true if the cref has a suffix, i.e. ":"
bool hasSuffix() const; ///< returns true if the cref has a suffix, i.e. contains ":"
bool hasSuffix(const std::string& suffix) const; ///< returns true if the cref has a suffix that matches the argument
ComRef popSuffix() const; ///< returns a copy of a ComRef without its suffix
std::string getSuffix() const; ///< returns the suffix as string

const char* c_str() const {return cref;}
size_t size() {return strlen(cref);}
Expand Down
3 changes: 1 addition & 2 deletions src/OMSimulatorLib/Values.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,7 @@ oms_status_enu_t oms::Values::setBoolean(const ComRef& cref, bool value)
oms_status_enu_t oms::Values::deleteStartValue(const ComRef& cref)
{
oms::ComRef signal(cref);
if (signal.hasSuffix("start"))
signal = cref.popSuffix();
signal.pop_suffix("start");

// reals
auto realValue = realStartValues.find(signal);
Expand Down