Skip to content

Commit

Permalink
Improve oms::ComRef to work with suffixes easily (#959)
Browse files Browse the repository at this point in the history
  • Loading branch information
lochel committed Feb 22, 2021
1 parent c86196c commit 1dad749
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 30 deletions.
49 changes: 24 additions & 25 deletions src/OMSimulatorLib/ComRef.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -98,44 +98,43 @@ bool oms::ComRef::isEmpty() const
return (cref[0] == '\0');
}

bool oms::ComRef::hasSuffixStart() const
bool oms::ComRef::hasSuffix() const
{
int i=0;
while (cref[i])
++i;

if (i < 7)
return false;

if (cref[i-6] == ':' &&
cref[i-5] == 's' &&
cref[i-4] == 't' &&
cref[i-3] == 'a' &&
cref[i-2] == 'r' &&
cref[i-1] == 't')
return true;
for (int i=0; cref[i]; ++i)
if (cref[i] == ':')
return true;

return false;
}

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

oms::ComRef oms::ComRef::popSuffix() const
{
int index=0;
for (int i=0; cref[i]; ++i)
if (cref[i] == ':')
index = i;

if (index > 0)
{
cref[index] = '\0';
oms::ComRef front(cref);
cref[index] = ':';
return front;
}
{
cref[i] = '\0';
oms::ComRef newCref(cref);
cref[i] = ':';
return newCref;
}

return *this;
}

std::string oms::ComRef::getSuffix() const
{
for (int i=0; cref[i]; ++i)
if (cref[i] == ':')
return std::string(cref+i+1);

return std::string();
}

bool oms::ComRef::isRootOf(ComRef child) const
{
ComRef root(*this);
Expand Down
6 changes: 4 additions & 2 deletions src/OMSimulatorLib/ComRef.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,10 @@ namespace oms
ComRef front() const;
ComRef pop_front();

bool hasSuffixStart() const;
ComRef popSuffix() const;
bool hasSuffix() const; ///< returns true if the cref has a suffix, i.e. ":"
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
4 changes: 2 additions & 2 deletions src/OMSimulatorLib/System.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1581,7 +1581,7 @@ oms_status_enu_t oms::System::delete_(const oms::ComRef& cref)
if (tail.isEmpty())
{
// check cref has ":start" suffix at the end to delete only start values
if (front.hasSuffixStart())
if (front.hasSuffix("start"))
{
if (oms_status_ok != values.deleteStartValue(getValidCref(front)))
return logWarning("failed to delete start value \"" + std::string(getFullCref()+front) + "\"" + " because the identifier couldn't be resolved to any system signal");
Expand Down Expand Up @@ -1663,7 +1663,7 @@ oms_status_enu_t oms::System::delete_(const oms::ComRef& cref)
if (component != components.end())
{
// check cref has ":start" suffix at the end to delete only start values
if (tail.hasSuffixStart())
if (tail.hasSuffix("start"))
{
// add prefix to cref start values if ssv file is used (e.g) k1:start -> addP.k1:start
if(!Flags::ExportParametersInline())
Expand Down
2 changes: 1 addition & 1 deletion src/OMSimulatorLib/Values.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,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.hasSuffixStart())
if (signal.hasSuffix("start"))
signal = cref.popSuffix();

// reals
Expand Down

0 comments on commit 1dad749

Please sign in to comment.