Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:39:18

0001 /*
0002  *          Copyright Andrey Semashev 2007 - 2015.
0003  * Distributed under the Boost Software License, Version 1.0.
0004  *    (See accompanying file LICENSE_1_0.txt or copy at
0005  *          http://www.boost.org/LICENSE_1_0.txt)
0006  */
0007 /*!
0008  * \file   current_thread_id.hpp
0009  * \author Andrey Semashev
0010  * \date   12.09.2009
0011  *
0012  * The header contains implementation of a current thread id attribute
0013  */
0014 
0015 #ifndef BOOST_LOG_ATTRIBUTES_CURRENT_THREAD_ID_HPP_INCLUDED_
0016 #define BOOST_LOG_ATTRIBUTES_CURRENT_THREAD_ID_HPP_INCLUDED_
0017 
0018 #include <boost/log/detail/config.hpp>
0019 
0020 #ifdef BOOST_HAS_PRAGMA_ONCE
0021 #pragma once
0022 #endif
0023 
0024 #if defined(BOOST_LOG_NO_THREADS)
0025 #error Boost.Log: The current_thread_id attribute is only available in multithreaded builds
0026 #endif
0027 
0028 #include <boost/smart_ptr/intrusive_ptr.hpp>
0029 #include <boost/log/detail/thread_id.hpp>
0030 #include <boost/log/attributes/attribute.hpp>
0031 #include <boost/log/attributes/attribute_cast.hpp>
0032 #include <boost/log/attributes/attribute_value_impl.hpp>
0033 #include <boost/log/detail/header.hpp>
0034 
0035 namespace boost {
0036 
0037 BOOST_LOG_OPEN_NAMESPACE
0038 
0039 //! Thread identifier type
0040 typedef boost::log::aux::thread::id thread_id;
0041 
0042 namespace attributes {
0043 
0044 /*!
0045  * \brief A class of an attribute that always returns the current thread identifier
0046  *
0047  * \note This attribute can be registered globally, it will still return the correct
0048  *       thread identifier, no matter which thread emits the log record.
0049  */
0050 class current_thread_id :
0051     public attribute
0052 {
0053 public:
0054     //! A held attribute value type
0055     typedef thread_id value_type;
0056 
0057 protected:
0058     //! Factory implementation
0059     class BOOST_SYMBOL_VISIBLE impl :
0060         public attribute_value::impl
0061     {
0062     public:
0063         bool dispatch(type_dispatcher& dispatcher)
0064         {
0065             type_dispatcher::callback< value_type > callback =
0066                 dispatcher.get_callback< value_type >();
0067             if (callback)
0068             {
0069                 callback(boost::log::aux::this_thread::get_id());
0070                 return true;
0071             }
0072             else
0073                 return false;
0074         }
0075 
0076         intrusive_ptr< attribute_value::impl > detach_from_thread()
0077         {
0078             typedef attribute_value_impl< value_type > detached_value;
0079             return new detached_value(boost::log::aux::this_thread::get_id());
0080         }
0081 
0082         typeindex::type_index get_type() const { return typeindex::type_id< value_type >(); }
0083     };
0084 
0085 public:
0086     /*!
0087      * Default constructor
0088      */
0089     current_thread_id() : attribute(new impl())
0090     {
0091     }
0092     /*!
0093      * Constructor for casting support
0094      */
0095     explicit current_thread_id(cast_source const& source) :
0096         attribute(source.as< impl >())
0097     {
0098     }
0099 };
0100 
0101 } // namespace attributes
0102 
0103 BOOST_LOG_CLOSE_NAMESPACE // namespace log
0104 
0105 } // namespace boost
0106 
0107 #include <boost/log/detail/footer.hpp>
0108 
0109 #endif // BOOST_LOG_ATTRIBUTES_CURRENT_THREAD_ID_HPP_INCLUDED_