From ba25f019012b632f6af9f5addee5058ba6a4e077 Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Tue, 6 Mar 2018 21:37:39 +0000 Subject: Wrap support for making unique ptrs from functions --- service/apiImpl.cpp | 44 ++++++++++++++++++++++++++------------------ 1 file changed, 26 insertions(+), 18 deletions(-) (limited to 'service/apiImpl.cpp') diff --git a/service/apiImpl.cpp b/service/apiImpl.cpp index 588d360..1de31cb 100644 --- a/service/apiImpl.cpp +++ b/service/apiImpl.cpp @@ -1,8 +1,9 @@ #include "apiImpl.h" +#include "uptr.h" #include #include -#include +#include #include #include @@ -21,35 +22,42 @@ namespace MirrorSearch { return fetch(sql::getServices); } - typedef std::shared_ptr xmlDocSPtr; - typedef std::shared_ptr xmlXPathContextSPtr; - typedef std::shared_ptr xmlXPathObjectSPtr; + template + void + libxmlErrorHandler(const std::string & fn, const P & ... p) + { + throw XmlError(Fmt::get(fn, xmlGetLastError()->message, p...)); + } + template + auto lEHB(const P & ... p) + { + return std::bind(&libxmlErrorHandler, std::placeholders::_1, p...); + } + typedef UPtr xmlDocSPtr; + typedef UPtr xmlXPathContextSPtr; + typedef UPtr xmlXPathObjectSPtr; + + AdHocFormatter(Read, "Failed to read in %? (%?) [%?, %?]"); static auto getDoc(const ::std::string & url, int flags) { - if (auto doc = xmlDocSPtr(htmlReadFile(url.c_str(), NULL, flags), xmlFreeDoc)) { - return doc; - } - throw XmlError("Failed to open " + url); + return make_unique(htmlReadFile, xmlFreeDoc, lEHB(url, flags), url.c_str(), (const char*)NULL, flags); } + AdHocFormatter(XPathCtx, "Failed to create xpath context in %? (%?)"); static auto getXPathCxt(const xmlDocSPtr & doc) { - if (auto xpathCtx = xmlXPathContextSPtr(xmlXPathNewContext(doc.get()), xmlXPathFreeContext)) { - return xpathCtx; - } - throw XmlError("Failed to create xpath context"); + return make_unique(xmlXPathNewContext, xmlXPathFreeContext, lEHB(), doc.get()); } + AdHocFormatter(XPathEval, "Failed to evaluate xpath in %? (%?) [%?]"); static auto getXPathObj(const ::std::string & xpath, const xmlXPathContextSPtr & ctx, xmlXPathObjectType type) { - if (auto xpathObj = xmlXPathObjectSPtr(xmlXPathEvalExpression(BAD_CAST xpath.c_str(), ctx.get()), xmlXPathFreeObject)) { - if (xpathObj->type != type) { - throw XmlError("Xpath evaluates to wrong type " + xpath); - } - return xpathObj; + auto xpathObj = make_unique(xmlXPathEvalExpression, xmlXPathFreeObject, lEHB(xpath), BAD_CAST xpath.c_str(), ctx.get()); + if (xpathObj->type != type) { + throw XmlError("Xpath evaluates to wrong type " + xpath); } - throw XmlError("Failed to evaluate xpath " + xpath); + return xpathObj; } void SearchImpl::callService(const ::std::string & fn, const SearchServicePtr & s, SearchHits & sh) const -- cgit v1.2.3