Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:28:45

0001 //
0002 // detail/std_global.hpp
0003 // ~~~~~~~~~~~~~~~~~~~~~
0004 //
0005 // Copyright (c) 2003-2023 Christopher M. Kohlhoff (chris at kohlhoff dot com)
0006 //
0007 // Distributed under the Boost Software License, Version 1.0. (See accompanying
0008 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
0009 //
0010 
0011 #ifndef BOOST_ASIO_DETAIL_STD_GLOBAL_HPP
0012 #define BOOST_ASIO_DETAIL_STD_GLOBAL_HPP
0013 
0014 #if defined(_MSC_VER) && (_MSC_VER >= 1200)
0015 # pragma once
0016 #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
0017 
0018 #include <boost/asio/detail/config.hpp>
0019 #include <exception>
0020 #include <mutex>
0021 
0022 #include <boost/asio/detail/push_options.hpp>
0023 
0024 namespace boost {
0025 namespace asio {
0026 namespace detail {
0027 
0028 template <typename T>
0029 struct std_global_impl
0030 {
0031   // Helper function to perform initialisation.
0032   static void do_init()
0033   {
0034     instance_.ptr_ = new T;
0035   }
0036 
0037   // Destructor automatically cleans up the global.
0038   ~std_global_impl()
0039   {
0040     delete ptr_;
0041   }
0042 
0043   static std::once_flag init_once_;
0044   static std_global_impl instance_;
0045   T* ptr_;
0046 };
0047 
0048 template <typename T>
0049 std::once_flag std_global_impl<T>::init_once_;
0050 
0051 template <typename T>
0052 std_global_impl<T> std_global_impl<T>::instance_;
0053 
0054 template <typename T>
0055 T& std_global()
0056 {
0057   std::call_once(std_global_impl<T>::init_once_, &std_global_impl<T>::do_init);
0058   return *std_global_impl<T>::instance_.ptr_;
0059 }
0060 
0061 } // namespace detail
0062 } // namespace asio
0063 } // namespace boost
0064 
0065 #include <boost/asio/detail/pop_options.hpp>
0066 
0067 #endif // BOOST_ASIO_DETAIL_STD_GLOBAL_HPP