Skip to content

Commit

Permalink
Add and use xml_scoped_ptr helper.
Browse files Browse the repository at this point in the history
  • Loading branch information
vslavik committed Jul 4, 2013
1 parent ea82953 commit e5bb7ea
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 8 deletions.
21 changes: 21 additions & 0 deletions src/libxml/utility.h
Expand Up @@ -48,6 +48,27 @@ namespace xml
namespace impl
{

// Helper for holding libxml objects with guaranteed freeing
template<typename TPtr, void (*FreeFunc)(TPtr)>
class xml_scoped_ptr
{
public:
explicit xml_scoped_ptr(TPtr ptr) : ptr_(ptr) {}

~xml_scoped_ptr()
{
if (ptr_)
FreeFunc(ptr_);
}

TPtr operator->() const { return ptr_; }
TPtr get() const { return ptr_; }
operator TPtr() const { return ptr_; }

private:
TPtr ptr_;
};

// exception safe wrapper around xmlChar*s that are returned from some
// of the libxml functions that the user must free.
class xmlchar_helper
Expand Down
10 changes: 2 additions & 8 deletions src/libxml/xpath.cxx
Expand Up @@ -69,8 +69,6 @@ class nodeset_next_functor : public impl::iter_advance_functor

for ( size_t i = 0; i < length-1; i++ )
m_next[table[i]] = table[i+1];

xmlXPathFreeObject(pathobj);
}

virtual xmlNodePtr operator()(xmlNodePtr node) const
Expand Down Expand Up @@ -104,20 +102,16 @@ struct xpath_context_impl
template<typename NodesView>
NodesView evaluate(const std::string& expr, node& n)
{
// TODO: use auto ptr for this
xmlXPathObjectPtr nsptr =
xml_scoped_ptr<xmlXPathObjectPtr, xmlXPathFreeObject> nsptr(
xmlXPathNodeEval(reinterpret_cast<xmlNodePtr>(n.get_node_data()),
ctxt_);
xml_string(expr),
ctxt_));

if ( !nsptr )
return NodesView();

if ( xmlXPathNodeSetIsEmpty(nsptr->nodesetval) )
{
xmlXPathFreeObject(nsptr);
return NodesView();
}

return NodesView(nsptr->nodesetval->nodeTab[0],
new nodeset_next_functor(nsptr));
Expand Down

0 comments on commit e5bb7ea

Please sign in to comment.