~ubuntu-branches/ubuntu/intrepid/miro/intrepid

« back to all changes in this revision

Viewing changes to portable/libtorrent/include/libtorrent/asio/detail/winsock_init.hpp

  • Committer: Bazaar Package Importer
  • Author(s): Christopher James Halse Rogers
  • Date: 2008-02-09 13:37:10 UTC
  • mfrom: (1.1.2 upstream)
  • Revision ID: james.westby@ubuntu.com-20080209133710-9rs90q6gckvp1b6i
Tags: 1.1.2-0ubuntu1
New upstream release

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
//
 
2
// winsock_init.hpp
 
3
// ~~~~~~~~~~~~~~~~
 
4
//
 
5
// Copyright (c) 2003-2007 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/push_options.hpp"
 
19
 
 
20
#include "asio/detail/push_options.hpp"
 
21
#include <boost/config.hpp>
 
22
#include "asio/detail/pop_options.hpp"
 
23
 
 
24
#if defined(BOOST_WINDOWS) || defined(__CYGWIN__)
 
25
 
 
26
#include "asio/detail/push_options.hpp"
 
27
#include <boost/shared_ptr.hpp>
 
28
#include <boost/throw_exception.hpp>
 
29
#include "asio/detail/pop_options.hpp"
 
30
 
 
31
#include "asio/error.hpp"
 
32
#include "asio/system_error.hpp"
 
33
#include "asio/detail/noncopyable.hpp"
 
34
#include "asio/detail/socket_types.hpp"
 
35
 
 
36
namespace asio {
 
37
namespace detail {
 
38
 
 
39
template <int Major = 2, int Minor = 0>
 
40
class winsock_init
 
41
  : private noncopyable
 
42
{
 
43
private:
 
44
  // Structure to perform the actual initialisation.
 
45
  struct do_init
 
46
  {
 
47
    do_init()
 
48
    {
 
49
      WSADATA wsa_data;
 
50
      result_ = ::WSAStartup(MAKEWORD(Major, Minor), &wsa_data);
 
51
    }
 
52
 
 
53
    ~do_init()
 
54
    {
 
55
      ::WSACleanup();
 
56
    }
 
57
 
 
58
    int result() const
 
59
    {
 
60
      return result_;
 
61
    }
 
62
 
 
63
    // Helper function to manage a do_init singleton. The static instance of the
 
64
    // winsock_init object ensures that this function is always called before
 
65
    // main, and therefore before any other threads can get started. The do_init
 
66
    // instance must be static in this function to ensure that it gets
 
67
    // initialised before any other global objects try to use it.
 
68
    static boost::shared_ptr<do_init> instance()
 
69
    {
 
70
      static boost::shared_ptr<do_init> init(new do_init);
 
71
      return init;
 
72
    }
 
73
 
 
74
  private:
 
75
    int result_;
 
76
  };
 
77
 
 
78
public:
 
79
  // Constructor.
 
80
  winsock_init()
 
81
    : ref_(do_init::instance())
 
82
  {
 
83
    // Check whether winsock was successfully initialised. This check is not
 
84
    // performed for the global instance since there will be nobody around to
 
85
    // catch the exception.
 
86
    if (this != &instance_ && ref_->result() != 0)
 
87
    {
 
88
      asio::system_error e(
 
89
          asio::error_code(ref_->result(),
 
90
            asio::error::system_category),
 
91
          "winsock");
 
92
      boost::throw_exception(e);
 
93
    }
 
94
  }
 
95
 
 
96
  // Destructor.
 
97
  ~winsock_init()
 
98
  {
 
99
  }
 
100
 
 
101
private:
 
102
  // Instance to force initialisation of winsock at global scope.
 
103
  static winsock_init instance_;
 
104
 
 
105
  // Reference to singleton do_init object to ensure that winsock does not get
 
106
  // cleaned up until the last user has finished with it.
 
107
  boost::shared_ptr<do_init> ref_;
 
108
};
 
109
 
 
110
template <int Major, int Minor>
 
111
winsock_init<Major, Minor> winsock_init<Major, Minor>::instance_;
 
112
 
 
113
} // namespace detail
 
114
} // namespace asio
 
115
 
 
116
#endif // defined(BOOST_WINDOWS) || defined(__CYGWIN__)
 
117
 
 
118
#include "asio/detail/pop_options.hpp"
 
119
 
 
120
#endif // ASIO_DETAIL_WINSOCK_INIT_HPP