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 constant.hpp 9 * \author Andrey Semashev 10 * \date 15.04.2007 11 * 12 * The header contains implementation of a constant attribute. 13 */ 14 15 #ifndef BOOST_LOG_ATTRIBUTES_CONSTANT_HPP_INCLUDED_ 16 #define BOOST_LOG_ATTRIBUTES_CONSTANT_HPP_INCLUDED_ 17 18 #include <boost/move/core.hpp> 19 #include <boost/move/utility_core.hpp> 20 #include <boost/type_traits/remove_reference.hpp> 21 #include <boost/type_traits/is_nothrow_move_constructible.hpp> 22 #include <boost/log/detail/config.hpp> 23 #include <boost/log/detail/embedded_string_type.hpp> 24 #include <boost/log/attributes/attribute.hpp> 25 #include <boost/log/attributes/attribute_cast.hpp> 26 #include <boost/log/attributes/attribute_value_impl.hpp> 27 #include <boost/log/detail/header.hpp> 28 29 #ifdef BOOST_HAS_PRAGMA_ONCE 30 #pragma once 31 #endif 32 33 namespace boost { 34 35 BOOST_LOG_OPEN_NAMESPACE 36 37 namespace attributes { 38 39 /*! 40 * \brief A class of an attribute that holds a single constant value 41 * 42 * The constant is a simplest and one of the most frequently used types of attributes. 43 * It stores a constant value, which it eventually returns as its value each time 44 * requested. 45 */ 46 template< typename T > 47 class constant : 48 public attribute 49 { 50 public: 51 //! Attribute value type 52 typedef T value_type; 53 54 protected: 55 //! Factory implementation 56 class BOOST_SYMBOL_VISIBLE impl : 57 public attribute_value_impl< value_type > 58 { 59 //! Base type 60 typedef attribute_value_impl< value_type > base_type; 61 62 public: 63 /*! 64 * Constructor with the stored value initialization 65 */ impl(value_type const & value)66 explicit impl(value_type const& value) : base_type(value) {} 67 /*! 68 * Constructor with the stored value initialization 69 */ BOOST_NOEXCEPT_IF(boost::is_nothrow_move_constructible<value_type>::value)70 explicit impl(BOOST_RV_REF(value_type) value) BOOST_NOEXCEPT_IF(boost::is_nothrow_move_constructible< value_type >::value) : 71 base_type(boost::move(value)) 72 { 73 } 74 }; 75 76 public: 77 /*! 78 * Constructor with the stored value initialization 79 */ constant(value_type const & value)80 explicit constant(value_type const& value) : attribute(new impl(value)) {} 81 /*! 82 * Constructor with the stored value initialization 83 */ constant(BOOST_RV_REF (value_type)value)84 explicit constant(BOOST_RV_REF(value_type) value) : attribute(new impl(boost::move(value))) {} 85 /*! 86 * Constructor for casting support 87 */ constant(cast_source const & source)88 explicit constant(cast_source const& source) : attribute(source.as< impl >()) 89 { 90 } 91 92 /*! 93 * \return Reference to the contained value. 94 */ get() const95 value_type const& get() const 96 { 97 return static_cast< impl* >(this->get_impl())->get(); 98 } 99 }; 100 101 /*! 102 * The function constructs a \c constant attribute containing the provided value. 103 * The function automatically converts C string arguments to \c std::basic_string objects. 104 */ 105 template< typename T > 106 inline constant< 107 typename boost::log::aux::make_embedded_string_type< 108 typename remove_reference< T >::type 109 >::type make_constant(BOOST_FWD_REF (T)val)110> make_constant(BOOST_FWD_REF(T) val) 111 { 112 typedef typename boost::log::aux::make_embedded_string_type< 113 typename remove_reference< T >::type 114 >::type value_type; 115 return constant< value_type >(boost::forward< T >(val)); 116 } 117 118 } // namespace attributes 119 120 BOOST_LOG_CLOSE_NAMESPACE // namespace log 121 122 } // namespace boost 123 124 #include <boost/log/detail/footer.hpp> 125 126 #endif // BOOST_LOG_ATTRIBUTES_CONSTANT_HPP_INCLUDED_ 127