Make getSubFieldT return shared pointer

Signed-off-by: Dave Hickin <david.hickin@diamond.ac.uk>
This commit is contained in:
Dave Hickin
2015-07-13 12:48:05 +01:00
parent 9827caa3e3
commit ac2b6ea8db
5 changed files with 33 additions and 30 deletions

View File

@@ -2309,15 +2309,15 @@ public:
template&lt;typename PVT&gt;
std::tr1::shared_ptr&lt;PVT&gt; getSubField(std::size_t fieldOffset) const
PVField&amp; getSubFieldT(std::string const &amp;fieldName) const;
PVFieldPtr getSubFieldT(std::string const &amp;fieldName) const;
template&lt;typename PVT&gt;
PVT&amp; getSubFieldT(std::string const &amp;fieldName) const
std::tr1::shared_ptr&lt;PVT&gt; getSubFieldT(std::string const &amp;fieldName) const
PVField&amp; getSubFieldT(std::size_t fieldOffset) const;
PVFieldPtr getSubFieldT(std::size_t fieldOffset) const;
template&lt;typename PVT&gt;
PVT&amp; getSubFieldT(std::size_t fieldOffset) const
std::tr1::shared_ptr&lt;PVT&gt; getSubFieldT(std::size_t fieldOffset) const
virtual void serialize(
ByteBuffer *pbuffer,SerializableControl *pflusher) const ;

View File

@@ -2309,15 +2309,15 @@ public:
template&lt;typename PVT&gt;
std::tr1::shared_ptr&lt;PVT&gt; getSubField(std::size_t fieldOffset) const
PVField&amp; getSubFieldT(std::string const &amp;fieldName) const;
PVFieldPtr getSubFieldT(std::string const &amp;fieldName) const;
template&lt;typename PVT&gt;
PVT&amp; getSubFieldT(std::string const &amp;fieldName) const
std::tr1::shared_ptr&lt;PVT&gt; getSubFieldT(std::string const &amp;fieldName) const
PVField&amp; getSubFieldT(std::size_t fieldOffset) const;
PVFieldPtr getSubFieldT(std::size_t fieldOffset) const;
template&lt;typename PVT&gt;
PVT&amp; getSubFieldT(std::size_t fieldOffset) const
std::tr1::shared_ptr&lt;PVT&gt; getSubFieldT(std::size_t fieldOffset) const
virtual void serialize(
ByteBuffer *pbuffer,SerializableControl *pflusher) const ;

View File

@@ -134,11 +134,11 @@ PVFieldPtr PVStructure::getSubField(size_t fieldOffset) const
throw std::logic_error("PVStructure.getSubField: Logic error");
}
PVField& PVStructure::getSubFieldT(std::size_t fieldOffset) const
PVFieldPtr PVStructure::getSubFieldT(std::size_t fieldOffset) const
{
PVField * raw = getSubField(fieldOffset).get();
if (raw)
return *raw;
PVFieldPtr pvField = getSubField(fieldOffset);
if (pvField.get())
return pvField;
else
{
std::stringstream ss;

View File

@@ -736,9 +736,9 @@ public:
* @returns A reference to the sub-field (never NULL)
* @throws std::runtime_error if the requested sub-field doesn't exist, or has a different type
*/
FORCE_INLINE PVField& getSubFieldT(std::string const &fieldName) const
FORCE_INLINE PVFieldPtr getSubFieldT(std::string const &fieldName) const
{
return *getSubFieldImpl(fieldName.c_str());
return getSubFieldImpl(fieldName.c_str())->shared_from_this();
}
/**
@@ -747,35 +747,37 @@ public:
* @returns A reference to the sub-field (never NULL)
* @throws std::runtime_error if the requested sub-field doesn't exist, or has a different type
* @code
* PVInt& ref = pvStruct->getSubFieldT<PVInt>("substruct.leaffield");
* PVIntPtr ptr = pvStruct->getSubFieldT<PVInt>("substruct.leaffield");
* @endcode
*/
template<typename PVT>
FORCE_INLINE PVT& getSubFieldT(std::string const &fieldName) const
FORCE_INLINE std::tr1::shared_ptr<PVT> getSubFieldT(std::string const &fieldName) const
{
return this->getSubFieldT<PVT>(fieldName.c_str());
}
template<typename PVT>
PVT& getSubFieldT(const char *name) const
std::tr1::shared_ptr<PVT> getSubFieldT(const char *name) const
{
PVT *raw = dynamic_cast<PVT*>(getSubFieldImpl(name));
if(!raw)
std::tr1::shared_ptr<PVT> pvField = std::tr1::dynamic_pointer_cast<PVT>(
getSubFieldImpl(name)->shared_from_this());
if (pvField.get())
return pvField;
else
{
std::stringstream ss;
ss << "Failed to get field: " << name << " (Field has wrong type)";
throw std::runtime_error(ss.str());
}
return *raw;
}
/**
* Get the subfield with the specified offset.
* @param fieldOffset The offset.
* @returns A reference to the sub-field (never NULL)
* @throws std::runtime_error if the requested sub-field doesn't exist
*/
PVField& getSubFieldT(std::size_t fieldOffset) const;
PVFieldPtr getSubFieldT(std::size_t fieldOffset) const;
/**
* Get the subfield with the specified offset.
@@ -784,11 +786,12 @@ public:
* @throws std::runtime_error if the requested sub-field doesn't exist, or has a different type
*/
template<typename PVT>
PVT& getSubFieldT(std::size_t fieldOffset) const
std::tr1::shared_ptr<PVT> getSubFieldT(std::size_t fieldOffset) const
{
PVT* raw = dynamic_cast<PVT*>(&getSubFieldT(fieldOffset));
if (raw)
return *raw;
std::tr1::shared_ptr<PVT> pvField = std::tr1::dynamic_pointer_cast<PVT>(
getSubFieldT(fieldOffset));
if (pvField.get())
return pvField;
else
{
std::stringstream ss;

View File

@@ -541,16 +541,16 @@ static void testFieldAccess()
PVIntPtr a = fld->getSubField<PVInt>("test");
testOk1(a!=NULL);
if(a.get()) {
PVInt& b = fld->getSubFieldT<PVInt>("test");
testOk(&b==a.get(), "%p == %p", &b, a.get());
PVIntPtr b = fld->getSubFieldT<PVInt>("test");
testOk(b.get()==a.get(), "%p == %p", b.get(), a.get());
} else
testSkip(1, "test doesn't exist?");
a = fld->getSubField<PVInt>("hello.world");
testOk1(a!=NULL);
if(a.get()) {
PVInt& b = fld->getSubFieldT<PVInt>("hello.world");
testOk(&b==a.get(), "%p == %p", &b, a.get());
PVIntPtr b = fld->getSubFieldT<PVInt>("hello.world");
testOk(b.get()==a.get(), "%p == %p", b.get(), a.get());
} else
testSkip(1, "hello.world doesn't exist?");