Add {Structure,Union}::getFieldT, fix {Structure,Union}::getField
This commit is contained in:
committed by
mdavidsaver
parent
cfcdd1a3f9
commit
caa11605fc
@@ -532,15 +532,6 @@ string Structure::getID() const
|
||||
return id;
|
||||
}
|
||||
|
||||
FieldConstPtr Structure::getField(string const & fieldName) const {
|
||||
for(size_t i=0, N=fields.size(); i<N; i++) {
|
||||
if(fieldName==fieldNames[i]) {
|
||||
return fields[i];
|
||||
}
|
||||
}
|
||||
return FieldConstPtr();
|
||||
}
|
||||
|
||||
size_t Structure::getFieldIndex(string const &fieldName) const {
|
||||
size_t numberFields = fields.size();
|
||||
for(size_t i=0; i<numberFields; i++) {
|
||||
@@ -551,6 +542,35 @@ size_t Structure::getFieldIndex(string const &fieldName) const {
|
||||
return -1;
|
||||
}
|
||||
|
||||
FieldConstPtr Structure::getFieldImpl(string const & fieldName, bool throws) const {
|
||||
for(size_t i=0, N=fields.size(); i<N; i++)
|
||||
if(fieldName==fieldNames[i])
|
||||
return fields[i];
|
||||
|
||||
if (throws) {
|
||||
std::stringstream ss;
|
||||
ss << "Failed to get field: "
|
||||
<< fieldName << " (not found)";
|
||||
throw std::runtime_error(ss.str());
|
||||
} else {
|
||||
return FieldConstPtr();
|
||||
}
|
||||
}
|
||||
|
||||
FieldConstPtr Structure::getFieldImpl(size_t fieldOffset, bool throws) const {
|
||||
if (fieldOffset < fields.size())
|
||||
return fields[fieldOffset];
|
||||
|
||||
if (throws) {
|
||||
std::stringstream ss;
|
||||
ss << "Failed to get field with offset "
|
||||
<< fieldOffset << " (Invalid offset)";
|
||||
throw std::runtime_error(ss.str());
|
||||
} else {
|
||||
return FieldConstPtr();
|
||||
}
|
||||
}
|
||||
|
||||
std::ostream& Structure::dump(std::ostream& o) const
|
||||
{
|
||||
o << format::indent() << getID() << std::endl;
|
||||
@@ -730,16 +750,6 @@ string Union::getID() const
|
||||
return id;
|
||||
}
|
||||
|
||||
FieldConstPtr Union::getField(string const & fieldName) const {
|
||||
size_t numberFields = fields.size();
|
||||
for(size_t i=0; i<numberFields; i++) {
|
||||
FieldConstPtr pfield = fields[i];
|
||||
int result = fieldName.compare(fieldNames[i]);
|
||||
if(result==0) return pfield;
|
||||
}
|
||||
return FieldConstPtr();
|
||||
}
|
||||
|
||||
size_t Union::getFieldIndex(string const &fieldName) const {
|
||||
size_t numberFields = fields.size();
|
||||
for(size_t i=0; i<numberFields; i++) {
|
||||
@@ -750,6 +760,35 @@ size_t Union::getFieldIndex(string const &fieldName) const {
|
||||
return -1;
|
||||
}
|
||||
|
||||
FieldConstPtr Union::getFieldImpl(string const & fieldName, bool throws) const {
|
||||
for(size_t i=0, N=fields.size(); i<N; i++)
|
||||
if(fieldName==fieldNames[i])
|
||||
return fields[i];
|
||||
|
||||
if (throws) {
|
||||
std::stringstream ss;
|
||||
ss << "Failed to get field: "
|
||||
<< fieldName << " (not found)";
|
||||
throw std::runtime_error(ss.str());
|
||||
} else {
|
||||
return FieldConstPtr();
|
||||
}
|
||||
}
|
||||
|
||||
FieldConstPtr Union::getFieldImpl(size_t fieldOffset, bool throws) const {
|
||||
if (fieldOffset < fields.size())
|
||||
return fields[fieldOffset];
|
||||
|
||||
if (throws) {
|
||||
std::stringstream ss;
|
||||
ss << "Failed to get field with offset "
|
||||
<< fieldOffset << " (Invalid offset)";
|
||||
throw std::runtime_error(ss.str());
|
||||
} else {
|
||||
return FieldConstPtr();
|
||||
}
|
||||
}
|
||||
|
||||
std::ostream& Union::dump(std::ostream& o) const
|
||||
{
|
||||
o << format::indent() << getID() << std::endl;
|
||||
|
||||
@@ -719,7 +719,7 @@ public:
|
||||
* @return The introspection interface.
|
||||
* This will hold a null pointer if the field is not in the structure.
|
||||
*/
|
||||
FieldConstPtr getField(std::string const &fieldName) const;
|
||||
FieldConstPtr getField(std::string const &fieldName) const {return getFieldImpl(fieldName, false);};
|
||||
|
||||
template<typename FT>
|
||||
std::tr1::shared_ptr<const FT> getField(std::string const &fieldName) const
|
||||
@@ -731,13 +731,27 @@ public:
|
||||
return std::tr1::shared_ptr<const FT>();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the field for the specified fieldName.
|
||||
* @param fieldName The name of the field to get;
|
||||
* @return The introspection interface.
|
||||
* This will throw a runtime_error exception if the field is not in the structure.
|
||||
*/
|
||||
FieldConstPtr getFieldT(std::string const &fieldName) const {return getFieldImpl(fieldName, true);};
|
||||
|
||||
template<typename FT>
|
||||
std::tr1::shared_ptr<const FT> getFieldT(std::string const &fieldName) const
|
||||
{
|
||||
return std::tr1::dynamic_pointer_cast<const FT>(getFieldT(fieldName));
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the field for the specified fieldName.
|
||||
* @param index The index of the field to get;
|
||||
* @return The introspection interface.
|
||||
* This will hold a null pointer if the field is not in the structure.
|
||||
*/
|
||||
const FieldConstPtr& getField(std::size_t index) const {return fields.at(index);}
|
||||
FieldConstPtr getField(std::size_t index) const {return getFieldImpl(index, false);}
|
||||
|
||||
template<typename FT>
|
||||
std::tr1::shared_ptr<const FT> getField(std::size_t index) const
|
||||
@@ -749,6 +763,20 @@ public:
|
||||
return std::tr1::shared_ptr<const FT>();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the field for the specified fieldName.
|
||||
* @param index The index of the field to get;
|
||||
* @return The introspection interface.
|
||||
* This will throw a runtime_error exception if the field is not in the structure.
|
||||
*/
|
||||
FieldConstPtr getFieldT(std::size_t index) const {return getFieldImpl(index, true);}
|
||||
|
||||
template<typename FT>
|
||||
std::tr1::shared_ptr<const FT> getFieldT(std::size_t index) const
|
||||
{
|
||||
return std::tr1::dynamic_pointer_cast<const FT>(getFieldT(index));
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the field index for the specified fieldName.
|
||||
* @return The introspection interface.
|
||||
@@ -790,6 +818,8 @@ private:
|
||||
FieldConstPtrArray fields;
|
||||
std::string id;
|
||||
|
||||
FieldConstPtr getFieldImpl(const std::string& fieldName, bool throws) const;
|
||||
FieldConstPtr getFieldImpl(const std::size_t fieldOffset, bool throws) const;
|
||||
void dumpFields(std::ostream& o) const;
|
||||
|
||||
friend class FieldCreate;
|
||||
@@ -842,7 +872,7 @@ public:
|
||||
* @return The introspection interface.
|
||||
* This will hold a null pointer if the field is not in the union.
|
||||
*/
|
||||
FieldConstPtr getField(std::string const &fieldName) const;
|
||||
FieldConstPtr getField(std::string const &fieldName) const {return getFieldImpl(fieldName, false);};
|
||||
|
||||
template<typename FT>
|
||||
std::tr1::shared_ptr<const FT> getField(std::string const &fieldName) const
|
||||
@@ -854,13 +884,27 @@ public:
|
||||
return std::tr1::shared_ptr<const FT>();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the field for the specified fieldName.
|
||||
* @param fieldName The name of the field to get;
|
||||
* @return The introspection interface.
|
||||
* This will throw a runtime_error exception if the field is not in the union.
|
||||
*/
|
||||
FieldConstPtr getFieldT(std::string const &fieldName) const {return getFieldImpl(fieldName, true);};
|
||||
|
||||
template<typename FT>
|
||||
std::tr1::shared_ptr<const FT> getFieldT(std::string const &fieldName) const
|
||||
{
|
||||
return std::tr1::dynamic_pointer_cast<const FT>(getFieldT(fieldName));
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the field for the specified fieldName.
|
||||
* @param index The index of the field to get;
|
||||
* @return The introspection interface.
|
||||
* This will hold a null pointer if the field is not in the union.
|
||||
*/
|
||||
FieldConstPtr getField(std::size_t index) const {return fields.at(index);}
|
||||
FieldConstPtr getField(std::size_t index) const {return getFieldImpl(index, false);}
|
||||
|
||||
template<typename FT>
|
||||
std::tr1::shared_ptr<const FT> getField(std::size_t index) const
|
||||
@@ -872,6 +916,20 @@ public:
|
||||
return std::tr1::shared_ptr<const FT>();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the field for the specified fieldName.
|
||||
* @param index The index of the field to get;
|
||||
* @return The introspection interface.
|
||||
* This will throw a runtime_error exception if the field is not in the union.
|
||||
*/
|
||||
FieldConstPtr getFieldT(std::size_t index) const {return getFieldImpl(index, true);}
|
||||
|
||||
template<typename FT>
|
||||
std::tr1::shared_ptr<const FT> getFieldT(std::size_t index) const
|
||||
{
|
||||
return std::tr1::dynamic_pointer_cast<const FT>(getFieldT(index));
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the field index for the specified fieldName.
|
||||
* @return The introspection interface.
|
||||
@@ -929,7 +987,9 @@ private:
|
||||
StringArray fieldNames;
|
||||
FieldConstPtrArray fields;
|
||||
std::string id;
|
||||
|
||||
|
||||
FieldConstPtr getFieldImpl(const std::string& fieldName, bool throws) const;
|
||||
FieldConstPtr getFieldImpl(const std::size_t fieldOffset, bool throws) const;
|
||||
void dumpFields(std::ostream& o) const;
|
||||
|
||||
friend class FieldCreate;
|
||||
|
||||
@@ -146,6 +146,28 @@ static void testStructure()
|
||||
testOk1(struct1->getFieldName(0)==names1[0]);
|
||||
testOk1(struct1->getFieldName(1)==names1[1]);
|
||||
|
||||
testOk1(struct1->getField("nonexistent").get()==NULL);
|
||||
testOk1(struct1->getField(9999).get()==NULL);
|
||||
|
||||
testOk1(struct1->getFieldT("innerA")==fields1[0]);
|
||||
testOk1(struct1->getFieldT("innerB")==fields1[1]);
|
||||
testOk1(struct1->getFieldT(0)==fields1[0]);
|
||||
testOk1(struct1->getFieldT(1)==fields1[1]);
|
||||
|
||||
try {
|
||||
FieldConstPtr field(struct1->getFieldT("nonexistent"));
|
||||
testFail("missing required exception");
|
||||
} catch (std::runtime_error& e) {
|
||||
testPass("caught expected exception: %s", e.what());
|
||||
}
|
||||
|
||||
try {
|
||||
FieldConstPtr field(struct1->getFieldT(9999));
|
||||
testFail("missing required exception");
|
||||
} catch (std::runtime_error& e) {
|
||||
testPass("caught expected exception: %s", e.what());
|
||||
}
|
||||
|
||||
testOk1(struct1->getID() == Structure::DEFAULT_ID);
|
||||
|
||||
testOk1(fields1 == struct1->getFields()); // vector equality
|
||||
@@ -190,6 +212,28 @@ static void testUnion()
|
||||
testOk1(union1->getFieldName(0)==names1[0]);
|
||||
testOk1(union1->getFieldName(1)==names1[1]);
|
||||
|
||||
testOk1(union1->getField("nonexistent").get()==NULL);
|
||||
testOk1(union1->getField(9999).get()==NULL);
|
||||
|
||||
testOk1(union1->getFieldT("innerA")==fields1[0]);
|
||||
testOk1(union1->getFieldT("innerB")==fields1[1]);
|
||||
testOk1(union1->getFieldT(0)==fields1[0]);
|
||||
testOk1(union1->getFieldT(1)==fields1[1]);
|
||||
|
||||
try {
|
||||
FieldConstPtr field(union1->getFieldT("nonexistent"));
|
||||
testFail("missing required exception");
|
||||
} catch (std::runtime_error& e) {
|
||||
testPass("caught expected exception: %s", e.what());
|
||||
}
|
||||
|
||||
try {
|
||||
FieldConstPtr field(union1->getFieldT(9999));
|
||||
testFail("missing required exception");
|
||||
} catch (std::runtime_error& e) {
|
||||
testPass("caught expected exception: %s", e.what());
|
||||
}
|
||||
|
||||
testOk1(union1->getID() == Union::DEFAULT_ID);
|
||||
|
||||
testOk1(fields1 == union1->getFields()); // vector equality
|
||||
@@ -311,7 +355,7 @@ static void testMapping()
|
||||
|
||||
MAIN(testIntrospect)
|
||||
{
|
||||
testPlan(326);
|
||||
testPlan(342);
|
||||
fieldCreate = getFieldCreate();
|
||||
pvDataCreate = getPVDataCreate();
|
||||
standardField = getStandardField();
|
||||
|
||||
Reference in New Issue
Block a user