libmoost
/home/mhx/git/github/libmoost/include/moost/murcl/global.hpp
Go to the documentation of this file.
00001 /* vim:set ts=3 sw=3 sts=3 et: */
00037 #ifndef FM_LAST_MOOST_MURCL_GLOBAL_HPP__
00038 #define FM_LAST_MOOST_MURCL_GLOBAL_HPP__
00039 
00040 #include <stdexcept>
00041 
00042 #include <curl/curl.h>
00043 
00044 #include <boost/shared_ptr.hpp>
00045 #include "../utils/singleton.hpp"
00046 
00047 namespace moost { namespace murcl {
00048 
00052 class global
00053 {
00054 template <typename T> friend
00055    class moost::utils::singleton_default<T>::friend_type;
00056 
00057 private:
00061    global()
00062       : code_(CURLE_FAILED_INIT)
00063    {
00064       // This bad-boy is instantiated as a singleton so it
00065       // absolutely must not throw during construction!
00066       try
00067       {
00068          // Since this call is to a C API is shouldn't throw...
00069          code_ = curl_global_init(CURL_GLOBAL_ALL);
00070       }
00071       catch(...)
00072       {
00073          // ...but just in case!
00074       }
00075    }
00076 
00077 public:
00078 
00086    bool isok() const
00087    {
00088       return (code_ == CURLE_OK);
00089    }
00090 
00096    CURLcode status_code() const
00097    {
00098       return code_;
00099    }
00100 
00104    ~global()
00105    {
00106       curl_global_cleanup();
00107    }
00108 
00109 private:
00110    CURLcode code_;
00111 };
00112 
00121 typedef moost::utils::singleton_default<global> global_singleton;
00122 
00123 }}
00124 
00125 #endif // FM_LAST_MOOST_MURCL_GLOBAL_HPP__