| 1 | /* |
| 2 | * Copyright Andrey Semashev 2007 - 2015. |
| 3 | * Distributed under the Boost Software License, Version 1.0. |
| 4 | * (See accompanying file LICENSE_1_0.txt or copy at |
| 5 | * http://www.boost.org/LICENSE_1_0.txt) |
| 6 | */ |
| 7 | /*! |
| 8 | * \file attribute_cast.hpp |
| 9 | * \author Andrey Semashev |
| 10 | * \date 06.08.2010 |
| 11 | * |
| 12 | * The header contains utilities for casting between attribute factories. |
| 13 | */ |
| 14 | |
| 15 | #ifndef BOOST_LOG_ATTRIBUTES_ATTRIBUTE_CAST_HPP_INCLUDED_ |
| 16 | #define BOOST_LOG_ATTRIBUTES_ATTRIBUTE_CAST_HPP_INCLUDED_ |
| 17 | |
| 18 | #include <boost/log/detail/config.hpp> |
| 19 | #include <boost/log/attributes/attribute.hpp> |
| 20 | #include <boost/log/detail/header.hpp> |
| 21 | |
| 22 | #ifdef BOOST_HAS_PRAGMA_ONCE |
| 23 | #pragma once |
| 24 | #endif |
| 25 | |
| 26 | namespace boost { |
| 27 | |
| 28 | BOOST_LOG_OPEN_NAMESPACE |
| 29 | |
| 30 | namespace attributes { |
| 31 | |
| 32 | /*! |
| 33 | * The class holds a reference to the attribute factory implementation being casted |
| 34 | */ |
| 35 | class cast_source |
| 36 | { |
| 37 | private: |
| 38 | attribute::impl* m_pImpl; |
| 39 | |
| 40 | public: |
| 41 | /*! |
| 42 | * Initializing constructor. Creates a source that refers to the specified factory implementation. |
| 43 | */ |
| 44 | explicit cast_source(attribute::impl* p) : m_pImpl(p) |
| 45 | { |
| 46 | } |
| 47 | |
| 48 | /*! |
| 49 | * The function attempts to cast the aggregated pointer to the implementation to the specified type. |
| 50 | * |
| 51 | * \return The converted pointer or \c NULL, if the conversion fails. |
| 52 | */ |
| 53 | template< typename T > |
| 54 | T* as() const { return dynamic_cast< T* >(m_pImpl); } |
| 55 | }; |
| 56 | |
| 57 | } // namespace attributes |
| 58 | |
| 59 | /*! |
| 60 | * The function casts one attribute factory to another |
| 61 | */ |
| 62 | template< typename T > |
| 63 | inline T attribute_cast(attribute const& attr) |
| 64 | { |
| 65 | return T(attributes::cast_source(attr.get_impl())); |
| 66 | } |
| 67 | |
| 68 | BOOST_LOG_CLOSE_NAMESPACE // namespace log |
| 69 | |
| 70 | } // namespace boost |
| 71 | |
| 72 | #include <boost/log/detail/footer.hpp> |
| 73 | |
| 74 | #endif // BOOST_LOG_ATTRIBUTES_ATTRIBUTE_CAST_HPP_INCLUDED_ |
| 75 | |