]> jfr.im git - irc/gameservirc.git/blob - gameserv-2.0/asio/asio/detail/winsock_init.hpp
Added the asio framework to start developing a GameServ server
[irc/gameservirc.git] / gameserv-2.0 / asio / asio / detail / winsock_init.hpp
1 //
2 // detail/winsock_init.hpp
3 // ~~~~~~~~~~~~~~~~~~~~~~~
4 //
5 // Copyright (c) 2003-2011 Christopher M. Kohlhoff (chris at kohlhoff dot com)
6 //
7 // Distributed under the Boost Software License, Version 1.0. (See accompanying
8 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
9 //
10
11 #ifndef ASIO_DETAIL_WINSOCK_INIT_HPP
12 #define ASIO_DETAIL_WINSOCK_INIT_HPP
13
14 #if defined(_MSC_VER) && (_MSC_VER >= 1200)
15 # pragma once
16 #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
17
18 #include "asio/detail/config.hpp"
19
20 #if defined(BOOST_WINDOWS) || defined(__CYGWIN__)
21
22 #include "asio/detail/push_options.hpp"
23
24 namespace asio {
25 namespace detail {
26
27 class winsock_init_base
28 {
29 protected:
30 // Structure to track result of initialisation and number of uses. POD is used
31 // to ensure that the values are zero-initialised prior to any code being run.
32 struct data
33 {
34 long init_count_;
35 long result_;
36 };
37
38 ASIO_DECL static void startup(data& d,
39 unsigned char major, unsigned char minor);
40
41 ASIO_DECL static void cleanup(data& d);
42
43 ASIO_DECL static void throw_on_error(data& d);
44 };
45
46 template <int Major = 2, int Minor = 0>
47 class winsock_init : private winsock_init_base
48 {
49 public:
50 winsock_init(bool allow_throw = true)
51 {
52 startup(data_, Major, Minor);
53 if (allow_throw)
54 throw_on_error(data_);
55 }
56
57 winsock_init(const winsock_init&)
58 {
59 startup(data_, Major, Minor);
60 throw_on_error(data_);
61 }
62
63 ~winsock_init()
64 {
65 cleanup(data_);
66 }
67
68 private:
69 static data data_;
70 };
71
72 template <int Major, int Minor>
73 winsock_init_base::data winsock_init<Major, Minor>::data_;
74
75 // Static variable to ensure that winsock is initialised before main, and
76 // therefore before any other threads can get started.
77 static const winsock_init<>& winsock_init_instance = winsock_init<>(false);
78
79 } // namespace detail
80 } // namespace asio
81
82 #include "asio/detail/pop_options.hpp"
83
84 #if defined(ASIO_HEADER_ONLY)
85 # include "asio/detail/impl/winsock_init.ipp"
86 #endif // defined(ASIO_HEADER_ONLY)
87
88 #endif // defined(BOOST_WINDOWS) || defined(__CYGWIN__)
89
90 #endif // ASIO_DETAIL_WINSOCK_INIT_HPP