PageRenderTime 9ms CodeModel.GetById 1ms app.highlight 6ms RepoModel.GetById 1ms app.codeStats 0ms

/Src/Dependencies/Boost/boost/asio/ssl/detail/openssl_init.hpp

http://hadesmem.googlecode.com/
C++ Header | 88 lines | 50 code | 17 blank | 21 comment | 1 complexity | 5dd42e29d4020f8e2f94e7087907aa98 MD5 | raw file
 1//
 2// ssl/detail/openssl_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 BOOST_ASIO_SSL_DETAIL_OPENSSL_INIT_HPP
12#define BOOST_ASIO_SSL_DETAIL_OPENSSL_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 <boost/asio/detail/config.hpp>
19#include <cstring>
20#include <boost/asio/detail/noncopyable.hpp>
21#include <boost/asio/detail/shared_ptr.hpp>
22
23#include <boost/asio/detail/push_options.hpp>
24
25namespace boost {
26namespace asio {
27namespace ssl {
28namespace detail {
29
30class openssl_init_base
31  : private noncopyable
32{
33protected:
34  // Class that performs the actual initialisation.
35  class do_init;
36
37  // Helper function to manage a do_init singleton. The static instance of the
38  // openssl_init object ensures that this function is always called before
39  // main, and therefore before any other threads can get started. The do_init
40  // instance must be static in this function to ensure that it gets
41  // initialised before any other global objects try to use it.
42  BOOST_ASIO_DECL static boost::asio::detail::shared_ptr<do_init> instance();
43};
44
45template <bool Do_Init = true>
46class openssl_init : private openssl_init_base
47{
48public:
49  // Constructor.
50  openssl_init()
51    : ref_(instance())
52  {
53    using namespace std; // For memmove.
54
55    // Ensure openssl_init::instance_ is linked in.
56    openssl_init* tmp = &instance_;
57    memmove(&tmp, &tmp, sizeof(openssl_init*));
58  }
59
60  // Destructor.
61  ~openssl_init()
62  {
63  }
64
65private:
66  // Instance to force initialisation of openssl at global scope.
67  static openssl_init instance_;
68
69  // Reference to singleton do_init object to ensure that openssl does not get
70  // cleaned up until the last user has finished with it.
71  boost::asio::detail::shared_ptr<do_init> ref_;
72};
73
74template <bool Do_Init>
75openssl_init<Do_Init> openssl_init<Do_Init>::instance_;
76
77} // namespace detail
78} // namespace ssl
79} // namespace asio
80} // namespace boost
81
82#include <boost/asio/detail/pop_options.hpp>
83
84#if defined(BOOST_ASIO_HEADER_ONLY)
85# include <boost/asio/ssl/detail/impl/openssl_init.ipp>
86#endif // defined(BOOST_ASIO_HEADER_ONLY)
87
88#endif // BOOST_ASIO_SSL_DETAIL_OPENSSL_INIT_HPP