attribute_cast.hpp 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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. #ifndef BOOST_LOG_ATTRIBUTES_ATTRIBUTE_CAST_HPP_INCLUDED_
  15. #define BOOST_LOG_ATTRIBUTES_ATTRIBUTE_CAST_HPP_INCLUDED_
  16. #include <boost/log/detail/config.hpp>
  17. #include <boost/log/attributes/attribute.hpp>
  18. #include <boost/log/detail/header.hpp>
  19. #ifdef BOOST_HAS_PRAGMA_ONCE
  20. #pragma once
  21. #endif
  22. namespace boost {
  23. BOOST_LOG_OPEN_NAMESPACE
  24. namespace attributes {
  25. /*!
  26. * The class holds a reference to the attribute factory implementation being casted
  27. */
  28. class cast_source
  29. {
  30. private:
  31. attribute::impl* m_pImpl;
  32. public:
  33. /*!
  34. * Initializing constructor. Creates a source that refers to the specified factory implementation.
  35. */
  36. explicit cast_source(attribute::impl* p) : m_pImpl(p)
  37. {
  38. }
  39. /*!
  40. * The function attempts to cast the aggregated pointer to the implementation to the specified type.
  41. *
  42. * \return The converted pointer or \c NULL, if the conversion fails.
  43. */
  44. template< typename T >
  45. T* as() const { return dynamic_cast< T* >(m_pImpl); }
  46. };
  47. } // namespace attributes
  48. /*!
  49. * The function casts one attribute factory to another
  50. */
  51. template< typename T >
  52. inline T attribute_cast(attribute const& attr)
  53. {
  54. return T(attributes::cast_source(attr.get_impl()));
  55. }
  56. BOOST_LOG_CLOSE_NAMESPACE // namespace log
  57. } // namespace boost
  58. #include <boost/log/detail/footer.hpp>
  59. #endif // BOOST_LOG_ATTRIBUTES_ATTRIBUTE_CAST_HPP_INCLUDED_