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_name.hpp 9 * \author Andrey Semashev 10 * \date 28.06.2010 11 * 12 * The header contains attribute name interface definition. 13 */ 14 15 #ifndef BOOST_LOG_ATTRIBUTE_NAME_HPP_INCLUDED_ 16 #define BOOST_LOG_ATTRIBUTE_NAME_HPP_INCLUDED_ 17 18 #include <iosfwd> 19 #include <string> 20 #include <boost/assert.hpp> 21 #include <boost/cstdint.hpp> 22 #include <boost/core/explicit_operator_bool.hpp> 23 #include <boost/log/detail/config.hpp> 24 #include <boost/log/detail/header.hpp> 25 26 #ifdef BOOST_HAS_PRAGMA_ONCE 27 #pragma once 28 #endif 29 30 namespace boost { 31 32 BOOST_LOG_OPEN_NAMESPACE 33 34 /*! 35 * \brief The class represents an attribute name in containers used by the library 36 * 37 * The class mostly serves for optimization purposes. Each attribute name that is used 38 * with the library is automatically associated with a unique identifier, which is much 39 * lighter in terms of memory footprint and operations complexity. This is done 40 * transparently by this class, on object construction. Passing objects of this class 41 * to other library methods, such as attribute lookup functions, will not require 42 * this translation and/or string copying and thus will result in a more efficient code. 43 */ 44 class attribute_name 45 { 46 public: 47 //! String type 48 typedef std::string string_type; 49 #ifdef BOOST_LOG_DOXYGEN_PASS 50 //! Associated identifier 51 typedef unspecified id_type; 52 #else 53 typedef uint32_t id_type; 54 55 private: 56 class repository; 57 friend class repository; 58 59 private: 60 //! Associated identifier 61 id_type m_id; 62 //! A special identifier value indicating unassigned attribute name 63 static BOOST_CONSTEXPR_OR_CONST id_type uninitialized = ~static_cast< id_type >(0u); 64 #endif 65 66 public: 67 /*! 68 * Default constructor. Creates an object that does not refer to any attribute name. 69 */ attribute_name()70 BOOST_CONSTEXPR attribute_name() BOOST_NOEXCEPT : m_id(uninitialized) 71 { 72 } 73 /*! 74 * Constructs an attribute name from the specified string 75 * 76 * \param name An attribute name 77 * \pre \a name is not NULL and points to a zero-terminated string 78 */ attribute_name(const char * name)79 attribute_name(const char* name) : 80 m_id(get_id_from_string(name)) 81 { 82 } 83 /*! 84 * Constructs an attribute name from the specified string 85 * 86 * \param name An attribute name 87 */ attribute_name(string_type const & name)88 attribute_name(string_type const& name) : 89 m_id(get_id_from_string(name.c_str())) 90 { 91 } 92 93 /*! 94 * Compares the attribute names 95 * 96 * \return \c true if <tt>*this</tt> and \c that refer to the same attribute name, 97 * and \c false otherwise. 98 */ operator ==(attribute_name const & that) const99 bool operator== (attribute_name const& that) const BOOST_NOEXCEPT { return m_id == that.m_id; } 100 /*! 101 * Compares the attribute names 102 * 103 * \return \c true if <tt>*this</tt> and \c that refer to different attribute names, 104 * and \c false otherwise. 105 */ operator !=(attribute_name const & that) const106 bool operator!= (attribute_name const& that) const BOOST_NOEXCEPT { return m_id != that.m_id; } 107 108 /*! 109 * Compares the attribute names 110 * 111 * \return \c true if <tt>*this</tt> and \c that refer to the same attribute name, 112 * and \c false otherwise. 113 */ operator ==(const char * that) const114 bool operator== (const char* that) const { return (m_id != uninitialized) && (this->string() == that); } 115 /*! 116 * Compares the attribute names 117 * 118 * \return \c true if <tt>*this</tt> and \c that refer to different attribute names, 119 * and \c false otherwise. 120 */ operator !=(const char * that) const121 bool operator!= (const char* that) const { return !operator== (that); } 122 123 /*! 124 * Compares the attribute names 125 * 126 * \return \c true if <tt>*this</tt> and \c that refer to the same attribute name, 127 * and \c false otherwise. 128 */ operator ==(string_type const & that) const129 bool operator== (string_type const& that) const { return (m_id != uninitialized) && (this->string() == that); } 130 /*! 131 * Compares the attribute names 132 * 133 * \return \c true if <tt>*this</tt> and \c that refer to different attribute names, 134 * and \c false otherwise. 135 */ operator !=(string_type const & that) const136 bool operator!= (string_type const& that) const { return !operator== (that); } 137 138 /*! 139 * Checks if the object was default-constructed 140 * 141 * \return \c true if <tt>*this</tt> was constructed with an attribute name, \c false otherwise 142 */ 143 BOOST_EXPLICIT_OPERATOR_BOOL_NOEXCEPT() 144 /*! 145 * Checks if the object was default-constructed 146 * 147 * \return \c true if <tt>*this</tt> was default-constructed and does not refer to any attribute name, 148 * \c false otherwise 149 */ 150 bool operator! () const BOOST_NOEXCEPT { return (m_id == uninitialized); } 151 152 /*! 153 * \return The associated id value 154 * \pre <tt>(!*this) == false</tt> 155 */ id() const156 id_type id() const BOOST_NOEXCEPT 157 { 158 BOOST_ASSERT(m_id != uninitialized); 159 return m_id; 160 } 161 /*! 162 * \return The attribute name string that was used during the object construction 163 * \pre <tt>(!*this) == false</tt> 164 */ string() const165 string_type const& string() const { return get_string_from_id(m_id); } 166 167 private: 168 #ifndef BOOST_LOG_DOXYGEN_PASS 169 static BOOST_LOG_API id_type get_id_from_string(const char* name); 170 static BOOST_LOG_API string_type const& get_string_from_id(id_type id); 171 #endif 172 }; 173 174 template< typename CharT, typename TraitsT > 175 BOOST_LOG_API std::basic_ostream< CharT, TraitsT >& operator<< ( 176 std::basic_ostream< CharT, TraitsT >& strm, 177 attribute_name const& name); 178 179 BOOST_LOG_CLOSE_NAMESPACE // namespace log 180 181 } // namespace boost 182 183 #include <boost/log/detail/footer.hpp> 184 185 #endif // BOOST_LOG_ATTRIBUTE_NAME_HPP_INCLUDED_ 186