libmoost
/home/mhx/git/github/libmoost/include/moost/murcl/option_traits.hpp
Go to the documentation of this file.
00001 /* vim:set ts=3 sw=3 sts=3 et: */
00036 #ifndef FM_LAST_MOOST_MURCL_OPTION_TRAITS_HPP__
00037 #define FM_LAST_MOOST_MURCL_OPTION_TRAITS_HPP__
00038 
00039 #include <map>
00040 #include <stdexcept>
00041 #include <sstream>
00042 #include <curl/curl.h>
00043 
00044 namespace moost { namespace murcl {
00045 
00049 template <CURLoption optval>
00050 class curl_opt_base
00051 {
00052    protected:
00053       curl_opt_base() {}
00054 
00055    template <typename T>
00056    static void error(
00057       T const & val,
00058       std::string const & opttype,
00059       std::string const & msg
00060       )
00061    {
00062       std::ostringstream oss;
00063       oss
00064          << "Error setting " << opttype
00065          << " option '" << optval
00066          << "' to value '" << val
00067          << "': " << msg;
00068 
00069      throw std::runtime_error(oss.str());
00070    }
00071 };
00072 
00076 template <CURLoption optval>
00077 struct curl_easyopt: curl_opt_base<optval>
00078 {
00079    typedef CURL * handle_type;
00080    typedef CURLoption option_type;
00081    typedef CURLcode result_type;
00082    static const option_type option_value = optval;
00083 
00084    template <typename T>
00085    static void set(handle_type handle, T const & val)
00086    {
00087       result_type code = curl_easy_setopt(handle, option_value, val);
00088 
00089       if(code != CURLE_OK)
00090       {
00091          curl_opt_base<optval>::error(val, "easy", curl_easy_strerror(code));
00092       }
00093    }
00094 };
00095 
00099 template <CURLMoption optval>
00100 struct curl_multiopt: curl_opt_base<optval>
00101 {
00102    typedef CURLM * handle_type;
00103    typedef CURLMoption option_type;
00104    typedef CURLMcode result_type;
00105    static const option_type option_value = optval;
00106 
00107    template <typename T>
00108    static void set(handle_type handle, T const & val)
00109    {
00110       result_type code = curl_multi_setopt(handle, option_value, val);
00111 
00112       if(code != CURLM_OK)
00113       {
00114          curl_opt_base<optval>::error(val, "multi", curl_multi_strerror(code));
00115       }
00116    }
00117 };
00118 
00122 template <CURLSHoption optval>
00123 struct curl_shareopt: curl_opt_base<optval>
00124 {
00125    typedef CURLSH * handle_type;
00126    typedef CURLSHoption option_type;
00127    typedef CURLSHcode result_type;
00128    static const option_type option_value = optval;
00129 
00130    template <typename T>
00131    static void set(handle_type handle, T const & val)
00132    {
00133       result_type code = curl_easy_setopt(handle, option_value, val);
00134 
00135       if(code != CURLSHE_OK)
00136       {
00137          curl_opt_base<optval>::error(val, "share", curl_share_strerror(code));
00138       }
00139    }
00140 };
00141 
00147 template <typename O, typename T>
00148 struct option_traits
00149 {
00150    typedef O curl_option;
00151    typedef typename curl_option::handle_type handle_type;
00152    typedef typename curl_option::option_type option_type;
00153    typedef typename curl_option::result_type result_type;
00154    static const option_type option_value = curl_option::option_value;
00155    typedef T value_type;
00156 };
00157 
00158 }}
00159 
00160 #endif //FM_LAST_MOOST_MURCL_OPTION_TRAITS_HPP__