Skip to content

Commit

Permalink
Changed StringArray to store its data in a shared_ptr (#823)
Browse files Browse the repository at this point in the history
Signed-off-by: Jared Duffey <jared.duffey@bluequartz.net>
  • Loading branch information
JDuffeyBQ authored Jan 16, 2024
1 parent 3e63732 commit ce7ae08
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 18 deletions.
34 changes: 17 additions & 17 deletions src/simplnx/DataStructure/StringArray.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ StringArray* StringArray::Create(DataStructure& dataStructure, const std::string
StringArray* StringArray::CreateWithValues(DataStructure& dataStructure, const std::string_view& name, collection_type strings, const std::optional<IdType>& parentId)
{
auto data = std::shared_ptr<StringArray>(new StringArray(dataStructure, name.data()));
data->m_Strings = std::move(strings);
*data->m_Strings = std::move(strings);
if(!AttemptToAddObject(dataStructure, data, parentId))
{
return nullptr;
Expand All @@ -41,14 +41,14 @@ StringArray::StringArray(DataStructure& dataStructure, std::string name)

StringArray::StringArray(DataStructure& dataStructure, std::string name, collection_type strings)
: IArray(dataStructure, std::move(name))
, m_Strings(std::move(strings))
{
*m_Strings = std::move(strings);
}

StringArray::StringArray(DataStructure& dataStructure, std::string name, IdType importId, collection_type strings)
: IArray(dataStructure, std::move(name), importId)
, m_Strings(std::move(strings))
{
*m_Strings = std::move(strings);
}

StringArray::StringArray(const StringArray& other)
Expand Down Expand Up @@ -92,7 +92,7 @@ std::shared_ptr<DataObject> StringArray::deepCopy(const DataPath& copyPath)
return nullptr;
}
// Don't construct with identifier since it will get created when inserting into data structure
const auto copy = std::shared_ptr<StringArray>(new StringArray(dataStruct, copyPath.getTargetName(), m_Strings));
const auto copy = std::shared_ptr<StringArray>(new StringArray(dataStruct, copyPath.getTargetName(), *m_Strings));
if(dataStruct.insert(copy, copyPath.getParent()))
{
return copy;
Expand All @@ -102,22 +102,22 @@ std::shared_ptr<DataObject> StringArray::deepCopy(const DataPath& copyPath)

size_t StringArray::size() const
{
return m_Strings.size();
return m_Strings->size();
}

const StringArray::collection_type& StringArray::values() const
{
return m_Strings;
return *m_Strings;
}

StringArray::reference StringArray::operator[](usize index)
{
return m_Strings[index];
return (*m_Strings)[index];
}

StringArray::const_reference StringArray::operator[](usize index) const
{
return m_Strings[index];
return (*m_Strings)[index];
}

StringArray::const_reference StringArray::at(usize index) const
Expand All @@ -126,36 +126,36 @@ StringArray::const_reference StringArray::at(usize index) const
{
throw std::out_of_range(fmt::format("Attempting to access string at index {} out of {}", index, size()));
}
return m_Strings[index];
return (*m_Strings)[index];
}

StringArray::iterator StringArray::begin()
{
return m_Strings.begin();
return m_Strings->begin();
}

StringArray::iterator StringArray::end()
{
return m_Strings.end();
return m_Strings->end();
}

StringArray::const_iterator StringArray::begin() const
{
return m_Strings.begin();
return m_Strings->begin();
}

StringArray::const_iterator StringArray::end() const
{
return m_Strings.end();
return m_Strings->end();
}
StringArray::const_iterator StringArray::cbegin() const
{
return m_Strings.begin();
return m_Strings->begin();
}

StringArray::const_iterator StringArray::cend() const
{
return m_Strings.end();
return m_Strings->end();
}

StringArray& StringArray::operator=(const StringArray& rhs)
Expand All @@ -179,7 +179,7 @@ usize StringArray::getSize() const

bool StringArray::empty() const
{
return m_Strings.empty();
return m_Strings->empty();
}

IArray::ShapeType StringArray::getTupleShape() const
Expand Down Expand Up @@ -207,7 +207,7 @@ void StringArray::resizeTuples(const std::vector<usize>& tupleShape)
auto numTuples = std::accumulate(tupleShape.cbegin(), tupleShape.cend(), static_cast<usize>(1), std::multiplies<>());
if(numTuples != size())
{
m_Strings.resize(numTuples);
m_Strings->resize(numTuples);
}
}
} // namespace nx::core
2 changes: 1 addition & 1 deletion src/simplnx/DataStructure/StringArray.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,6 @@ class SIMPLNX_EXPORT StringArray : public IArray
StringArray(DataStructure& dataStructure, std::string name, IdType importId, collection_type strings);

private:
collection_type m_Strings;
std::shared_ptr<collection_type> m_Strings = std::make_shared<collection_type>();
};
} // namespace nx::core

0 comments on commit ce7ae08

Please sign in to comment.