From 09b29deaf88545c6c64e0546761a282dbc153d98 Mon Sep 17 00:00:00 2001 From: Michael Jackson Date: Wed, 24 Apr 2024 11:48:18 -0400 Subject: [PATCH] API: DataPath::replaceName convenience method. - All codes should be updated to use this new API Signed-off-by: Michael Jackson --- src/simplnx/DataStructure/DataPath.cpp | 17 ++++++++++++++--- src/simplnx/DataStructure/DataPath.hpp | 19 +++++++++++++++++-- 2 files changed, 31 insertions(+), 5 deletions(-) diff --git a/src/simplnx/DataStructure/DataPath.cpp b/src/simplnx/DataStructure/DataPath.cpp index 8854e71176..9b6d56979c 100644 --- a/src/simplnx/DataStructure/DataPath.cpp +++ b/src/simplnx/DataStructure/DataPath.cpp @@ -93,21 +93,32 @@ DataPath DataPath::getParent() const } std::vector 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 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 newPath = m_Path; + newPath.back() = newName; + return {(std::move(newPath))}; } DataPath DataPath::replace(std::string_view symbol, std::string_view targetName) const { std::vector 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) diff --git a/src/simplnx/DataStructure/DataPath.hpp b/src/simplnx/DataStructure/DataPath.hpp index eb384d590d..cf2e460c1e 100644 --- a/src/simplnx/DataStructure/DataPath.hpp +++ b/src/simplnx/DataStructure/DataPath.hpp @@ -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 FromString(std::string_view string, char delimter = '/'); + static std::optional FromString(std::string_view string, char delimiter = '/'); /** * @brief Default constructor. @@ -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