Skip to content
Open
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
8 changes: 7 additions & 1 deletion src/simplnx/DataStructure/BaseGroup.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "BaseGroup.hpp"

#include "simplnx/DataStructure/DataPath.hpp"
#include "simplnx/DataStructure/DataStructure.hpp"
#include "simplnx/Utilities/StringUtilities.hpp"

using namespace nx::core;
Expand All @@ -21,7 +22,7 @@ BaseGroup::BaseGroup(const BaseGroup& other)
{
}

BaseGroup::BaseGroup(BaseGroup&& other)
BaseGroup::BaseGroup(BaseGroup&& other) noexcept
: DataObject(std::move(other))
, m_DataMap(std::move(other.m_DataMap))
{
Expand Down Expand Up @@ -105,6 +106,11 @@ bool BaseGroup::canInsert(const DataObject* obj) const
{
return false;
}
// Do not allow adding a DataStructure to a BaseGroup
if(dynamic_cast<const DataStructure*>(obj) != nullptr)
{
return false;
}
if(contains(obj) || contains(obj->getName()))
{
return false;
Expand Down
4 changes: 2 additions & 2 deletions src/simplnx/DataStructure/BaseGroup.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ class SIMPLNX_EXPORT BaseGroup : public DataObject
* the provided BaseGroup.
* @param other
*/
BaseGroup(BaseGroup&& other);
BaseGroup(BaseGroup&& other) noexcept;

/**
* @brief Destroys the BaseGroup and removes it from the list of it's
Expand Down Expand Up @@ -278,7 +278,7 @@ class SIMPLNX_EXPORT BaseGroup : public DataObject
/**
* @brief Clears the group of all children.
*/
void clear();
virtual void clear();

/**
* @brief Returns an iterator to the beginning of the container.
Expand Down
2 changes: 1 addition & 1 deletion src/simplnx/DataStructure/DataObject.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ DataObject& DataObject::operator=(DataObject&& rhs) noexcept

DataObject::~DataObject() noexcept
{
if(m_DataStructure == nullptr)
if(m_DataStructure == nullptr || m_DataStructure == this)
{
return;
}
Expand Down
85 changes: 49 additions & 36 deletions src/simplnx/DataStructure/DataStructure.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,14 @@ const std::string k_Delimiter = "|--";
namespace nx::core
{
DataStructure::DataStructure()
: m_IsValid(true)
: BaseGroup(*this, k_TypeName)
, m_IsValid(true)
{
}

DataStructure::DataStructure(const DataStructure& dataStructure)
: m_DataObjects(dataStructure.m_DataObjects)
, m_RootGroup(dataStructure.m_RootGroup)
: BaseGroup(dataStructure)
, m_DataObjects(dataStructure.m_DataObjects)
, m_IsValid(dataStructure.m_IsValid)
, m_NextId(dataStructure.m_NextId)
{
Expand All @@ -54,16 +55,16 @@ DataStructure::DataStructure(const DataStructure& dataStructure)
}
// Updates all DataMaps with the corresponding m_DataObjects pointers.
// Updates all DataObjects with their new DataStructure
m_RootGroup.setDataStructure(this);
setDataStructure(this);
}

DataStructure::DataStructure(DataStructure&& dataStructure) noexcept
: m_DataObjects(std::move(dataStructure.m_DataObjects))
, m_RootGroup(std::move(dataStructure.m_RootGroup))
: BaseGroup(std::move(dataStructure))
, m_DataObjects(std::move(dataStructure.m_DataObjects))
, m_IsValid(dataStructure.m_IsValid)
, m_NextId(dataStructure.m_NextId)
{
m_RootGroup.setDataStructure(this);
getDataMap().setDataStructure(this);
}

DataStructure::~DataStructure()
Expand Down Expand Up @@ -98,7 +99,7 @@ size_t DataStructure::getSize() const

void DataStructure::clear()
{
auto topDataIds = m_RootGroup.getKeys();
auto topDataIds = getDataMap().getKeys();
for(auto dataId : topDataIds)
{
removeData(dataId);
Expand All @@ -125,7 +126,7 @@ LinkedPath DataStructure::getLinkedPath(const DataPath& path) const
try
{
std::vector<DataObject::IdType> pathIds;
const DataObject* data = m_RootGroup[path[0]];
const DataObject* data = getDataMap()[path[0]];
const BaseGroup* parent = dynamic_cast<const BaseGroup*>(data);
pathIds.push_back(data->getId());

Expand Down Expand Up @@ -163,7 +164,7 @@ Result<LinkedPath> DataStructure::makePath(const DataPath& path)
{
std::vector<DataObject::IdType> pathIds;
std::string name = path[0];
const DataObject* data = m_RootGroup[name];
const DataObject* data = getDataMap()[name];
if(data == nullptr)
{
data = nx::core::DataGroup::Create(*this, name);
Expand Down Expand Up @@ -272,7 +273,7 @@ DataObject* DataStructure::getData(const DataPath& path)
{
return nullptr;
}
DataObject* targetObject = m_RootGroup[path[0]];
DataObject* targetObject = getDataMap()[path[0]];
for(usize index = 1; index < path.getLength(); index++)
{
if(targetObject == nullptr)
Expand Down Expand Up @@ -351,7 +352,7 @@ const DataObject* DataStructure::getData(const DataPath& path) const
{
return nullptr;
}
const DataObject* targetObject = m_RootGroup[path[0]];
const DataObject* targetObject = getDataMap()[path[0]];
for(usize index = 1; index < path.getLength(); index++)
{
if(targetObject == nullptr)
Expand Down Expand Up @@ -508,10 +509,15 @@ void DataStructure::dataDeleted(DataObject::IdType identifier, const std::string
notify(msg);
}

DataMap& DataStructure::getRootGroup()
{
return getDataMap();
}

std::vector<DataObject*> DataStructure::getTopLevelData() const
{
std::vector<DataObject*> topLevel;
for(auto& iter : m_RootGroup)
for(auto& iter : getDataMap())
{
auto obj = iter.second;
topLevel.push_back(obj.get());
Expand All @@ -520,35 +526,26 @@ std::vector<DataObject*> DataStructure::getTopLevelData() const
return topLevel;
}

const DataMap& DataStructure::getDataMap() const
{
return m_RootGroup;
}

DataMap& DataStructure::getRootGroup()
{
return m_RootGroup;
}

bool DataStructure::insertTopLevel(const std::shared_ptr<DataObject>& obj)
{
if(obj == nullptr)
{
return false;
}

if(m_RootGroup.contains(obj.get()) || m_RootGroup.contains(obj->getName()))
auto& dataMap = getDataMap();
if(dataMap.contains(obj.get()) || dataMap.contains(obj->getName()))
{
return false;
}

return m_RootGroup.insert(obj);
return dataMap.insert(obj);
}

bool DataStructure::removeTopLevel(DataObject* data)
{
std::string name = data->getName();
if(!m_RootGroup.remove(data))
if(!getDataMap().remove(data))
{
return false;
}
Expand Down Expand Up @@ -585,22 +582,22 @@ bool DataStructure::finishAddingObject(const std::shared_ptr<DataObject>& dataOb

DataStructure::Iterator DataStructure::begin()
{
return m_RootGroup.begin();
return getDataMap().begin();
}

DataStructure::Iterator DataStructure::end()
{
return m_RootGroup.end();
return getDataMap().end();
}

DataStructure::ConstIterator DataStructure::begin() const
{
return m_RootGroup.begin();
return getDataMap().begin();
}

DataStructure::ConstIterator DataStructure::end() const
{
return m_RootGroup.end();
return getDataMap().end();
}

bool DataStructure::insert(const std::shared_ptr<DataObject>& dataObject, const DataPath& dataPath)
Expand Down Expand Up @@ -644,7 +641,7 @@ bool DataStructure::insertIntoRoot(const std::shared_ptr<DataObject>& dataObject
return false;
}

if(!m_RootGroup.insert(dataObject))
if(!getDataMap().insert(dataObject))
{
return false;
}
Expand Down Expand Up @@ -734,7 +731,7 @@ void DataStructure::notify(const std::shared_ptr<AbstractDataStructureMessage>&
DataStructure& DataStructure::operator=(const DataStructure& rhs)
{
m_DataObjects = rhs.m_DataObjects;
m_RootGroup = rhs.m_RootGroup;
getDataMap() = rhs.getDataMap();
m_IsValid = rhs.m_IsValid;
m_NextId = rhs.m_NextId;

Expand All @@ -760,17 +757,33 @@ DataStructure& DataStructure::operator=(const DataStructure& rhs)
DataStructure& DataStructure::operator=(DataStructure&& rhs) noexcept
{
m_DataObjects = std::move(rhs.m_DataObjects);
m_RootGroup = std::move(rhs.m_RootGroup);
getDataMap() = std::move(rhs.getDataMap());
m_IsValid = std::move(rhs.m_IsValid);
m_NextId = std::move(rhs.m_NextId);

applyAllDataStructure();
return *this;
}

std::shared_ptr<DataObject> DataStructure::deepCopy(const DataPath& copyPath)
{
// DataStructure cannot be contained in another DataStructure...
return nullptr;
}

DataObject* DataStructure::shallowCopy()
{
return new DataStructure(*this);
}

std::string DataStructure::getTypeName() const
{
return k_TypeName;
}

void DataStructure::applyAllDataStructure()
{
m_RootGroup.setDataStructure(this);
setDataStructure(this);
}

nonstd::expected<void, std::string> DataStructure::validateNumberOfTuples(const std::vector<DataPath>& dataPaths) const
Expand Down Expand Up @@ -861,7 +874,7 @@ void DataStructure::resetIds(DataObject::IdType startingId)
dataObjectPtr->checkUpdatedIds(updatedIdsMap);
}
}
m_RootGroup.updateIds(updatedIdsMap);
getDataMap().updateIds(updatedIdsMap);
}

void DataStructure::exportHierarchyAsGraphViz(std::ostream& outputStream) const
Expand Down Expand Up @@ -1015,7 +1028,7 @@ Result<> DataStructure::validateGeometries() const
* the compile will fail if the unit tests are enabled. Which they are on all the CI machines.
*/
Result<> result;
for(const auto& dataObject : m_RootGroup)
for(const auto& dataObject : getDataMap())
{
auto dataObjectType = dataObject.second->getDataObjectType();
if(dataObjectType >= DataObject::Type::IGeometry && dataObjectType <= DataObject::Type::TetrahedralGeom)
Expand Down
32 changes: 23 additions & 9 deletions src/simplnx/DataStructure/DataStructure.hpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#pragma once

#include "simplnx/Common/Result.hpp"
#include "simplnx/DataStructure/BaseGroup.hpp"
#include "simplnx/DataStructure/DataMap.hpp"
#include "simplnx/DataStructure/DataObject.hpp"
#include "simplnx/DataStructure/LinkedPath.hpp"
Expand Down Expand Up @@ -40,7 +41,7 @@ inline const std::string k_ImportableTag = "Importable";
* geometries, and scalars are added to the structure. The DataStructure allows
* parents to be added to or removed from DataObjects.
*/
class SIMPLNX_EXPORT DataStructure
class SIMPLNX_EXPORT DataStructure : public BaseGroup
{
using WeakCollectionType = std::map<DataObject::IdType, std::weak_ptr<DataObject>>;

Expand All @@ -56,6 +57,8 @@ class SIMPLNX_EXPORT DataStructure
bool finishAddingObject(const std::shared_ptr<DataObject>& obj, const std::optional<DataObject::IdType>& parent = {});

public:
static constexpr StringLiteral k_TypeName = "DataStructure";

using SignalType = nod::signal<void(DataStructure*, const std::shared_ptr<AbstractDataStructureMessage>&)>;
using Iterator = DataMap::Iterator;
using ConstIterator = DataMap::ConstIterator;
Expand Down Expand Up @@ -95,7 +98,7 @@ class SIMPLNX_EXPORT DataStructure
* @brief Clears the DataStructure by removing all DataObjects. The next
* DataObject ID remains unchanged after the operation.
*/
void clear();
void clear() override;

/**
* @brief Returns the IdType for the DataObject found at the specified DataPath. The
Expand Down Expand Up @@ -745,12 +748,6 @@ class SIMPLNX_EXPORT DataStructure
*/
std::vector<DataObject*> getTopLevelData() const;

/**
* @brief Returns a reference to the DataMap backing the top level of the DataStructure.
* @return const DataMap&
*/
const DataMap& getDataMap() const;

/**
* @brief Inserts a new DataObject into the DataStructure nested under the given
* DataPath. If the DataPath is empty, the DataObject is added directly to
Expand Down Expand Up @@ -868,6 +865,24 @@ class SIMPLNX_EXPORT DataStructure
*/
DataStructure& operator=(DataStructure&& rhs) noexcept;

/**
* @brief Returns a deep copy of the DataStructure.
* @return DataObject*
*/
std::shared_ptr<DataObject> deepCopy(const DataPath& copyPath) override;

/**
* @brief Returns a shallow copy of the DataObject.
* @return DataObject*
*/
DataObject* shallowCopy() override;

/**
* @brief Returns typename of the DataObject as a std::string.
* @return std::string
*/
std::string getTypeName() const override;

/**
* @brief Sets the next ID to use when constructing a DataObject.
* Because IDs are created to be unique, this should only be called when
Expand Down Expand Up @@ -1025,7 +1040,6 @@ class SIMPLNX_EXPORT DataStructure
// Variables
SignalType m_Signal;
WeakCollectionType m_DataObjects;
DataMap m_RootGroup;
bool m_IsValid = false;
DataObject::IdType m_NextId = 1;
};
Expand Down