From 3b72454cf087b5ba100f4515241ff37c077c315a Mon Sep 17 00:00:00 2001 From: Goutham Mudide Date: Wed, 19 Jul 2023 08:31:50 +0530 Subject: [PATCH] PlyData class cannot be implicitly copied. -Explicitly delete the copy constructor, to make it clear that the behavior is intentional -Provide a .copy() function for the few situations where one really does want to explicitly copy --- happly.h | 150 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 150 insertions(+) diff --git a/happly.h b/happly.h index 46ded3e..99df324 100644 --- a/happly.h +++ b/happly.h @@ -129,6 +129,29 @@ class Property { * @param name_ */ Property(const std::string& name_) : name(name_){}; + + Property(const Property& rhs) { name = rhs.name; } + + Property(Property&& rhs) { name = std::move(rhs.name); } + + Property& operator=(const Property& rhs) { + + if (this != &rhs) { + name = rhs.name; + } + + return *this; + } + + Property& operator=(Property&& rhs) { + + if (this != &rhs) { + name = std::move(rhs.name); + } + + return *this; + } + virtual ~Property(){}; std::string name; @@ -206,6 +229,8 @@ class Property { * @return */ virtual std::string propertyTypeName() = 0; + + virtual std::unique_ptr clone() const = 0; }; namespace { @@ -302,6 +327,30 @@ class TypedProperty : public Property { virtual ~TypedProperty() override{}; + TypedProperty(const TypedProperty& rhs) : Property(rhs) { + + // Deep copy the vector elements + data.reserve(rhs.data.size()); + std::copy(rhs.data.begin(), rhs.data.end(), std::back_inserter(data)); + } + + TypedProperty& operator=(const TypedProperty& rhs) { + + if (this != &other) { + Property::operator=(rhs); + + // Deep copy the vector elements + data.clear(); + data.reserve(rhs.data.size()); + std::copy(rhs.data.begin(), rhs.data.end(), std::back_inserter(data)); + } + + return *this; + } + + TypedProperty(TypedProperty&& rhs) = delete; + TypedProperty& operator=(TypedProperty&& rhs) = delete; + /** * @brief Reserve memory. * @@ -401,6 +450,8 @@ class TypedProperty : public Property { */ virtual std::string propertyTypeName() override { return typeName(); } + std::unique_ptr clone() const override { return std::make_unique(*this); } + /** * @brief The actual data contained in the property */ @@ -449,6 +500,36 @@ class TypedListProperty : public Property { } }; + TypedListProperty(const TypedListProperty& rhs) : Property(rhs) { + + listCountBytes = rhs.listCountBytes; + flattenedIndexStart = rhs.flattenedIndexStart; + + // Deep copy the vector elements + flattenedData.reserve(rhs.flattenedData.size()); + std::copy(rhs.flattenedData.begin(), rhs.flattenedData.end(), std::back_inserter(flattenedData)); + } + + TypedListProperty& operator=(const TypedListProperty& rhs) { + + if (this != &other) { + Property::operator=(rhs); + + listCountBytes = rhs.listCountBytes; + flattenedIndexStart = rhs.flattenedIndexStart; + + // Deep copy the vector elements + data.clear(); + flattenedData.reserve(rhs.flattenedData.size()); + std::copy(rhs.flattenedData.begin(), rhs.flattenedData.end(), std::back_inserter(flattenedData)); + } + + return *this; + } + + TypedListProperty(TypedListProperty&& rhs) = delete; + TypedListProperty& operator=(TypedListProperty&& rhs) = delete; + virtual ~TypedListProperty() override{}; /** @@ -637,6 +718,8 @@ class TypedListProperty : public Property { */ virtual std::string propertyTypeName() override { return typeName(); } + std::unique_ptr clone() const override { return std::make_unique(*this); } + /** * @brief The (flattened) data for the property, as formed by concatenating all of the individual element lists * together. @@ -786,6 +869,49 @@ class Element { */ Element(const std::string& name_, size_t count_) : name(name_), count(count_) {} + Element(const Element& rhs) { + name = rhs.name; + count = rhs.count; + + //deep copy + properties.reserve(this->properties.size()); + for (const auto& element : this->properties) { + properties.push_back(element->clone()); + } + } + + Element operator=(const Element& rhs) + { + if (this != &rhs) { + name = rhs.name; + count = rhs.count; + + // deep copy + properties.clear(); + properties.reserve(this->properties.size()); + for (const auto& element : this->properties) { + properties.push_back(element->clone()); + } + } + + return *this; + } + + Element(Element&& rhs) { + name = std::move(rhs.name); + count = std::move(rhs.count); + properties = std::move(rhs.properties); + } + + Element operator=(Element&& rhs) { + + if (this != &rhs) { + name = std::move(rhs.name); + count = std::move(rhs.count); + properties = std::move(rhs.properties); + } + } + std::string name; size_t count; std::vector> properties; @@ -1328,6 +1454,30 @@ class PLYData { } } + PLYData(const PLYData&) = delete; + PLYData& operator=(const PLYData&) = delete; + PLYData(PLYData&&) = delete; + PLYData& operator=(PLYData&&) = delete; + + PLYData& copy() { + + PLYData tmp; + + //copy comments + for (const auto& comments : this->comments) { + tmp.comments.push_back(comments); // Create new copies of each string + } + + //copy obj_info comments + for (const auto& objInfoComments : this->objInfoComments) { + tmp.objInfoComments.push_back(objInfoComments); // Create new copies of each string + } + + tmp.elements = this->elements; + + return std::move(tmp); + } + /** * @brief Perform sanity checks on the file, throwing if any fail. */