attribute_name.hpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. /*
  2. * Copyright Andrey Semashev 2007 - 2013.
  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/log/detail/config.hpp>
  21. #include <boost/utility/explicit_operator_bool.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. enum { uninitialized = 0xFFFFFFFFu };
  50. class repository;
  51. friend class repository;
  52. private:
  53. //! Associated identifier
  54. id_type m_id;
  55. #endif
  56. public:
  57. /*!
  58. * Default constructor. Creates an object that does not refer to any attribute name.
  59. */
  60. BOOST_CONSTEXPR attribute_name() BOOST_NOEXCEPT : m_id(static_cast< id_type >(uninitialized))
  61. {
  62. }
  63. /*!
  64. * Constructs an attribute name from the specified string
  65. *
  66. * \param name An attribute name
  67. * \pre \a name is not NULL and points to a zero-terminated string
  68. */
  69. attribute_name(const char* name) :
  70. m_id(get_id_from_string(name))
  71. {
  72. }
  73. /*!
  74. * Constructs an attribute name from the specified string
  75. *
  76. * \param name An attribute name
  77. */
  78. attribute_name(string_type const& name) :
  79. m_id(get_id_from_string(name.c_str()))
  80. {
  81. }
  82. /*!
  83. * Compares the attribute names
  84. *
  85. * \return \c true if <tt>*this</tt> and \c that refer to the same attribute name,
  86. * and \c false otherwise.
  87. */
  88. bool operator== (attribute_name const& that) const BOOST_NOEXCEPT { return m_id == that.m_id; }
  89. /*!
  90. * Compares the attribute names
  91. *
  92. * \return \c true if <tt>*this</tt> and \c that refer to different attribute names,
  93. * and \c false otherwise.
  94. */
  95. bool operator!= (attribute_name const& that) const BOOST_NOEXCEPT { return m_id != that.m_id; }
  96. /*!
  97. * Compares the attribute names
  98. *
  99. * \return \c true if <tt>*this</tt> and \c that refer to the same attribute name,
  100. * and \c false otherwise.
  101. */
  102. bool operator== (const char* that) const { return (m_id != static_cast< id_type >(uninitialized)) && (this->string() == that); }
  103. /*!
  104. * Compares the attribute names
  105. *
  106. * \return \c true if <tt>*this</tt> and \c that refer to different attribute names,
  107. * and \c false otherwise.
  108. */
  109. bool operator!= (const char* that) const { return !operator== (that); }
  110. /*!
  111. * Compares the attribute names
  112. *
  113. * \return \c true if <tt>*this</tt> and \c that refer to the same attribute name,
  114. * and \c false otherwise.
  115. */
  116. bool operator== (string_type const& that) const { return (m_id != static_cast< id_type >(uninitialized)) && (this->string() == that); }
  117. /*!
  118. * Compares the attribute names
  119. *
  120. * \return \c true if <tt>*this</tt> and \c that refer to different attribute names,
  121. * and \c false otherwise.
  122. */
  123. bool operator!= (string_type const& that) const { return !operator== (that); }
  124. /*!
  125. * Checks if the object was default-constructed
  126. *
  127. * \return \c true if <tt>*this</tt> was constructed with an attribute name, \c false otherwise
  128. */
  129. BOOST_EXPLICIT_OPERATOR_BOOL()
  130. /*!
  131. * Checks if the object was default-constructed
  132. *
  133. * \return \c true if <tt>*this</tt> was default-constructed and does not refer to any attribute name,
  134. * \c false otherwise
  135. */
  136. bool operator! () const BOOST_NOEXCEPT { return (m_id == static_cast< id_type >(uninitialized)); }
  137. /*!
  138. * \return The associated id value
  139. * \pre <tt>(!*this) == false</tt>
  140. */
  141. id_type id() const BOOST_NOEXCEPT
  142. {
  143. BOOST_ASSERT(m_id != static_cast< id_type >(uninitialized));
  144. return m_id;
  145. }
  146. /*!
  147. * \return The attribute name string that was used during the object construction
  148. * \pre <tt>(!*this) == false</tt>
  149. */
  150. string_type const& string() const { return get_string_from_id(m_id); }
  151. private:
  152. #ifndef BOOST_LOG_DOXYGEN_PASS
  153. static BOOST_LOG_API id_type get_id_from_string(const char* name);
  154. static BOOST_LOG_API string_type const& get_string_from_id(id_type id);
  155. #endif
  156. };
  157. template< typename CharT, typename TraitsT >
  158. BOOST_LOG_API std::basic_ostream< CharT, TraitsT >& operator<< (
  159. std::basic_ostream< CharT, TraitsT >& strm,
  160. attribute_name const& name);
  161. BOOST_LOG_CLOSE_NAMESPACE // namespace log
  162. } // namespace boost
  163. #include <boost/log/detail/footer.hpp>
  164. #endif // BOOST_LOG_ATTRIBUTE_NAME_HPP_INCLUDED_