diff --git a/src/simplnx/Common/Result.hpp b/src/simplnx/Common/Result.hpp index 52c3976c5f..cfac4c6f36 100644 --- a/src/simplnx/Common/Result.hpp +++ b/src/simplnx/Common/Result.hpp @@ -72,7 +72,7 @@ using ResultBaseT = std::conditional_t, detail::ResultVo /** * @brief Result is meant for reporting errors/warnings from a function with an optional contained type. - * Functions similiarly to std::optional. Warnings are always accessible, and either the contained type or errors is accessible at a time. + * Functions similarly to std::optional. Warnings are always accessible, and either the contained type or errors is accessible at a time. * @tparam T Contained type. May be void. */ template diff --git a/src/simplnx/DataStructure/AttributeMatrix.cpp b/src/simplnx/DataStructure/AttributeMatrix.cpp index faf982a733..43e995fa4d 100644 --- a/src/simplnx/DataStructure/AttributeMatrix.cpp +++ b/src/simplnx/DataStructure/AttributeMatrix.cpp @@ -78,18 +78,19 @@ std::string AttributeMatrix::getTypeName() const return k_TypeName; } -bool AttributeMatrix::canInsert(const DataObject* obj) const +Result AttributeMatrix::canInsert(const DataObject* obj) const { - if(!BaseGroup::canInsert(obj)) + auto result = BaseGroup::canInsert(obj); + if(result.invalid()) { - return false; + return result; } const auto* arrayObjectPtr = dynamic_cast(obj); if(arrayObjectPtr == nullptr) { - return false; + return MakeErrorResult(-1673, "BaseGroup::canInsert() Error: DataObject being inserted is null"); } const IArray::ShapeType arrayTupleShape = arrayObjectPtr->getTupleShape(); @@ -98,9 +99,9 @@ bool AttributeMatrix::canInsert(const DataObject* obj) const const usize incomingTupleCount = std::accumulate(arrayTupleShape.cbegin(), arrayTupleShape.cend(), static_cast(1), std::multiplies<>()); if(totalTuples != incomingTupleCount) { - std::cout << "AttributeMatrix: CanInsert() Failed with object " << obj->getName() << ". totalTuples=" << totalTuples << " incomingTupleCount=" << incomingTupleCount << "\n"; + return MakeErrorResult(-1674, fmt::format("AttributeMatrix: CanInsert() Failed with object {}. totalTuples={} incomingTupleCount={}", obj->getName(), totalTuples, incomingTupleCount)); } - return (totalTuples == incomingTupleCount); + return {true}; } const AttributeMatrix::ShapeType& AttributeMatrix::getShape() const diff --git a/src/simplnx/DataStructure/AttributeMatrix.hpp b/src/simplnx/DataStructure/AttributeMatrix.hpp index 21c0b80f4b..65801370eb 100644 --- a/src/simplnx/DataStructure/AttributeMatrix.hpp +++ b/src/simplnx/DataStructure/AttributeMatrix.hpp @@ -171,7 +171,7 @@ class SIMPLNX_EXPORT AttributeMatrix : public BaseGroup * @param obj * @return bool */ - bool canInsert(const DataObject* obj) const override; + Result canInsert(const DataObject* obj) const override; private: ShapeType m_TupleShape; diff --git a/src/simplnx/DataStructure/BaseGroup.cpp b/src/simplnx/DataStructure/BaseGroup.cpp index 7da37c91f1..5bfa42f35e 100644 --- a/src/simplnx/DataStructure/BaseGroup.cpp +++ b/src/simplnx/DataStructure/BaseGroup.cpp @@ -1,4 +1,5 @@ #include "BaseGroup.hpp" +#include #include "simplnx/DataStructure/DataPath.hpp" #include "simplnx/Utilities/StringUtilities.hpp" @@ -99,21 +100,22 @@ const DataObject& BaseGroup::at(const std::string& name) const return m_DataMap.at(name); } -bool BaseGroup::canInsert(const DataObject* obj) const +Result BaseGroup::canInsert(const DataObject* obj) const { if(obj == nullptr) { - return false; + return MakeErrorResult(-1663, "BaseGroup::canInsert() Error: DataObject being inserted is null"); } if(contains(obj) || contains(obj->getName())) { - return false; + return MakeErrorResult(-1664, fmt::format("BaseGroup::canInsert() Error: DataObject with name='{}' and type='{}' already exists in the DataMap", obj->getName(), obj->getTypeName())); } if(const auto* objGroup = dynamic_cast(obj); objGroup != nullptr && objGroup->isParentOf(this)) { - return false; + return MakeErrorResult(-1665, fmt::format("BaseGroup::canInsert() Error: DataObject with name='{}' and type='{}' is a parent of the current DataObject. A circular reference would occur.", + obj->getName(), obj->getTypeName())); } - return true; + return {true}; } void BaseGroup::setDataStructure(DataStructure* dataStructure) @@ -138,19 +140,21 @@ bool BaseGroup::isParentOf(const DataObject* dataObj) const return std::find_if(origDataPaths.begin(), origDataPaths.end(), [dataObj](const DataPath& path) { return dataObj->hasParent(path); }) != origDataPaths.end(); } -bool BaseGroup::insert(const std::weak_ptr& obj) +Result BaseGroup::insert(const std::weak_ptr& obj) { auto ptr = obj.lock(); - if(!canInsert(ptr.get())) + auto result = canInsert(ptr.get()); + if(result.invalid()) { - return false; + return result; } if(m_DataMap.insert(ptr)) { ptr->addParent(this); - return true; + return {true}; } - return false; + return MakeErrorResult( + -1666, fmt::format("BaseGroup::insert() Error: DataObject with name='{}' and type='{}' could not be inserted into the DataMap.", obj.lock()->getName(), obj.lock()->getTypeName())); } bool BaseGroup::remove(DataObject* obj) @@ -255,4 +259,4 @@ void BaseGroup::checkUpdatedIdsImpl(const std::vector> std::vector BaseGroup::GetChildrenIds() { return m_DataMap.getKeys(); -} \ No newline at end of file +} diff --git a/src/simplnx/DataStructure/BaseGroup.hpp b/src/simplnx/DataStructure/BaseGroup.hpp index efd24f5f6d..f36ac5bb7c 100644 --- a/src/simplnx/DataStructure/BaseGroup.hpp +++ b/src/simplnx/DataStructure/BaseGroup.hpp @@ -1,5 +1,6 @@ #pragma once +#include "simplnx/Common/Result.hpp" #include "simplnx/DataStructure/DataMap.hpp" #include "simplnx/DataStructure/DataObject.hpp" #include "simplnx/simplnx_export.hpp" @@ -251,7 +252,7 @@ class SIMPLNX_EXPORT BaseGroup : public DataObject * @param obj * @return bool */ - bool insert(const std::weak_ptr& obj); + Result insert(const std::weak_ptr& obj); /** * Attempts to remove the specified DataObject from the container. Returns @@ -373,7 +374,7 @@ class SIMPLNX_EXPORT BaseGroup : public DataObject * @param obj * @return bool */ - virtual bool canInsert(const DataObject* obj) const; + virtual Result canInsert(const DataObject* obj) const; /** * @brief Sets a new DataStructure for the BaseGroup. Updates the DataMap diff --git a/src/simplnx/DataStructure/DataGroup.cpp b/src/simplnx/DataStructure/DataGroup.cpp index 64a5d50fa4..415c10168e 100644 --- a/src/simplnx/DataStructure/DataGroup.cpp +++ b/src/simplnx/DataStructure/DataGroup.cpp @@ -79,7 +79,7 @@ std::string DataGroup::getTypeName() const return k_TypeName; } -bool DataGroup::canInsert(const DataObject* obj) const +Result DataGroup::canInsert(const DataObject* obj) const { return BaseGroup::canInsert(obj); } diff --git a/src/simplnx/DataStructure/DataGroup.hpp b/src/simplnx/DataStructure/DataGroup.hpp index 8c144097ab..c1f0bb1374 100644 --- a/src/simplnx/DataStructure/DataGroup.hpp +++ b/src/simplnx/DataStructure/DataGroup.hpp @@ -126,6 +126,6 @@ class SIMPLNX_EXPORT DataGroup : public BaseGroup * @param obj * @return bool */ - bool canInsert(const DataObject* obj) const override; + Result canInsert(const DataObject* obj) const override; }; } // namespace nx::core diff --git a/src/simplnx/DataStructure/Montage/AbstractMontage.cpp b/src/simplnx/DataStructure/Montage/AbstractMontage.cpp index 9959eecec5..c134296d26 100644 --- a/src/simplnx/DataStructure/Montage/AbstractMontage.cpp +++ b/src/simplnx/DataStructure/Montage/AbstractMontage.cpp @@ -102,11 +102,11 @@ const AbstractMontage::CollectionType& AbstractMontage::getCollection() const return m_Collection; } -bool AbstractMontage::canInsert(const DataObject* obj) const +Result AbstractMontage::canInsert(const DataObject* obj) const { if(!dynamic_cast(obj)) { - return false; + return MakeErrorResult(-1676, fmt::format("AbstractMontage::canInsert() Error: DataObject with name='{}' and type='{}' is not subclass of IGeometry", obj->getName(), obj->getTypeName())); } return BaseGroup::canInsert(obj); } diff --git a/src/simplnx/DataStructure/Montage/AbstractMontage.hpp b/src/simplnx/DataStructure/Montage/AbstractMontage.hpp index 6f52cbe5e8..90fb6d4d70 100644 --- a/src/simplnx/DataStructure/Montage/AbstractMontage.hpp +++ b/src/simplnx/DataStructure/Montage/AbstractMontage.hpp @@ -162,7 +162,7 @@ class SIMPLNX_EXPORT AbstractMontage : public BaseGroup * @param obj * @return bool */ - bool canInsert(const DataObject* obj) const override; + Result canInsert(const DataObject* obj) const override; /** * @brief Returns a reference of the collection for use in derived classes.