diff options
author | Dan Goodliffe <daniel.goodliffe@pressassociation.com> | 2015-09-01 11:41:36 +0100 |
---|---|---|
committer | Dan Goodliffe <daniel.goodliffe@pressassociation.com> | 2015-09-01 16:56:11 +0100 |
commit | 6a5b61396b48f27ca9bc0ee12a2fe6717f67dfd2 (patch) | |
tree | de8f4091e33e1c4f05c84fc290fea02c914036a1 | |
parent | No auto in lambda parameters (diff) | |
download | libadhocutil-6a5b61396b48f27ca9bc0ee12a2fe6717f67dfd2.tar.bz2 libadhocutil-6a5b61396b48f27ca9bc0ee12a2fe6717f67dfd2.tar.xz libadhocutil-6a5b61396b48f27ca9bc0ee12a2fe6717f67dfd2.zip |
Improved wrappers for setopt and getinfo
-rw-r--r-- | libadhocutil/curlHandle.cpp | 42 | ||||
-rw-r--r-- | libadhocutil/curlHandle.h | 20 | ||||
-rw-r--r-- | libadhocutil/unittests/testCurl.cpp | 26 |
3 files changed, 85 insertions, 3 deletions
diff --git a/libadhocutil/curlHandle.cpp b/libadhocutil/curlHandle.cpp index 7912af7..6192c70 100644 --- a/libadhocutil/curlHandle.cpp +++ b/libadhocutil/curlHandle.cpp @@ -1,5 +1,6 @@ #include "curlHandle.h" #include <net.h> +#include <boost/numeric/conversion/cast.hpp> static void cleanup() __attribute__((destructor)); static void cleanup() @@ -27,12 +28,53 @@ CurlHandle::~CurlHandle() curl_easy_cleanup(curl_handle); } +template <> void CurlHandle::setopt(CURLoption opt, const void * val) { curl_easy_setopt(curl_handle, opt, val); } +template <> +void +CurlHandle::setopt(CURLoption opt, int val) +{ + curl_easy_setopt(curl_handle, opt, val); +} + +template <> +void +CurlHandle::setopt(CURLoption opt, long val) +{ + curl_easy_setopt(curl_handle, opt, val); +} + +void +CurlHandle::getinfo(CURLINFO info, long & val) const +{ + curl_easy_getinfo(curl_handle, info, &val); +} + +void +CurlHandle::getinfo(CURLINFO info, int & ival) const +{ + long val; + curl_easy_getinfo(curl_handle, info, &val); + ival = boost::numeric_cast<int>(val); +} + +void +CurlHandle::getinfo(CURLINFO info, double & val) const +{ + curl_easy_getinfo(curl_handle, info, &val); +} + +void +CurlHandle::getinfo(CURLINFO info, char * & val) const +{ + curl_easy_getinfo(curl_handle, info, &val); +} + void CurlHandle::appendHeader(const char * header) { diff --git a/libadhocutil/curlHandle.h b/libadhocutil/curlHandle.h index b5a5d63..0bdcd11 100644 --- a/libadhocutil/curlHandle.h +++ b/libadhocutil/curlHandle.h @@ -13,8 +13,12 @@ class DLL_PUBLIC CurlHandle : public virtual IntrusivePtrBase { CurlHandle(const CurlHandle &) = delete; void operator=(const CurlHandle &) = delete; - void setopt(CURLoption opt, const void * val); - void getinto(CURLoption opt, void * & val); + template <typename T> + void setopt(CURLoption opt, const T val); + void getinfo(CURLINFO info, long & val) const; + void getinfo(CURLINFO info, int & val) const; + void getinfo(CURLINFO info, double & val) const; + void getinfo(CURLINFO info, char * & val) const; void appendHeader(const char *); void appendPost(const char *, const char *); void perform(); @@ -30,5 +34,17 @@ class DLL_PUBLIC CurlHandle : public virtual IntrusivePtrBase { }; typedef boost::intrusive_ptr<CurlHandle> CurlHandlePtr; +template <> +void CurlHandle::setopt(CURLoption opt, const void * val); +template <> +void CurlHandle::setopt(CURLoption opt, int val); +template <> +void CurlHandle::setopt(CURLoption opt, long val); +template <typename T> +void CurlHandle::setopt(CURLoption opt, const T val) +{ + setopt(opt, (const void *)val); +} + #endif diff --git a/libadhocutil/unittests/testCurl.cpp b/libadhocutil/unittests/testCurl.cpp index 0aed488..5365f58 100644 --- a/libadhocutil/unittests/testCurl.cpp +++ b/libadhocutil/unittests/testCurl.cpp @@ -17,10 +17,34 @@ BOOST_AUTO_TEST_CASE( fetch_file ) { auto url = "file://" + rootDir.string() + "/testCurl.cpp"; CurlHandle ch(url); - ch.setopt(CURLOPT_WRITEFUNCTION, (void*)&discard); + ch.setopt(CURLOPT_WRITEFUNCTION, discard); ch.perform(); } +BOOST_AUTO_TEST_CASE( setAndGetOptions ) +{ + auto url = "file://" + rootDir.string() + "/testCurl.cpp"; + CurlHandle ch(url); + // function + ch.setopt(CURLOPT_WRITEFUNCTION, discard); + // object + ch.setopt(CURLOPT_WRITEDATA, this); + // int + ch.setopt(CURLOPT_LOCALPORT, 8000); + // long + ch.setopt(CURLOPT_LOCALPORT, 8000L); + ch.perform(); + // char * + char * eurl; + ch.getinfo(CURLINFO_EFFECTIVE_URL, eurl); + BOOST_REQUIRE_EQUAL(url, eurl); + // double + double totalTime; + ch.getinfo(CURLINFO_TOTAL_TIME, totalTime); + BOOST_REQUIRE(totalTime > 0); + BOOST_REQUIRE(totalTime < 50); +} + BOOST_AUTO_TEST_CASE( fetch_missing ) { auto url = "file://" + rootDir.string() + "/nothere"; |