Skip to content

Commit

Permalink
Starting the API changes. This will spread to the entire code base...
Browse files Browse the repository at this point in the history
Signed-off-by: Michael Jackson <[email protected]>
  • Loading branch information
imikejackson committed Jun 21, 2024
1 parent 5e90d99 commit 643a8c1
Show file tree
Hide file tree
Showing 9 changed files with 32 additions and 26 deletions.
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
13 changes: 7 additions & 6 deletions src/simplnx/DataStructure/AttributeMatrix.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,18 +78,19 @@ std::string AttributeMatrix::getTypeName() const
return k_TypeName;
}

bool AttributeMatrix::canInsert(const DataObject* obj) const
Result<bool> 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<bool>(-1673, "BaseGroup::canInsert() Error: DataObject being inserted is null");
}

const IArray::ShapeType arrayTupleShape = arrayObjectPtr->getTupleShape();
Expand All @@ -98,9 +99,9 @@ bool AttributeMatrix::canInsert(const DataObject* obj) const
const usize incomingTupleCount = std::accumulate(arrayTupleShape.cbegin(), arrayTupleShape.cend(), static_cast<usize>(1), std::multiplies<>());
if(totalTuples != incomingTupleCount)
{
std::cout << "AttributeMatrix: CanInsert() Failed with object " << obj->getName() << ". totalTuples=" << totalTuples << " incomingTupleCount=" << incomingTupleCount << "\n";
return MakeErrorResult<bool>(-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
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<bool> 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<bool> BaseGroup::canInsert(const DataObject* obj) const
{
if(obj == nullptr)
{
return false;
return MakeErrorResult<bool>(-1663, "BaseGroup::canInsert() Error: DataObject being inserted is null");
}
if(contains(obj) || contains(obj->getName()))
{
return false;
return MakeErrorResult<bool>(-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<bool>(-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)
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<bool> 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 {true};
}
return false;
return MakeErrorResult<bool>(
-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<bool> 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<bool> 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<bool> 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<bool> canInsert(const DataObject* obj) const override;
};
} // namespace nx::core
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<bool> AbstractMontage::canInsert(const DataObject* obj) const
{
if(!dynamic_cast<const IGeometry*>(obj))
{
return false;
return MakeErrorResult<bool>(-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<bool> canInsert(const DataObject* obj) const override;

/**
* @brief Returns a reference of the collection for use in derived classes.
Expand Down

0 comments on commit 643a8c1

Please sign in to comment.