attribute_name.hpp 5.7 KB

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