update data.h

This commit is contained in:
Michael Davidsaver
2019-12-18 18:29:15 -08:00
parent 583ee684ab
commit cdeecdcc46
5 changed files with 46 additions and 11 deletions
+21 -3
View File
@@ -14,6 +14,12 @@
namespace pvxs {
NoField::NoField()
:std::runtime_error ("No such field")
{}
NoField::~NoField() {}
NoConvert::NoConvert()
:std::runtime_error ("No conversion defined")
{}
@@ -154,6 +160,12 @@ const std::string& Value::id() const
return desc->id;
}
bool Value::idStartsWith(const std::string& prefix) const
{
auto ID = this->id();
return ID.size()>=prefix.size() && prefix==ID.substr(0u, prefix.size());
}
namespace {
// C-style cast between scalar storage types, and print to string (base 10)
template<typename Src>
@@ -176,7 +188,7 @@ bool copyOutScalar(const Src& src, void *ptr, StoreType type)
void Value::copyOut(void *ptr, StoreType type) const
{
if(!desc)
throw NoConvert();
throw NoField();
switch(store->code) {
case StoreType::Real: if(copyOutScalar(store->as<double>(), ptr, type)) return; else break;
@@ -251,7 +263,7 @@ bool copyInScalar(Dest& dest, const void *ptr, StoreType type)
void Value::copyIn(const void *ptr, StoreType type)
{
if(!desc)
throw NoConvert();
throw NoField();
switch(store->code) {
case StoreType::Real: if(!copyInScalar(store->as<double>(), ptr, type)) throw NoConvert(); break;
@@ -316,8 +328,14 @@ void Value::copyIn(const void *ptr, StoreType type)
break;
}
}
// fall through
throw NoConvert();
case StoreType::Null:
if(desc->code==TypeCode::Struct && type==StoreType::Compound) {
auto& val = *reinterpret_cast<const Value*>(ptr);
if(val.desc && val.desc->code==TypeCode::Struct) {
// Struct to Struct assignment.
}
}
throw NoConvert();
}
+2 -3
View File
@@ -9,8 +9,7 @@
namespace pvxs {
namespace nt {
TypeDef NTScalar::build()
TypeDef NTScalar::build() const
{
return TypeDef(TypeCode::Struct,
value.isarray() ? "epics:nt/NTScalarArray:1.0" : "epics:nt/NTScalar:1.0", {
@@ -72,7 +71,7 @@ TypeDef NTScalar::build()
// return def;
}
TypeDef NTNDArray::build()
TypeDef NTNDArray::build() const
{
TypeDef def(TypeCode::Struct, "epics:nt/NTNDArray:1.0");
+12 -2
View File
@@ -37,7 +37,7 @@ struct FieldDesc;
//! maps T to one of the types which can be stored in the FieldStorage::store union
//! typename StorageMap<T>::store_t is, if existant, is one such type.
//! Can store_t shall be cast-able to/from T.
//! store_t shall be cast-able to/from T.
//! StorageMap<T>::code is the associated StoreType.
template<typename T, typename Enable=void>
struct StorageMap;
@@ -65,6 +65,10 @@ template<>
struct StorageMap<char*>
{ typedef std::string store_t; static constexpr StoreType code{StoreType::String}; };
template<>
struct StorageMap<const char*>
{ typedef std::string store_t; static constexpr StoreType code{StoreType::String}; };
template<>
struct StorageMap<shared_array<const void>>
{ typedef shared_array<const void> store_t; static constexpr StoreType code{StoreType::Array}; };
@@ -214,11 +218,16 @@ public:
PVXS_API
std::ostream& operator<<(std::ostream& strm, const TypeDef&);
struct PVXS_API NoField : public std::runtime_error
{
explicit NoField();
virtual ~NoField();
};
struct PVXS_API NoConvert : public std::runtime_error
{
explicit NoConvert();
virtual ~NoConvert();
TypeCode source;
};
//! pointer-like reference to a single data field
@@ -265,6 +274,7 @@ public:
TypeCode type() const;
StoreType storageType() const;
const std::string& id() const;
bool idStartsWith(const std::string& prefix) const;
//! test for instance equality.
inline bool compareInst(const Value& o) { return store==o.store; }
+10 -2
View File
@@ -9,6 +9,8 @@
#include <pvxs/version.h>
#include <pvxs/data.h>
struct epicsTimeStamp; // epicsTime.h
namespace pvxs {
namespace nt {
@@ -19,12 +21,18 @@ struct NTScalar {
bool valueAlarm;
PVXS_API
TypeDef build();
TypeDef build() const;
inline Value create() const {
return build().create();
}
};
struct NTNDArray {
PVXS_API
TypeDef build();
TypeDef build() const;
inline Value create() const {
return build().create();
}
};
}} // namespace pvxs::nt
+1 -1
View File
@@ -43,7 +43,7 @@ void testBasic()
testOk1(!val.valid());
testOk1(!val.isMarked());
testThrows<NoConvert>([&val]() {
testThrows<NoField>([&val]() {
val.from(4.2);
});
}