Skip to content

Commit

Permalink
API: DataPath::replaceName convenience method.
Browse files Browse the repository at this point in the history
- All codes should be updated to use this new API

Signed-off-by: Michael Jackson <[email protected]>
  • Loading branch information
imikejackson committed Apr 24, 2024
1 parent 1d80265 commit 09b29de
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 5 deletions.
17 changes: 14 additions & 3 deletions src/simplnx/DataStructure/DataPath.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -93,21 +93,32 @@ DataPath DataPath::getParent() const
}

std::vector<std::string> parentPath(m_Path.cbegin(), m_Path.cend() - 1);
return DataPath(std::move(parentPath));
return {(std::move(parentPath))};
}

DataPath DataPath::createChildPath(std::string name) const
{
std::vector<std::string> path = m_Path;
path.push_back(std::move(name));
return DataPath(std::move(path));
return {(std::move(path))};
}

DataPath DataPath::replaceName(const std::string& newName) const
{
if(m_Path.empty())
{
return DataPath({newName});
}
std::vector<std::string> newPath = m_Path;
newPath.back() = newName;
return {(std::move(newPath))};
}

DataPath DataPath::replace(std::string_view symbol, std::string_view targetName) const
{
std::vector<std::string> newPath = m_Path;
std::replace(newPath.begin(), newPath.end(), symbol, targetName);
return DataPath(std::move(newPath));
return {(std::move(newPath))};
}

bool DataPath::attemptRename(const DataPath& oldPath, const DataPath& newPath)
Expand Down
19 changes: 17 additions & 2 deletions src/simplnx/DataStructure/DataPath.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@ class SIMPLNX_EXPORT DataPath
/**
* @brief Attempts to create a DataPath from the given string by splitting it using the given delimiter.
* @param string
* @param delimter
* @param delimiter
* @return
*/
static std::optional<DataPath> FromString(std::string_view string, char delimter = '/');
static std::optional<DataPath> FromString(std::string_view string, char delimiter = '/');

/**
* @brief Default constructor.
Expand Down Expand Up @@ -90,6 +90,21 @@ class SIMPLNX_EXPORT DataPath
*/
std::string getTargetName() const;

/**
* @Brief Returns a newly constructed DataPath where the only change is the replacement of the last
* element in the DataPath to the 'newName' value.
*
* @code
*
* DataPath foo({"A", "B", "C"}); // foo is now "A/B/C"
* DataPath bar = foo.replaceName("D"); // bar is now "A/B/D"
* @endcode
*
* @param newName The replacement value for the last element in the DataPath
* @return DataPath
*/
[[nodiscard]] DataPath replaceName(const std::string& newName) const;

/**
* @brief Returns the path as a vector of strings.
* @return std::vector<std::string>
Expand Down

0 comments on commit 09b29de

Please sign in to comment.