summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorarseny.kapoulkine <arseny.kapoulkine@99668b35-9821-0410-8761-19e4c4f06640>2009-11-08 13:52:11 +0000
committerarseny.kapoulkine <arseny.kapoulkine@99668b35-9821-0410-8761-19e4c4f06640>2009-11-08 13:52:11 +0000
commitc8a8b30bddf0df8f137203b0a1533c46358000be (patch)
tree2d5be9f11727b6184e77bc6dcf30781890ca51f8
parenteaa078b76a6be5417e73b3fc4f5f78642d3ca6c4 (diff)
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
-rw-r--r--src/pugixml.cpp8
-rw-r--r--src/pugixml.hpp31
-rw-r--r--src/pugixpath.cpp41
3 files changed, 58 insertions, 22 deletions
diff --git a/src/pugixml.cpp b/src/pugixml.cpp
index 2e17fae..a6160e3 100644
--- a/src/pugixml.cpp
+++ b/src/pugixml.cpp
@@ -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
diff --git a/src/pugixml.hpp b/src/pugixml.hpp
index 92b7f90..3ed6229 100644
--- a/src/pugixml.hpp
+++ b/src/pugixml.hpp
@@ -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;
diff --git a/src/pugixpath.cpp b/src/pugixpath.cpp
index 4897523..f2e0e56 100644
--- a/src/pugixpath.cpp
+++ b/src/pugixpath.cpp
@@ -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);
}