1 // 2 // detail/null_global.hpp 3 // ~~~~~~~~~~~~~~~~~~~~~~ 4 // 5 // Copyright (c) 2003-2019 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_DETAIL_NULL_GLOBAL_HPP 12 #define BOOST_ASIO_DETAIL_NULL_GLOBAL_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 20 #include <boost/asio/detail/push_options.hpp> 21 22 namespace boost { 23 namespace asio { 24 namespace detail { 25 26 template <typename T> 27 struct null_global_impl 28 { null_global_implboost::asio::detail::null_global_impl29 null_global_impl() 30 : ptr_(0) 31 { 32 } 33 34 // Destructor automatically cleans up the global. ~null_global_implboost::asio::detail::null_global_impl35 ~null_global_impl() 36 { 37 delete ptr_; 38 } 39 40 static null_global_impl instance_; 41 T* ptr_; 42 }; 43 44 template <typename T> 45 null_global_impl<T> null_global_impl<T>::instance_; 46 47 template <typename T> null_global()48T& null_global() 49 { 50 if (null_global_impl<T>::instance_.ptr_ == 0) 51 null_global_impl<T>::instance_.ptr_ = new T; 52 return *null_global_impl<T>::instance_.ptr_; 53 } 54 55 } // namespace detail 56 } // namespace asio 57 } // namespace boost 58 59 #include <boost/asio/detail/pop_options.hpp> 60 61 #endif // BOOST_ASIO_DETAIL_NULL_GLOBAL_HPP 62