Improve docs. Make getFieldT(offset) use vector::at.

This commit is contained in:
Bruno Martins
2019-05-20 15:49:00 -04:00
committed by mdavidsaver
parent a29894ee2b
commit 4dd7a18301
3 changed files with 112 additions and 94 deletions

View File

@@ -566,20 +566,6 @@ FieldConstPtr Structure::getFieldImpl(string const & fieldName, bool throws) con
}
}
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;
@@ -794,20 +780,6 @@ FieldConstPtr Union::getFieldImpl(string const & fieldName, bool throws) const {
}
}
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;

View File

@@ -717,72 +717,96 @@ public:
* @return The number of fields.
*/
std::size_t getNumberFields() const {return fieldNames.size();}
/**
* Get the field for the specified fieldName.
* @param fieldName The name of the field to get;
* @return The introspection interface.
* This will hold a null pointer if the field is not in the structure.
* Lookup Field by name
* @param fieldName Member field name. May not contain '.'
* @return NULL if no member by this name.
*/
FieldConstPtr getField(std::string const &fieldName) const;
/** Lookup Field by name and cast to Field sub-class.
* @param fieldName Member field name. May not contain '.'
* @return NULL If no member by this name, or member exists, but has type other than FT.
*/
template<typename FT>
std::tr1::shared_ptr<const FT> getField(std::string const &fieldName) const
{
STATIC_ASSERT(FT::isField); // only allow cast from Field sub-class
FieldConstPtr field(getField(fieldName));
if (field)
return std::tr1::dynamic_pointer_cast<const FT>(field);
else
return std::tr1::shared_ptr<const FT>();
return std::tr1::dynamic_pointer_cast<const FT>(getField(fieldName));
}
/**
* 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.
* Lookup Field by name
* @param fieldName Member field name. May not contain '.'
* @return Field pointer (never NULL)
* @throws std::runtime_error If no member by this name
*/
FieldConstPtr getFieldT(std::string const &fieldName) const {return getFieldImpl(fieldName, true);};
/** Lookup Field by name and cast to Field sub-class.
* @param fieldName Member field name. May not contain '.'
* @return Field pointer (never NULL)
* @throws std::runtime_error If no member by this name, or member exists, but has type other than FT.
*/
template<typename FT>
std::tr1::shared_ptr<const FT> getFieldT(std::string const &fieldName) const
{
STATIC_ASSERT(FT::isField); // only allow cast from Field sub-class
return std::tr1::dynamic_pointer_cast<const FT>(getFieldT(fieldName));
std::tr1::shared_ptr<const FT> result(
std::tr1::dynamic_pointer_cast<const FT>(getFieldT(fieldName))
);
if (!result)
throw std::runtime_error("Wrong Field type");
return result;
}
/**
* 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.
/** Lookup Field by index, within this Structure.
* @param index Index of member in this structure. @code index>=0 && index<getNumberFields() @endcode
* @return Field pointer (never NULL)
* @throws std::out_of_range If index >= getNumberFields()
*/
const FieldConstPtr& getField(std::size_t index) const {return fields.at(index);}
/** Lookup Field by index, within this Structure.
* @param index Index of member in this structure. @code index>=0 && index<getNumberFields() @endcode
* @return NULL if member is not a sub-class of FT
* @throws std::out_of_range If index >= getNumberFields()
*/
template<typename FT>
std::tr1::shared_ptr<const FT> getField(std::size_t index) const
{
STATIC_ASSERT(FT::isField); // only allow cast from Field sub-class
const FieldConstPtr& field(getField(index));
if (field)
return std::tr1::dynamic_pointer_cast<const FT>(field);
else
return std::tr1::shared_ptr<const FT>();
return std::tr1::dynamic_pointer_cast<const FT>(getField(index));
}
/**
* 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.
/** Lookup Field by index, within this Structure.
* @param index Index of member in this structure. @code index>=0 && index<getNumberFields() @endcode
* @return Field pointer (never NULL)
* @throws std::out_of_range If index >= getNumberFields()
*/
FieldConstPtr getFieldT(std::size_t index) const {return getFieldImpl(index, true);}
FieldConstPtr getFieldT(std::size_t index) const {return fields.at(index);}
/** Lookup Field by index, within this Structure.
* @param index Index of member in this structure. @code index>=0 && index<getNumberFields() @endcode
* @return Field pointer (never NULL)
* @throws std::out_of_range If index >= getNumberFields()
* @throws std::runtime_error If member is not a sub-class of FT
*/
template<typename FT>
std::tr1::shared_ptr<const FT> getFieldT(std::size_t index) const
{
STATIC_ASSERT(FT::isField); // only allow cast from Field sub-class
return std::tr1::dynamic_pointer_cast<const FT>(getFieldT(index));
std::tr1::shared_ptr<const FT> result(
std::tr1::dynamic_pointer_cast<const FT>(getFieldT(index))
);
if (!result)
throw std::runtime_error("Wrong Field type");
return result;
}
/**
@@ -827,7 +851,6 @@ private:
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;
@@ -874,72 +897,96 @@ public:
* @return The number of fields.
*/
std::size_t getNumberFields() const {return fieldNames.size();}
/**
* Get the field for the specified fieldName.
* @param fieldName The name of the field to get;
* @return The introspection interface.
* This will hold a null pointer if the field is not in the union.
* Lookup Field by name
* @param fieldName Member field name. May not contain '.'
* @return NULL if no member by this name.
*/
FieldConstPtr getField(std::string const &fieldName) const;
/** Lookup Field by name and cast to Field sub-class.
* @param fieldName Member field name. May not contain '.'
* @return NULL If no member by this name, or member exists, but has type other than FT.
*/
template<typename FT>
std::tr1::shared_ptr<const FT> getField(std::string const &fieldName) const
{
STATIC_ASSERT(FT::isField); // only allow cast from Field sub-class
FieldConstPtr field = getField(fieldName);
if (field)
return std::tr1::dynamic_pointer_cast<const FT>(field);
else
return std::tr1::shared_ptr<const FT>();
return std::tr1::dynamic_pointer_cast<const FT>(getField(fieldName));
}
/**
* 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.
* Lookup Field by name
* @param fieldName Member field name. May not contain '.'
* @return Field pointer (never NULL)
* @throws std::runtime_error If no member by this name
*/
FieldConstPtr getFieldT(std::string const &fieldName) const {return getFieldImpl(fieldName, true);};
/** Lookup Field by name and cast to Field sub-class.
* @param fieldName Member field name. May not contain '.'
* @return Field pointer (never NULL)
* @throws std::runtime_error If no member by this name, or member exists, but has type other than FT.
*/
template<typename FT>
std::tr1::shared_ptr<const FT> getFieldT(std::string const &fieldName) const
{
STATIC_ASSERT(FT::isField); // only allow cast from Field sub-class
return std::tr1::dynamic_pointer_cast<const FT>(getFieldT(fieldName));
std::tr1::shared_ptr<const FT> result(
std::tr1::dynamic_pointer_cast<const FT>(getFieldT(fieldName))
);
if (!result)
throw std::runtime_error("Wrong Field type");
return result;
}
/**
* 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.
/** Lookup Field by index, within this Union.
* @param index Index of member in this union. @code index>=0 && index<getNumberFields() @endcode
* @return Field pointer (never NULL)
* @throws std::out_of_range If index >= getNumberFields()
*/
FieldConstPtr getField(std::size_t index) const {return fields.at(index);}
/** Lookup Field by index, within this Union.
* @param index Index of member in this union. @code index>=0 && index<getNumberFields() @endcode
* @return NULL if member is not a sub-class of FT
* @throws std::out_of_range If index >= getNumberFields()
*/
template<typename FT>
std::tr1::shared_ptr<const FT> getField(std::size_t index) const
{
STATIC_ASSERT(FT::isField); // only allow cast from Field sub-class
FieldConstPtr field = getField(index);
if (field)
return std::tr1::dynamic_pointer_cast<const FT>(field);
else
return std::tr1::shared_ptr<const FT>();
return std::tr1::dynamic_pointer_cast<const FT>(getField(index));
}
/**
* 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.
/** Lookup Field by index, within this Union.
* @param index Index of member in this union. @code index>=0 && index<getNumberFields() @endcode
* @return Field pointer (never NULL)
* @throws std::out_of_range If index >= getNumberFields()
*/
FieldConstPtr getFieldT(std::size_t index) const {return getFieldImpl(index, true);}
FieldConstPtr getFieldT(std::size_t index) const {return fields.at(index);}
/** Lookup Field by index, within this Structure.
* @param index Index of member in this structure. @code index>=0 && index<getNumberFields() @endcode
* @return Field pointer (never NULL)
* @throws std::out_of_range If index >= getNumberFields()
* @throws std::runtime_error If member is not a sub-class of FT
*/
template<typename FT>
std::tr1::shared_ptr<const FT> getFieldT(std::size_t index) const
{
STATIC_ASSERT(FT::isField); // only allow cast from Field sub-class
return std::tr1::dynamic_pointer_cast<const FT>(getFieldT(index));
std::tr1::shared_ptr<const FT> result(
std::tr1::dynamic_pointer_cast<const FT>(getFieldT(index))
);
if (!result)
throw std::runtime_error("Wrong Field type");
return result;
}
/**
@@ -1001,7 +1048,6 @@ private:
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;

View File

@@ -169,7 +169,7 @@ static void testStructure()
try {
FieldConstPtr field(struct1->getFieldT(9999));
testFail("missing required exception");
} catch (std::runtime_error& e) {
} catch (std::out_of_range& e) {
testPass("caught expected exception: %s", e.what());
}
@@ -241,7 +241,7 @@ static void testUnion()
try {
FieldConstPtr field(union1->getFieldT(9999));
testFail("missing required exception");
} catch (std::runtime_error& e) {
} catch (std::out_of_range& e) {
testPass("caught expected exception: %s", e.what());
}