From 9ac030169ac96617c8cfcef43a73bd5fc74b375c Mon Sep 17 00:00:00 2001 From: Michael Davidsaver Date: Thu, 25 Jul 2013 17:22:16 -0400 Subject: [PATCH] update pvD array handling Interface only uses shared_vector storage also uses only const. --- pvDataApp/misc/byteBuffer.h | 4 +-- pvDataApp/pv/pvData.h | 61 +++++++++++++++---------------------- 2 files changed, 26 insertions(+), 39 deletions(-) diff --git a/pvDataApp/misc/byteBuffer.h b/pvDataApp/misc/byteBuffer.h index 5964668..903daf2 100644 --- a/pvDataApp/misc/byteBuffer.h +++ b/pvDataApp/misc/byteBuffer.h @@ -379,7 +379,7 @@ public: * @param count The number of elements. */ template - inline void putArray(T* values, std::size_t count); + inline void putArray(const T* values, std::size_t count); /** * Get an array of type {@code T} from the byte buffer. * The position is adjusted. @@ -842,7 +842,7 @@ private: } template - inline void ByteBuffer::putArray(T* values, std::size_t count) + inline void ByteBuffer::putArray(const T* values, std::size_t count) { // this avoids int8 specialization, compiler will take care if optimization, -O2 or more if (sizeof(T) == 1) diff --git a/pvDataApp/pv/pvData.h b/pvDataApp/pv/pvData.h index ef4232b..257fd34 100644 --- a/pvDataApp/pv/pvData.h +++ b/pvDataApp/pv/pvData.h @@ -1022,11 +1022,9 @@ namespace detail { virtual ~PVVectorStorage(){}; // Primative array manipulations - protected: - //! unchecked reference to writable data - //! Please consider the view() method instead of viewUnsafe(). - virtual const svector& viewUnsafe() const = 0; - public: + + //! Fetch a read-only view of the current array data + virtual const_svector view() const = 0; /** Exchange our contents for the provided. * @@ -1038,32 +1036,15 @@ namespace detail { * Before you call this directly, consider using * the reuse(), or replace() methods. */ - virtual void swap(svector& other) = 0; + virtual void swap(const_svector& other) = 0; //! Discard current contents and replaced with the provided. //! Fails for Immutable arrays //! calls postPut() - virtual void replace(const const_svector& next) - { - svector temp(const_shared_vector_cast(next)); - this->swap(temp); - this->postPut(); - } - - // methods from PVArray - - virtual size_t getLength() const {return viewUnsafe().size();} - virtual size_t getCapacity() const {return viewUnsafe().capacity();} + virtual void replace(const const_svector& next) = 0; // Derived operations - //! Fetch a read-only view of the current array data - inline const_svector view() const - { - const_svector newref(this->viewUnsafe()); - return newref; - } - /** Remove and return the current array data * or an unique copy if shared. * @@ -1074,10 +1055,9 @@ namespace detail { */ inline svector reuse() { - svector result; + const_svector result; this->swap(result); - result.make_unique(); - return result; + return thaw(result); } /** @@ -1111,16 +1091,14 @@ namespace detail { { from += fromOffset; - svector temp; - this->swap(temp); + svector temp(this->reuse()); if(temp.size() < length+offset) temp.resize(length+offset); else temp.make_unique(); std::copy(from, from + length, temp.begin() + offset); - this->swap(temp); - this->postPut(); + this->replace(freeze(temp)); return length; } @@ -1143,12 +1121,13 @@ namespace detail { vector& vref = *value.get(); typename svector::shared_pointer_type p(&vref[0], detail::shared_ptr_vector_deletor(value)); - svector temp(p, 0, std::min(length, vref.size())); + const_svector temp(p, 0, std::min(length, vref.size())); this->swap(temp); } - pointer get() const { - return this->viewUnsafe().data(); + pointer get() const USAGE_DEPRECATED { + // evil unsafe cast! + return (pointer)this->view().data(); } vector const & getVector() USAGE_ERROR("No longer implemented. Replace with view()"); @@ -1255,6 +1234,10 @@ public: * Destructor */ virtual ~PVValueArray() {} + + virtual size_t getLength() const {return value.size();} + virtual size_t getCapacity() const {return value.capacity();} + /** * Set the array capacity. * @param capacity The length. @@ -1289,8 +1272,12 @@ public: */ virtual void compress(); - virtual const svector& viewUnsafe() const { return value; } - virtual void swap(svector &other); + virtual const_svector view() const { return value; } + virtual void swap(const_svector &other); + virtual void replace(const const_svector &other) { + value = other; + PVField::postPut(); + } virtual void serialize(ByteBuffer *pbuffer, SerializableControl *pflusher) const; @@ -1309,7 +1296,7 @@ protected: {} private: StructureArrayConstPtr structureArray; - svector value; + const_svector value; friend class PVDataCreate; };