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

[WIP] BUG: Rework API for DataMap and DeepCopy in DataObject. #864

Draft
wants to merge 4 commits into
base: develop
Choose a base branch
from
Draft
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
2 changes: 1 addition & 1 deletion src/simplnx/Common/Result.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ using ResultBaseT = std::conditional_t<std::is_same_v<T, void>, 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 <class T = void>
Expand Down
16 changes: 10 additions & 6 deletions src/simplnx/DataStructure/AttributeMatrix.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,26 +78,30 @@ 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<const IArray*>(obj);

if(arrayObjectPtr == nullptr)
{
return false;
return MakeErrorResult<>(-1673, "BaseGroup::canInsert() Error: DataObject being inserted is null");
}

const IArray::ShapeType arrayTupleShape = arrayObjectPtr->getTupleShape();

const usize totalTuples = std::accumulate(m_TupleShape.cbegin(), m_TupleShape.cend(), static_cast<usize>(1), std::multiplies<>());
const usize incomingTupleCount = std::accumulate(arrayTupleShape.cbegin(), arrayTupleShape.cend(), static_cast<usize>(1), std::multiplies<>());

return (totalTuples == incomingTupleCount);
if(totalTuples != incomingTupleCount)
{
return MakeErrorResult<>(-1674, fmt::format("AttributeMatrix: CanInsert() Failed with object {}. totalTuples={} incomingTupleCount={}", obj->getName(), totalTuples, incomingTupleCount));
}
return {};
}

const AttributeMatrix::ShapeType& AttributeMatrix::getShape() const
Expand Down
2 changes: 1 addition & 1 deletion src/simplnx/DataStructure/AttributeMatrix.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
26 changes: 15 additions & 11 deletions src/simplnx/DataStructure/BaseGroup.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "BaseGroup.hpp"
#include <simplnx/Common/Result.hpp>

#include "simplnx/DataStructure/DataPath.hpp"
#include "simplnx/Utilities/StringUtilities.hpp"
Expand Down Expand Up @@ -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<const BaseGroup*>(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 {};
}

void BaseGroup::setDataStructure(DataStructure* dataStructure)
Expand All @@ -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<DataObject>& obj)
Result<> BaseGroup::insert(const std::weak_ptr<DataObject>& 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 {};
}
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)
Expand Down Expand Up @@ -255,4 +259,4 @@ void BaseGroup::checkUpdatedIdsImpl(const std::vector<std::pair<IdType, IdType>>
std::vector<DataObject::IdType> BaseGroup::GetChildrenIds()
{
return m_DataMap.getKeys();
}
}
5 changes: 3 additions & 2 deletions src/simplnx/DataStructure/BaseGroup.hpp
Original file line number Diff line number Diff line change
@@ -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"
Expand Down Expand Up @@ -251,7 +252,7 @@ class SIMPLNX_EXPORT BaseGroup : public DataObject
* @param obj
* @return bool
*/
bool insert(const std::weak_ptr<DataObject>& obj);
Result<> insert(const std::weak_ptr<DataObject>& obj);

/**
* Attempts to remove the specified DataObject from the container. Returns
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/simplnx/DataStructure/DataGroup.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
2 changes: 1 addition & 1 deletion src/simplnx/DataStructure/DataGroup.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
5 changes: 5 additions & 0 deletions src/simplnx/DataStructure/DataMap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@ DataMap DataMap::deepCopy(const DataPath& parentCopyPath) const
{
const auto& dataObj = m_Map.at(key);
const auto copy = dataObj->deepCopy(parentCopyPath.createChildPath(dataObj->getName()));
if(copy == nullptr)
{
throw std::runtime_error(
fmt::format("{}({}): Function {}: A deep copy request for parent '{}' failed on child object '{}'", __FILE__, __LINE__, "DataMap::DeepCopy", parentCopyPath.toString(), dataObj->getName()));
}
dataMap.m_Map[key] = copy;
}
return dataMap;
Expand Down
6 changes: 3 additions & 3 deletions src/simplnx/DataStructure/DataStructure.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -567,7 +567,7 @@ bool DataStructure::finishAddingObject(const std::shared_ptr<DataObject>& dataOb
{
return false;
}
if(!parentContainer->insert(dataObject))
if(parentContainer->insert(dataObject).invalid())
{
return false;
}
Expand Down Expand Up @@ -658,7 +658,7 @@ bool DataStructure::insertIntoParent(const std::shared_ptr<DataObject>& dataObje
return false;
}

if(!parentGroup->insert(dataObject))
if(parentGroup->insert(dataObject).invalid())
{
return false;
}
Expand Down Expand Up @@ -692,7 +692,7 @@ bool DataStructure::setAdditionalParent(DataObject::IdType targetId, DataObject:
return false;
}

if(!newParent->insert(target))
if(newParent->insert(target).invalid())
{
return false;
}
Expand Down
4 changes: 2 additions & 2 deletions src/simplnx/DataStructure/Montage/AbstractMontage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<const IGeometry*>(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);
}
2 changes: 1 addition & 1 deletion src/simplnx/DataStructure/Montage/AbstractMontage.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
Loading