From 849a5a081b1cd19dd2fa0cb557a0a7503b7d50e7 Mon Sep 17 00:00:00 2001 From: "arseny.kapoulkine" Date: Sun, 29 Aug 2010 15:04:27 +0000 Subject: XPath: Initial support for operation without exceptions git-svn-id: http://pugixml.googlecode.com/svn/trunk@636 99668b35-9821-0410-8761-19e4c4f06640 --- src/pugixml.hpp | 239 ++++++++++++++++++++++++++++++++------------------------ 1 file changed, 137 insertions(+), 102 deletions(-) (limited to 'src/pugixml.hpp') diff --git a/src/pugixml.hpp b/src/pugixml.hpp index 28e9a34..ce48a45 100644 --- a/src/pugixml.hpp +++ b/src/pugixml.hpp @@ -56,13 +56,13 @@ namespace std # endif #endif -// No XPath without STL or exceptions -#if (defined(PUGIXML_NO_STL) || defined(PUGIXML_NO_EXCEPTIONS)) && !defined(PUGIXML_NO_XPATH) +// No XPath without STL +#if !defined(PUGIXML_NO_XPATH) && defined(PUGIXML_NO_STL) # define PUGIXML_NO_XPATH #endif // Include exception header for XPath -#ifndef PUGIXML_NO_XPATH +#if !defined(PUGIXML_NO_XPATH) && !defined(PUGIXML_NO_EXCEPTIONS) # include #endif @@ -339,102 +339,9 @@ namespace pugi #ifndef PUGIXML_NO_XPATH class xpath_node; class xpath_node_set; - class xpath_ast_node; - class xpath_allocator; - - /// XPath query return type classification - enum xpath_value_type - { - xpath_type_none, ///< Unknown type (query failed to compile) - xpath_type_node_set, ///< Node set (xpath_node_set) - xpath_type_number, ///< Number - xpath_type_string, ///< String - xpath_type_boolean ///< Boolean - }; - - /// XPath query return type classification - /// \deprecated This type is deprecated and will be removed in future versions; use xpath_value_type instead - typedef xpath_value_type xpath_type_t; - - /** - * A class that holds compiled XPath query and allows to evaluate query result - */ - class PUGIXML_CLASS xpath_query - { - private: - // Non-copyable semantics - xpath_query(const xpath_query&); - xpath_query& operator=(const xpath_query&); - - xpath_allocator* m_alloc; - xpath_ast_node* m_root; - - public: - /** - * Constructor from string with XPath expression. - * Throws xpath_exception on compilation error, std::bad_alloc on out of memory error. - * - * \param query - string with XPath expression - */ - explicit xpath_query(const char_t* query); - - /** - * Destructor - */ - ~xpath_query(); - - /** - * Get query expression return type - * - * \return expression return type - **/ - xpath_value_type return_type() const; - - /** - * Evaluate expression as boolean value for the context node \a n. - * If expression does not directly evaluate to boolean, the expression result is converted - * as through boolean() XPath function call. - * Throws std::bad_alloc on out of memory error. - * - * \param n - context node - * \return evaluation result - */ - bool evaluate_boolean(const xml_node& n) const; - - /** - * Evaluate expression as double value for the context node \a n. - * If expression does not directly evaluate to double, the expression result is converted - * as through number() XPath function call. - * Throws std::bad_alloc on out of memory error. - * - * \param n - context node - * \return evaluation result - */ - double evaluate_number(const xml_node& n) const; - - /** - * Evaluate expression as string value for the context node \a n. - * If expression does not directly evaluate to string, the expression result is converted - * as through string() XPath function call. - * Throws std::bad_alloc on out of memory error. - * - * \param n - context node - * \return evaluation result - */ - string_t evaluate_string(const xml_node& n) const; - - /** - * Evaluate expression as node set for the context node \a n. - * If expression does not directly evaluate to node set, throws xpath_exception. - * Throws std::bad_alloc on out of memory error. - * - * \param n - context node - * \return evaluation result - */ - xpath_node_set evaluate_node_set(const xml_node& n) const; - }; + class xpath_query; #endif - + /** * Abstract writer class * \see xml_node::print @@ -2016,21 +1923,146 @@ namespace pugi }; #ifndef PUGIXML_NO_XPATH + class xpath_ast_node; + class xpath_allocator; + + /// XPath query return type classification + enum xpath_value_type + { + xpath_type_none, ///< Unknown type (query failed to compile) + xpath_type_node_set, ///< Node set (xpath_node_set) + xpath_type_number, ///< Number + xpath_type_string, ///< String + xpath_type_boolean ///< Boolean + }; + + /// XPath query return type classification + /// \deprecated This type is deprecated and will be removed in future versions; use xpath_value_type instead + typedef xpath_value_type xpath_type_t; + + struct PUGIXML_CLASS xpath_parse_result + { + /// Error message (0 if no error) + const char* error; + + /// Last parsed offset (in characters from string start) + ptrdiff_t offset; + + /// Cast to bool operator + operator bool() const + { + return error == 0; + } + + /// Get error description + const char* description() const; + }; + + /** + * A class that holds compiled XPath query and allows to evaluate query result + */ + class PUGIXML_CLASS xpath_query + { + private: + // Non-copyable semantics + xpath_query(const xpath_query&); + xpath_query& operator=(const xpath_query&); + + xpath_allocator* m_alloc; + xpath_ast_node* m_root; + xpath_parse_result _result; + + typedef xpath_ast_node* xpath_query::*unspecified_bool_type; + + public: + /** + * Constructor from string with XPath expression. + * Throws xpath_exception on compilation error, std::bad_alloc on out of memory error. + * + * \param query - string with XPath expression + */ + explicit xpath_query(const char_t* query); + + /** + * Destructor + */ + ~xpath_query(); + + /** + * Get query expression return type + * + * \return expression return type + **/ + xpath_value_type return_type() const; + + /** + * Evaluate expression as boolean value for the context node \a n. + * If expression does not directly evaluate to boolean, the expression result is converted + * as through boolean() XPath function call. + * Throws std::bad_alloc on out of memory error. + * + * \param n - context node + * \return evaluation result + */ + bool evaluate_boolean(const xml_node& n) const; + + /** + * Evaluate expression as double value for the context node \a n. + * If expression does not directly evaluate to double, the expression result is converted + * as through number() XPath function call. + * Throws std::bad_alloc on out of memory error. + * + * \param n - context node + * \return evaluation result + */ + double evaluate_number(const xml_node& n) const; + + /** + * Evaluate expression as string value for the context node \a n. + * If expression does not directly evaluate to string, the expression result is converted + * as through string() XPath function call. + * Throws std::bad_alloc on out of memory error. + * + * \param n - context node + * \return evaluation result + */ + string_t evaluate_string(const xml_node& n) const; + + /** + * Evaluate expression as node set for the context node \a n. + * If expression does not directly evaluate to node set, throws xpath_exception. + * Throws std::bad_alloc on out of memory error. + * + * \param n - context node + * \return evaluation result + */ + xpath_node_set evaluate_node_set(const xml_node& n) const; + + // Get parsing result + const xpath_parse_result& result() const; + + // Safe bool conversion + operator unspecified_bool_type() const; + + // Borland C++ workaround + bool operator!() const; + }; + + #ifndef PUGIXML_NO_EXCEPTIONS /** * XPath exception class. */ class PUGIXML_CLASS xpath_exception: public std::exception { private: - const char* m_message; + xpath_parse_result _result; public: /** - * Construct exception from static error string + * Construct exception from parse result * - * \param message - error string */ - explicit xpath_exception(const char* message); + explicit xpath_exception(const xpath_parse_result& result); /** * Return error message @@ -2038,7 +2070,10 @@ namespace pugi * \return error message */ virtual const char* what() const throw(); + + const xpath_parse_result& result() const; }; + #endif /** * XPath node class. -- cgit v1.2.3