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/api.ice | 4 ++-- service/apiImpl.cpp | 44 ++++++++++++++++++++++++++------------------ service/test.cpp | 5 +++++ service/uptr.cpp | 26 ++++++++++++++++++++++++++ service/uptr.h | 38 ++++++++++++++++++++++++++++++++++++++ 5 files changed, 97 insertions(+), 20 deletions(-) create mode 100644 service/uptr.cpp create mode 100644 service/uptr.h diff --git a/service/api.ice b/service/api.ice index c207c3d..64edfe7 100644 --- a/service/api.ice +++ b/service/api.ice @@ -10,8 +10,8 @@ module MirrorSearch { interface Search { idempotent SearchServices getServices(); - idempotent SearchHits getMatches(string filename); - idempotent optional(0) string feelingLucky(string filename); + idempotent SearchHits getMatches(string filename) throws XmlError; + idempotent optional(0) string feelingLucky(string filename) throws XmlError; }; }; 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 diff --git a/service/test.cpp b/service/test.cpp index c115712..8b93cf8 100644 --- a/service/test.cpp +++ b/service/test.cpp @@ -47,6 +47,11 @@ BOOST_AUTO_TEST_CASE(getServices) BOOST_CHECK(!ss.front()->urlxpath.empty()); } +BOOST_AUTO_TEST_CASE(getMatches_failure) +{ + BOOST_REQUIRE_THROW(s->getMatches("no.fixture"), MirrorSearch::XmlError); +} + BOOST_AUTO_TEST_CASE(getMatches_zstd_notfound) { auto ms = s->getMatches("zstd-1.3.3.tar.gz"); diff --git a/service/uptr.cpp b/service/uptr.cpp new file mode 100644 index 0000000..1669a69 --- /dev/null +++ b/service/uptr.cpp @@ -0,0 +1,26 @@ +#include "uptr.h" +#include +#include +#include +#include +#include + +namespace MirrorSearch { + + std::string + failingFunction(void * const func) + { + char ** fn = backtrace_symbols(&func, 1); + BOOST_ASSERT(fn); + BOOST_ASSERT(*fn); + std::string funcName(*fn); + free(fn); + return funcName; + } + + void + defaultErrorHandler(const std::string & fn) + { + throw std::runtime_error("Fatal error in " + fn); + } +} diff --git a/service/uptr.h b/service/uptr.h new file mode 100644 index 0000000..1de1f99 --- /dev/null +++ b/service/uptr.h @@ -0,0 +1,38 @@ +#ifndef MIRRORSEARCH_UPTR_H +#define MIRRORSEARCH_UPTR_H + +#include +#include + +namespace MirrorSearch { + typedef std::function OnError; + + std::string + failingFunction(void * const func); + + void + defaultErrorHandler(const std::string &); + + template using UPtr = std::unique_ptr; + + template + R * + make_unique(R * (*func)(P...), OnError onError, A ... p) + { + if (auto obj = func(p...)) { + return obj; + } + onError(failingFunction((void * const)func)); + throw std::runtime_error("Error handler did not throw"); + } + + template + UPtr + make_unique(R*(*get)(P...), void(*release)(R*), OnError onError, A ... p) + { + return std::unique_ptr(make_unique(get, onError, p...), release); + } +} + +#endif + -- cgit v1.2.3