/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. #ifndef BOOST_ASIO_SSL_DETAIL_OPENSSL_INIT_HPP
  11. #define BOOST_ASIO_SSL_DETAIL_OPENSSL_INIT_HPP
  12. #if defined(_MSC_VER) && (_MSC_VER >= 1200)
  13. # pragma once
  14. #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
  15. #include <boost/asio/detail/config.hpp>
  16. #include <cstring>
  17. #include <boost/asio/detail/noncopyable.hpp>
  18. #include <boost/asio/detail/shared_ptr.hpp>
  19. #include <boost/asio/detail/push_options.hpp>
  20. namespace boost {
  21. namespace asio {
  22. namespace ssl {
  23. namespace detail {
  24. class openssl_init_base
  25. : private noncopyable
  26. {
  27. protected:
  28. // Class that performs the actual initialisation.
  29. class do_init;
  30. // Helper function to manage a do_init singleton. The static instance of the
  31. // openssl_init object ensures that this function is always called before
  32. // main, and therefore before any other threads can get started. The do_init
  33. // instance must be static in this function to ensure that it gets
  34. // initialised before any other global objects try to use it.
  35. BOOST_ASIO_DECL static boost::asio::detail::shared_ptr<do_init> instance();
  36. };
  37. template <bool Do_Init = true>
  38. class openssl_init : private openssl_init_base
  39. {
  40. public:
  41. // Constructor.
  42. openssl_init()
  43. : ref_(instance())
  44. {
  45. using namespace std; // For memmove.
  46. // Ensure openssl_init::instance_ is linked in.
  47. openssl_init* tmp = &instance_;
  48. memmove(&tmp, &tmp, sizeof(openssl_init*));
  49. }
  50. // Destructor.
  51. ~openssl_init()
  52. {
  53. }
  54. private:
  55. // Instance to force initialisation of openssl at global scope.
  56. static openssl_init instance_;
  57. // Reference to singleton do_init object to ensure that openssl does not get
  58. // cleaned up until the last user has finished with it.
  59. boost::asio::detail::shared_ptr<do_init> ref_;
  60. };
  61. template <bool Do_Init>
  62. openssl_init<Do_Init> openssl_init<Do_Init>::instance_;
  63. } // namespace detail
  64. } // namespace ssl
  65. } // namespace asio
  66. } // namespace boost
  67. #include <boost/asio/detail/pop_options.hpp>
  68. #if defined(BOOST_ASIO_HEADER_ONLY)
  69. # include <boost/asio/ssl/detail/impl/openssl_init.ipp>
  70. #endif // defined(BOOST_ASIO_HEADER_ONLY)
  71. #endif // BOOST_ASIO_SSL_DETAIL_OPENSSL_INIT_HPP