add PVScalar::putFrom/getAs variants for AnyScalar

This commit is contained in:
Michael Davidsaver
2017-09-30 10:04:31 -05:00
parent 0d12464e30
commit 594a29b2db
2 changed files with 55 additions and 2 deletions

View File

@@ -23,6 +23,7 @@
#include <pv/pvIntrospect.h>
#include <pv/typeCast.h>
#include <pv/anyscalar.h>
#include <pv/sharedVector.h>
#include <shareLib.h>
@@ -339,6 +340,8 @@ protected:
virtual void getAs(void *, ScalarType) const = 0;
public:
virtual void getAs(AnyScalar& v) const =0;
/**
* Convert and assign the provided value.
* The value type is determined from the function template argument
@@ -357,6 +360,10 @@ public:
//! Convert and assign
virtual void putFrom(const void *, ScalarType) = 0;
inline void putFrom(const AnyScalar& v) {
if(v)
putFrom(v.unsafe(), v.type());
}
virtual void assign(const PVScalar&) = 0;
@@ -459,6 +466,11 @@ public:
put(castUnsafe<T,T1>(val));
}
FORCE_INLINE void putFrom(const AnyScalar& v) {
// the template form of putFrom() hides the base class AnyScalar overload
PVScalar::putFrom(v);
}
virtual void assign(const PVScalar& scalar) OVERRIDE
{
if(isImmutable())
@@ -492,6 +504,10 @@ protected:
castUnsafeV(1, rtype, result, typeCode, (const void*)&src);
}
public:
virtual void getAs(AnyScalar& v) const
{
v = get();
}
virtual void putFrom(const void *src, ScalarType stype) OVERRIDE
{
T result;

View File

@@ -11,7 +11,7 @@
#include <string>
#include <cstdio>
#include <epicsUnitTest.h>
#include <pv/pvUnitTest.h>
#include <testMain.h>
#include <pv/pvIntrospect.h>
@@ -626,6 +626,42 @@ static void testFieldAccess()
}
}
static void testAnyScalar()
{
PVStructurePtr value(getPVDataCreate()->createPVStructure(getFieldCreate()->createFieldBuilder()
->add("a", pvInt)
->add("b", pvDouble)
->add("c", pvString)
->createStructure()));
PVIntPtr a(value->getSubFieldT<PVInt>("a"));
PVDoublePtr b(value->getSubFieldT<PVDouble>("b"));
PVStringPtr c(value->getSubFieldT<PVString>("c"));
a->put(42);
testEqual(a->get(), 42);
testEqual(b->get(), 0.0);
{
AnyScalar temp;
a->getAs(temp);
b->putFrom(temp);
}
testEqual(a->get(), 42);
testEqual(b->get(), 42.0);
testEqual(c->get(), "");
{
AnyScalar temp;
a->getAs(temp);
c->putFrom(temp);
}
testEqual(a->get(), 42);
testEqual(c->get(), "42");
}
static void testSubField()
{
PVStructurePtr value(ValueBuilder()
@@ -678,7 +714,7 @@ static void testSubField()
MAIN(testPVData)
{
testPlan(251);
testPlan(258);
try{
fieldCreate = getFieldCreate();
pvDataCreate = getPVDataCreate();
@@ -692,6 +728,7 @@ MAIN(testPVData)
testRequest();
testCopy();
testFieldAccess();
testAnyScalar();
testSubField();
}catch(std::exception& e){
PRINT_EXCEPTION(e);