From 0c4ef8f079efb45f7a5239a2a5dde60b16405c38 Mon Sep 17 00:00:00 2001 From: Michael Davidsaver Date: Mon, 8 Jul 2013 16:35:58 -0400 Subject: [PATCH] PV* use shared_vector_convert --- pvDataApp/factory/printer.cpp | 2 +- pvDataApp/pv/pvData.h | 87 +++++++++++++---------------------- 2 files changed, 32 insertions(+), 57 deletions(-) diff --git a/pvDataApp/factory/printer.cpp b/pvDataApp/factory/printer.cpp index 1776a1d..23f4d72 100644 --- a/pvDataApp/factory/printer.cpp +++ b/pvDataApp/factory/printer.cpp @@ -176,7 +176,7 @@ void PrinterPlain::encodeScalar(const PVScalar& pv) void PrinterPlain::encodeArray(const PVScalarArray& pv) { indentN(S(), ilvl); - shared_vector temp; + shared_vector temp; pv.getAs(temp); S() << pv.getScalarArray()->getID() << " " diff --git a/pvDataApp/pv/pvData.h b/pvDataApp/pv/pvData.h index fcb2758..0a9e3cd 100644 --- a/pvDataApp/pv/pvData.h +++ b/pvDataApp/pv/pvData.h @@ -693,23 +693,27 @@ public: */ const ScalarArrayConstPtr getScalarArray() const ; +protected: + virtual void _getAsVoid(shared_vector&) const = 0; + virtual void _putFromVoid(const shared_vector&) = 0; +public: + /** - * Fetch the current value and convert to the requeted type. + * Fetch the current value and convert to the requested type. * * A copy is made if the requested type does not match * the element type. If the types do match then * no copy is made. */ template - inline void - getAs(shared_vector::type>& out) const + void + getAs(shared_vector::type>& out) const { - shared_vector temp(static_shared_vector_cast(out)); - getAs(ID, temp); - out = static_shared_vector_cast::type>(temp); + typedef typename ScalarTypeTraits::type dest_type; + shared_vector temp; + _getAsVoid(temp); + out = shared_vector_convert(temp); } - virtual void - getAs(ScalarType, shared_vector& out) const = 0; /** * Assign the given value after conversion. @@ -736,12 +740,11 @@ public: * Calls postPut() */ template - inline void putFrom(const shared_vector::type>& inp) + inline void putFrom(const shared_vector::type>& inp) { - shared_vector temp(static_shared_vector_cast(inp)); - putFrom(ID, temp); + shared_vector temp(static_shared_vector_cast(inp)); + _putFromVoid(temp); } - virtual void putFrom(ScalarType, const shared_vector&) = 0; /** * Assign the given value after conversion. @@ -766,7 +769,11 @@ public: * If the types do match then a new refernce to the provided * data is kept. */ - virtual void assign(PVScalarArray& pv) = 0; + void assign(PVScalarArray& pv) { + shared_vector temp; + pv._getAsVoid(temp); + _putFromVoid(temp); + } protected: PVScalarArray(ScalarArrayConstPtr const & scalarArray); @@ -1230,23 +1237,20 @@ public: return o << *(this->get() + index); } - virtual void - getAs(ScalarType id, ::epics::pvData::shared_vector& out) const +protected: + virtual void _getAsVoid(epics::pvData::shared_vector& out) const { - const svector& data(this->viewUnsafe()); - ::epics::pvData::shared_vector temp(static_shared_vector_cast(data)); - if(id==typeCode) { - out = temp; // no convert = no copy - } else { - //TODO: reuse out if possible?? - ::epics::pvData::shared_vector vcopy(ScalarTypeFunc::allocArray(id, data.size())); - - castUnsafeV(data.size(), id, vcopy.data(), typeCode, temp.data()); - - out.swap(vcopy); - } + out = static_shared_vector_cast(this->view()); } + virtual void _putFromVoid(const epics::pvData::shared_vector& in) + { + // TODO: try to re-use storage + replace(shared_vector_convert(in)); + } + +public: + virtual size_t copyOut(ScalarType id, void* ptr, size_t olen) const { const svector& data(this->viewUnsafe()); @@ -1256,24 +1260,6 @@ public: return len; } - virtual void - putFrom(ScalarType id, const ::epics::pvData::shared_vector& inp) - { - if(id==typeCode) { - svector next(static_shared_vector_cast(inp)); - this->swap(next); // no convert == no copy - } else { - size_t len = inp.size() / ScalarTypeFunc::elementSize(id); - svector result(this->take()); - result.resize(len); - - castUnsafeV(len, typeCode, result.data(), id, inp.data()); - - this->swap(result); - } - this->postPut(); - } - virtual void copyIn(ScalarType id, const void* ptr, size_t len) { svector data(this->take()); @@ -1287,17 +1273,6 @@ public: this->postPut(); } - virtual void assign(PVScalarArray& pv) - { - if(this==&pv) - return; - ::epics::pvData::shared_vector temp; - pv.getAs(typeCode, temp); - svector next(static_shared_vector_cast(temp)); - this->swap(next); - this->postPut(); - } - protected: PVValueArray(ScalarArrayConstPtr const & scalar) : base_t(scalar) {}