XPath: Fixed const-correctness, added boolean operators and operator! for Borland compilers, added operator[], minor other changes

git-svn-id: http://pugixml.googlecode.com/svn/trunk@220 99668b35-9821-0410-8761-19e4c4f06640
This commit is contained in:
arseny.kapoulkine 2009-11-08 13:52:11 +00:00
parent eaa078b76a
commit c8a8b30bdd
3 changed files with 58 additions and 22 deletions

View file

@ -1953,12 +1953,12 @@ namespace pugi
#ifdef __BORLANDC__
bool operator&&(const xml_attribute& lhs, bool rhs)
{
return lhs ? rhs : false;
return (bool)lhs && rhs;
}
bool operator||(const xml_attribute& lhs, bool rhs)
{
return lhs ? true : rhs;
return (bool)lhs || rhs;
}
#endif
@ -2720,12 +2720,12 @@ namespace pugi
#ifdef __BORLANDC__
bool operator&&(const xml_node& lhs, bool rhs)
{
return lhs ? rhs : false;
return (bool)lhs && rhs;
}
bool operator||(const xml_node& lhs, bool rhs)
{
return lhs ? true : rhs;
return (bool)lhs || rhs;
}
#endif

View file

@ -298,7 +298,7 @@ namespace pugi
* \param n - context node
* \return evaluation result
*/
bool evaluate_boolean(const xml_node& n);
bool evaluate_boolean(const xml_node& n) const;
/**
* Evaluate expression as double value for the context node \a n.
@ -309,7 +309,7 @@ namespace pugi
* \param n - context node
* \return evaluation result
*/
double evaluate_number(const xml_node& n);
double evaluate_number(const xml_node& n) const;
/**
* Evaluate expression as string value for the context node \a n.
@ -320,7 +320,7 @@ namespace pugi
* \param n - context node
* \return evaluation result
*/
std::string evaluate_string(const xml_node& n);
std::string evaluate_string(const xml_node& n) const;
/**
* Evaluate expression as node set for the context node \a n.
@ -330,7 +330,7 @@ namespace pugi
* \param n - context node
* \return evaluation result
*/
xpath_node_set evaluate_node_set(const xml_node& n);
xpath_node_set evaluate_node_set(const xml_node& n) const;
};
#endif
@ -1306,7 +1306,7 @@ namespace pugi
* \param query - compiled query
* \return first node from the resulting node set by document order, or empty node if none found
*/
xpath_node select_single_node(xpath_query& query) const;
xpath_node select_single_node(const xpath_query& query) const;
/**
* Select node set by evaluating XPath query
@ -1322,7 +1322,7 @@ namespace pugi
* \param query - compiled query
* \return resulting node set
*/
xpath_node_set select_nodes(xpath_query& query) const;
xpath_node_set select_nodes(const xpath_query& query) const;
#endif
/// \internal Document order or 0 if not set
@ -1893,6 +1893,9 @@ namespace pugi
*/
operator unspecified_bool_type() const;
// Borland C++ workaround
bool operator!() const;
/**
* Compares two XPath nodes
*
@ -1910,6 +1913,12 @@ namespace pugi
bool operator!=(const xpath_node& n) const;
};
#ifdef __BORLANDC__
// Borland C++ workaround
bool PUGIXML_FUNCTION operator&&(const xpath_node& lhs, bool rhs);
bool PUGIXML_FUNCTION operator||(const xpath_node& lhs, bool rhs);
#endif
/**
* Not necessarily ordered constant collection of XPath nodes
*/
@ -1943,7 +1952,6 @@ namespace pugi
typedef xpath_node* iterator;
iterator mut_begin();
iterator mut_end();
void push_back(const xpath_node& n);
@ -1993,6 +2001,14 @@ namespace pugi
* \return collection size
*/
size_t size() const;
/**
* Get element with the specified index
*
* \param index - requested index
* \return element
*/
xpath_node operator[](size_t index) const;
/**
* Get begin constant iterator for collection
@ -2019,6 +2035,7 @@ namespace pugi
* Get first node in the collection by document order
*
* \return first node by document order
* \note set.first() is not equal to set[0], since operator[] does not take document order into account
*/
xpath_node first() const;

View file

@ -630,6 +630,11 @@ namespace pugi
return (m_node || m_attribute) ? &xpath_node::m_node : 0;
}
bool xpath_node::operator!() const
{
return !(m_node || m_attribute);
}
bool xpath_node::operator==(const xpath_node& n) const
{
return m_node == n.m_node && m_attribute == n.m_attribute;
@ -640,6 +645,18 @@ namespace pugi
return m_node != n.m_node || m_attribute != n.m_attribute;
}
#ifdef __BORLANDC__
bool operator&&(const xpath_node& lhs, bool rhs)
{
return (bool)lhs && rhs;
}
bool operator||(const xpath_node& lhs, bool rhs)
{
return (bool)lhs || rhs;
}
#endif
xpath_node_set::xpath_node_set(): m_type(type_unsorted), m_begin(&m_storage), m_end(&m_storage), m_eos(&m_storage + 1), m_using_storage(true)
{
}
@ -691,6 +708,11 @@ namespace pugi
return size() == 0;
}
xpath_node xpath_node_set::operator[](size_t index) const
{
return (index >= size()) ? xpath_node() : m_begin[index];
}
xpath_node_set::iterator xpath_node_set::mut_begin()
{
return m_begin;
@ -701,11 +723,6 @@ namespace pugi
return m_begin;
}
xpath_node_set::iterator xpath_node_set::mut_end()
{
return m_end;
}
xpath_node_set::const_iterator xpath_node_set::end() const
{
return m_end;
@ -767,6 +784,8 @@ namespace pugi
xpath_node xpath_node_set::first() const
{
if (empty()) return xpath_node();
switch (m_type)
{
case type_sorted: return *m_begin;
@ -3613,7 +3632,7 @@ namespace pugi
m_root->check_semantics();
}
bool xpath_query::evaluate_boolean(const xml_node& n)
bool xpath_query::evaluate_boolean(const xml_node& n) const
{
if (!m_root) return false;
@ -3627,7 +3646,7 @@ namespace pugi
return m_root->eval_boolean(c);
}
double xpath_query::evaluate_number(const xml_node& n)
double xpath_query::evaluate_number(const xml_node& n) const
{
if (!m_root) return gen_nan();
@ -3641,7 +3660,7 @@ namespace pugi
return m_root->eval_number(c);
}
std::string xpath_query::evaluate_string(const xml_node& n)
std::string xpath_query::evaluate_string(const xml_node& n) const
{
if (!m_root) return std::string();
@ -3655,7 +3674,7 @@ namespace pugi
return m_root->eval_string(c);
}
xpath_node_set xpath_query::evaluate_node_set(const xml_node& n)
xpath_node_set xpath_query::evaluate_node_set(const xml_node& n) const
{
if (!m_root) return xpath_node_set();
@ -3675,7 +3694,7 @@ namespace pugi
return select_single_node(q);
}
xpath_node xml_node::select_single_node(xpath_query& query) const
xpath_node xml_node::select_single_node(const xpath_query& query) const
{
xpath_node_set s = query.evaluate_node_set(*this);
return s.empty() ? xpath_node() : s.first();
@ -3687,7 +3706,7 @@ namespace pugi
return select_nodes(q);
}
xpath_node_set xml_node::select_nodes(xpath_query& query) const
xpath_node_set xml_node::select_nodes(const xpath_query& query) const
{
return query.evaluate_node_set(*this);
}