Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011 #ifndef ASIO_DETAIL_WINSOCK_INIT_HPP
00012 #define ASIO_DETAIL_WINSOCK_INIT_HPP
00013
00014 #if defined(_MSC_VER) && (_MSC_VER >= 1200)
00015 # pragma once
00016 #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
00017
00018 #include "asio/detail/push_options.hpp"
00019
00020 #include "asio/detail/push_options.hpp"
00021 #include <boost/config.hpp>
00022 #include "asio/detail/pop_options.hpp"
00023
00024 #if defined(BOOST_WINDOWS) || defined(__CYGWIN__)
00025
00026 #include "asio/detail/push_options.hpp"
00027 #include <boost/shared_ptr.hpp>
00028 #include <boost/throw_exception.hpp>
00029 #include "asio/detail/pop_options.hpp"
00030
00031 #include "asio/error.hpp"
00032 #include "asio/system_error.hpp"
00033 #include "asio/detail/noncopyable.hpp"
00034 #include "asio/detail/socket_types.hpp"
00035
00036 namespace asio {
00037 namespace detail {
00038
00039 template <int Major = 2, int Minor = 0>
00040 class winsock_init
00041 : private noncopyable
00042 {
00043 private:
00044
00045 struct do_init
00046 {
00047 do_init()
00048 {
00049 WSADATA wsa_data;
00050 result_ = ::WSAStartup(MAKEWORD(Major, Minor), &wsa_data);
00051 }
00052
00053 ~do_init()
00054 {
00055 ::WSACleanup();
00056 }
00057
00058 int result() const
00059 {
00060 return result_;
00061 }
00062
00063
00064
00065
00066
00067
00068 static boost::shared_ptr<do_init> instance()
00069 {
00070 static boost::shared_ptr<do_init> init(new do_init);
00071 return init;
00072 }
00073
00074 private:
00075 int result_;
00076 };
00077
00078 public:
00079
00080 winsock_init()
00081 : ref_(do_init::instance())
00082 {
00083
00084
00085
00086 if (this != &instance_ && ref_->result() != 0)
00087 {
00088 asio::system_error e(
00089 asio::error_code(ref_->result(),
00090 asio::error::get_system_category()),
00091 "winsock");
00092 boost::throw_exception(e);
00093 }
00094 }
00095
00096
00097 ~winsock_init()
00098 {
00099 }
00100
00101 private:
00102
00103 static winsock_init instance_;
00104
00105
00106
00107 boost::shared_ptr<do_init> ref_;
00108 };
00109
00110 template <int Major, int Minor>
00111 winsock_init<Major, Minor> winsock_init<Major, Minor>::instance_;
00112
00113 }
00114 }
00115
00116 #endif // defined(BOOST_WINDOWS) || defined(__CYGWIN__)
00117
00118 #include "asio/detail/pop_options.hpp"
00119
00120 #endif // ASIO_DETAIL_WINSOCK_INIT_HPP