From 6171cd6867e4a12e1f0dfec95e50ec0e18ab6665 Mon Sep 17 00:00:00 2001 From: Michael Davidsaver Date: Sun, 7 Oct 2018 10:08:43 -0700 Subject: [PATCH] escape and quote PVString(Array)::dumpValue() Escaping for both. quote array values, but not scalar. Also move remaining template virtuals out of line. --- src/factory/PVDataCreateFactory.cpp | 111 ++++++++++++++++++++++++++++ src/pv/pvData.h | 78 ++++--------------- 2 files changed, 125 insertions(+), 64 deletions(-) diff --git a/src/factory/PVDataCreateFactory.cpp b/src/factory/PVDataCreateFactory.cpp index 6412548..41e1534 100644 --- a/src/factory/PVDataCreateFactory.cpp +++ b/src/factory/PVDataCreateFactory.cpp @@ -61,6 +61,48 @@ template<> const ScalarType PVStringArray::typeCode = pvString; template PVScalarValue::~PVScalarValue() {} +template +std::ostream& PVScalarValue::dumpValue(std::ostream& o) const +{ + return o << get(); +} + +template +void PVScalarValue::operator>>=(T& value) const +{ + value = get(); +} + +template +void PVScalarValue::operator<<=(typename storage_t::arg_type value) +{ + put(value); +} + +template +void PVScalarValue::assign(const PVScalar& scalar) +{ + if(isImmutable()) + throw std::invalid_argument("destination is immutable"); + copyUnchecked(scalar); +} + +template +void PVScalarValue::copy(const PVScalar& from) +{ + assign(from); +} + +template +void PVScalarValue::copyUnchecked(const PVScalar& from) +{ + if(this==&from) + return; + T result; + from.getAs((void*)&result, typeCode); + put(result); +} + template void PVScalarValue::serialize(ByteBuffer *pbuffer, SerializableControl *pflusher) const { @@ -100,6 +142,13 @@ PVString::PVString(ScalarConstPtr const & scalar) storage.maxLength = 0; } +std::ostream& PVString::dumpValue(std::ostream& o) const +{ + // we escape, but do not quote, for scalar string + o<::PVValueArray(UnionArrayConstPtr const & unionArray) template PVValueArray::~PVValueArray() {} +template +ArrayConstPtr PVValueArray::getArray() const +{ + return std::tr1::static_pointer_cast(this->getField()); +} + +template +std::ostream& PVValueArray::dumpValue(std::ostream& o) const +{ + const_svector v(this->view()); + typename const_svector::const_iterator it(v.begin()), + end(v.end()); + o << '['; + if(it!=end) { + o << print_cast(*it++); + for(; it!=end; ++it) + o << ',' << print_cast(*it); + + } + return o << ']'; +} + +template<> +std::ostream& PVValueArray::dumpValue(std::ostream& o, size_t index) const +{ + return o << '"' << escape(this->view().at(index)) << '"'; +} + +template<> +std::ostream& PVValueArray::dumpValue(std::ostream& o) const +{ + const_svector v(this->view()); + const_svector::const_iterator it(v.begin()), + end(v.end()); + o << '['; + if(it!=end) { + o << '"' << escape(*it++) << '"'; + for(; it!=end; ++it) + o << ", \"" << escape(*it) << '"'; + + } + return o << ']'; +} + +template +std::ostream& PVValueArray::dumpValue(std::ostream& o, size_t index) const +{ + return o << print_cast(this->view().at(index)); +} + template void PVValueArray::setCapacity(size_t capacity) { @@ -351,6 +450,18 @@ void PVValueArray::serialize(ByteBuffer *pbuffer, } } +template +void PVValueArray::_getAsVoid(epics::pvData::shared_vector& out) const +{ + out = static_shared_vector_cast(this->view()); +} + +template +void PVValueArray::_putFromVoid(const epics::pvData::shared_vector& in) +{ + this->replace(shared_vector_convert(in)); +} + // Factory PVDataCreate::PVDataCreate() diff --git a/src/pv/pvData.h b/src/pv/pvData.h index 83df1ca..59a0eac 100644 --- a/src/pv/pvData.h +++ b/src/pv/pvData.h @@ -395,31 +395,22 @@ public: * Put a new value into the PVScalar. * @param value The value. */ - void put(typename storage_t::arg_type v) { + inline void put(typename storage_t::arg_type v) { storage.store(v); PVField::postPut(); } - std::ostream& dumpValue(std::ostream& o) const OVERRIDE FINAL - { - return o << get(); - } + virtual std::ostream& dumpValue(std::ostream& o) const OVERRIDE; // get operator // double value; doubleField >>= value; // NOTE: virtual is needed for MS C++ compiler to get this operator exported - virtual void operator>>=(T& value) const - { - value = get(); - } + virtual void operator>>=(T& value) const; // put operator // double value = 12.8; doubleField <<= value; // NOTE: virtual is needed for MS C++ compiler to get this operator exported - virtual void operator<<=(typename storage_t::arg_type value) - { - put(value); - } + virtual void operator<<=(typename storage_t::arg_type value); template inline T1 getAs() const { @@ -437,24 +428,9 @@ public: PVScalar::putFrom(v); } - virtual void assign(const PVScalar& scalar) OVERRIDE FINAL - { - if(isImmutable()) - throw std::invalid_argument("destination is immutable"); - copyUnchecked(scalar); - } - virtual void copy(const PVScalar& from) OVERRIDE FINAL - { - assign(from); - } - virtual void copyUnchecked(const PVScalar& from) OVERRIDE FINAL - { - if(this==&from) - return; - T result; - from.getAs((void*)&result, typeCode); - put(result); - } + virtual void assign(const PVScalar& scalar) OVERRIDE FINAL; + virtual void copy(const PVScalar& from) OVERRIDE FINAL; + virtual void copyUnchecked(const PVScalar& from) OVERRIDE FINAL; virtual void serialize(ByteBuffer *pbuffer, SerializableControl *pflusher) const OVERRIDE; @@ -546,6 +522,8 @@ public: */ virtual ~PVString() {} + virtual std::ostream& dumpValue(std::ostream& o) const OVERRIDE FINAL; + virtual void serialize(ByteBuffer *pbuffer, SerializableControl *pflusher) const OVERRIDE FINAL; virtual void serialize(ByteBuffer *pbuffer, @@ -1207,25 +1185,10 @@ public: /** * Get introspection interface. */ - virtual ArrayConstPtr getArray() const OVERRIDE FINAL - { - return std::tr1::static_pointer_cast(this->getField()); - } + virtual ArrayConstPtr getArray() const OVERRIDE FINAL; - std::ostream& dumpValue(std::ostream& o) const OVERRIDE FINAL - { - const_svector v(this->view()); - typename const_svector::const_iterator it(v.begin()), - end(v.end()); - o << '['; - if(it!=end) { - o << print_cast(*it++); - for(; it!=end; ++it) - o << ',' << print_cast(*it); - - } - return o << ']'; - } + virtual std::ostream& dumpValue(std::ostream& o) const OVERRIDE FINAL; + virtual std::ostream& dumpValue(std::ostream& o, size_t index) const OVERRIDE FINAL; virtual size_t getLength() const OVERRIDE FINAL {return value.size();} virtual size_t getCapacity() const OVERRIDE FINAL {return value.capacity();} @@ -1243,22 +1206,9 @@ public: virtual void serialize(ByteBuffer *pbuffer, SerializableControl *pflusher, size_t offset, size_t count) const OVERRIDE FINAL; - std::ostream& dumpValue(std::ostream& o, size_t index) const OVERRIDE FINAL - { - return o << print_cast(this->view().at(index)); - } - protected: - virtual void _getAsVoid(epics::pvData::shared_vector& out) const OVERRIDE FINAL - { - out = static_shared_vector_cast(this->view()); - } - - virtual void _putFromVoid(const epics::pvData::shared_vector& in) OVERRIDE FINAL - { - // TODO: try to re-use storage - this->replace(shared_vector_convert(in)); - } + virtual void _getAsVoid(epics::pvData::shared_vector& out) const OVERRIDE FINAL; + virtual void _putFromVoid(const epics::pvData::shared_vector& in) OVERRIDE FINAL; explicit PVValueArray(ScalarArrayConstPtr const & scalar); const_svector value;