From 3bfe5793f6145aa5ec6efcb583641247b7d81c57 Mon Sep 17 00:00:00 2001 From: alemuntoni Date: Tue, 23 Mar 2021 13:15:24 +0100 Subject: [PATCH] SimpleTempData DataBegin and const DataBegin --- vcg/complex/allocate.h | 4 ++-- vcg/complex/base.h | 2 +- vcg/container/simple_temporary_data.h | 34 +++++++++++++++------------ 3 files changed, 22 insertions(+), 18 deletions(-) diff --git a/vcg/complex/allocate.h b/vcg/complex/allocate.h index fb088206..c81368ba 100644 --- a/vcg/complex/allocate.h +++ b/vcg/complex/allocate.h @@ -2207,7 +2207,7 @@ public: pa._padding = 0; } - template + template static void FixPaddedPerTetraAttribute(MeshType &m, PointerToAttribute &pa) { @@ -2245,7 +2245,7 @@ public: // copy the padded container in the new one char * ptr = (char*)( ((Attribute *)pa._handle)->DataBegin()); - memcpy((void*)_handle->attribute ,(void*) &(ptr[0]) ,sizeof(ATTR_TYPE)); + memcpy((void*)_handle->DataBegin() ,(void*) &(ptr[0]) ,sizeof(ATTR_TYPE)); // remove the padded container delete ( (Attribute *) pa._handle); diff --git a/vcg/complex/base.h b/vcg/complex/base.h index 828bdcae..599cc975 100644 --- a/vcg/complex/base.h +++ b/vcg/complex/base.h @@ -340,7 +340,7 @@ public: Attribute * _handle; int n_attr; - ATTR_TYPE & operator ()(){ return *((Attribute *)_handle)->attribute;} + ATTR_TYPE & operator ()(){ return *((ATTR_TYPE*)((Attribute *)_handle)->DataBegin());} }; // Some common Handle typedefs to simplify use diff --git a/vcg/container/simple_temporary_data.h b/vcg/container/simple_temporary_data.h index 059d65c7..ac00c378 100644 --- a/vcg/container/simple_temporary_data.h +++ b/vcg/container/simple_temporary_data.h @@ -41,6 +41,7 @@ public: virtual void Reorder(std::vector &newVertIndex) = 0; virtual size_t SizeOf() const = 0; virtual void *DataBegin() = 0; + virtual const void* DataBegin() const = 0; virtual void *At(size_t i) = 0; virtual const void *At(size_t i) const = 0; @@ -56,12 +57,12 @@ template class VectorNBW { public: - VectorNBW() : data(nullptr), datasize(0), datareserve(0) {} + VectorNBW() : booldata(nullptr), datasize(0), datareserve(0) {} ~VectorNBW() { - if (data) - delete[] data; + if (booldata) + delete[] booldata; } void reserve(size_t sz) @@ -70,8 +71,8 @@ public: return; bool *newdataLoc = new bool[sz]; if (datasize != 0) - memcpy(newdataLoc, data, sizeof(bool) * sizeof(datasize)); - std::swap(data, newdataLoc); + memcpy(newdataLoc, booldata, sizeof(bool) * sizeof(datasize)); + std::swap(booldata, newdataLoc); if (newdataLoc != 0) delete[] newdataLoc; datareserve = sz; @@ -85,12 +86,12 @@ public: if (sz > datareserve) reserve(sz); datasize = sz; - memset(&data[oldDatasize], 0, datasize - oldDatasize); + memset(&booldata[oldDatasize], 0, datasize - oldDatasize); } void push_back(const bool &v) { resize(datasize + 1); - data[datasize] = v; + booldata[datasize] = v; } void clear() { datasize = 0; } @@ -99,14 +100,14 @@ public: bool empty() const { return datasize == 0; } - bool* begin() {return data;} - const bool *begin() const { return data; } + bool* data() {return booldata;} + const bool *data() const { return booldata; } - bool &operator[](size_t i) { return data[i]; } - const bool &operator[](size_t i) const { return data[i]; } + bool &operator[](size_t i) { return booldata[i]; } + const bool &operator[](size_t i) const { return booldata[i]; } private: - bool *data; + bool *booldata; size_t datasize; size_t datareserve; }; @@ -184,7 +185,7 @@ public: void Reorder(std::vector &newVertIndex) { - for (unsigned int i = 0; i < data.size(); ++i) + for (size_t i = 0; i < data.size(); ++i) { if (newVertIndex[i] != (std::numeric_limits::max)()) data[newVertIndex[i]] = data[i]; @@ -192,7 +193,8 @@ public: } size_t SizeOf() const { return sizeof(ATTR_TYPE); } - void *DataBegin() { return data.empty() ? NULL : &(*data.begin()); } + void *DataBegin() { return data.empty() ? nullptr : data.data(); } + const void *DataBegin() const { return data.empty() ? nullptr : data.data(); } }; template @@ -200,11 +202,11 @@ class Attribute : public SimpleTempDataBase { public: typedef ATTR_TYPE AttrType; - AttrType *attribute; Attribute() { attribute = new ATTR_TYPE(); } ~Attribute() { delete attribute; } size_t SizeOf() const { return sizeof(ATTR_TYPE); } void *DataBegin() { return attribute; } + const void* DataBegin() const {return attribute;} void Resize(size_t) { assert(0); } void Reorder(std::vector &) { assert(0); } @@ -220,6 +222,8 @@ public: return (void *)0; } void CopyValue(const size_t, const size_t, const SimpleTempDataBase *) { assert(0); } +private: + AttrType *attribute; }; } // end namespace vcg