Made the attribute_list of the xml parser a little more friendly by allowing

you to ask for attributes that don't exist and get a defined behavior (an
exception being thrown) rather than it being a contract violation.
This commit is contained in:
Davis King 2017-05-06 12:46:19 -04:00
parent a1eea964b6
commit 4b2eea202c
2 changed files with 24 additions and 6 deletions

View File

@ -49,7 +49,7 @@ namespace dlib
public: public:
// These typedefs are here for backwards compatibily with previous versions of // These typedefs are here for backwards compatibly with previous versions of
// dlib. // dlib.
typedef xml_parser kernel_1a; typedef xml_parser kernel_1a;
typedef xml_parser kernel_1a_c; typedef xml_parser kernel_1a_c;
@ -103,7 +103,10 @@ namespace dlib
const std::string& key const std::string& key
) const ) const
{ {
if (is_in_list(key))
return list[key]; return list[key];
else
throw xml_attribute_list_error("No XML attribute named " + key + " is present in tag.");
} }
bool at_start ( bool at_start (

View File

@ -6,10 +6,24 @@
#include <string> #include <string>
#include "../interfaces/enumerable.h" #include "../interfaces/enumerable.h"
#include "../interfaces/map_pair.h" #include "../interfaces/map_pair.h"
#include "../error.h"
namespace dlib namespace dlib
{ {
// ----------------------------------------------------------------------------------------
class xml_attribute_list_error : public dlib::error
{
/*!
WHAT THIS OBJECT REPRESENTS
This is an exception object thrown by attribute_list objects if you try to
access a non-existent attribute.
!*/
public:
xml_attribute_list_error(const std::string& msg) : dlib::error(msg){}
};
// ---------------------------------------------------------------------------------------- // ----------------------------------------------------------------------------------------
// ---------------------------------------------------------------------------------------- // ----------------------------------------------------------------------------------------
// ---------------------------------------------------------------------------------------- // ----------------------------------------------------------------------------------------
@ -43,11 +57,12 @@ namespace dlib
const std::string& key const std::string& key
) const =0; ) const =0;
/*! /*!
requires
- is_in_list(key) == true
ensures ensures
- returns a const reference to the value associated with the if (is_in_list(key) == true) then
attribute named key. - returns a const reference to the value associated with the attribute
named key.
- else
- throws xml_attribute_list_error
!*/ !*/
protected: protected: