Chris@16: /* Chris@101: * Copyright Andrey Semashev 2007 - 2015. Chris@16: * Distributed under the Boost Software License, Version 1.0. Chris@16: * (See accompanying file LICENSE_1_0.txt or copy at Chris@16: * http://www.boost.org/LICENSE_1_0.txt) Chris@16: */ Chris@16: /*! Chris@16: * \file attribute_name.hpp Chris@16: * \author Andrey Semashev Chris@16: * \date 28.06.2010 Chris@16: * Chris@16: * The header contains attribute name interface definition. Chris@16: */ Chris@16: Chris@16: #ifndef BOOST_LOG_ATTRIBUTE_NAME_HPP_INCLUDED_ Chris@16: #define BOOST_LOG_ATTRIBUTE_NAME_HPP_INCLUDED_ Chris@16: Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: Chris@16: #ifdef BOOST_HAS_PRAGMA_ONCE Chris@16: #pragma once Chris@16: #endif Chris@16: Chris@16: namespace boost { Chris@16: Chris@16: BOOST_LOG_OPEN_NAMESPACE Chris@16: Chris@16: /*! Chris@16: * \brief The class represents an attribute name in containers used by the library Chris@16: * Chris@16: * The class mostly serves for optimization purposes. Each attribute name that is used Chris@16: * with the library is automatically associated with a unique identifier, which is much Chris@16: * lighter in terms of memory footprint and operations complexity. This is done Chris@16: * transparently by this class, on object construction. Passing objects of this class Chris@16: * to other library methods, such as attribute lookup functions, will not require Chris@16: * this translation and/or string copying and thus will result in a more efficient code. Chris@16: */ Chris@16: class attribute_name Chris@16: { Chris@16: public: Chris@16: //! String type Chris@16: typedef std::string string_type; Chris@16: #ifdef BOOST_LOG_DOXYGEN_PASS Chris@16: //! Associated identifier Chris@16: typedef unspecified id_type; Chris@16: #else Chris@16: typedef uint32_t id_type; Chris@16: Chris@16: private: Chris@16: enum { uninitialized = 0xFFFFFFFFu }; Chris@16: Chris@16: class repository; Chris@16: friend class repository; Chris@16: Chris@16: private: Chris@16: //! Associated identifier Chris@16: id_type m_id; Chris@16: #endif Chris@16: Chris@16: public: Chris@16: /*! Chris@16: * Default constructor. Creates an object that does not refer to any attribute name. Chris@16: */ Chris@16: BOOST_CONSTEXPR attribute_name() BOOST_NOEXCEPT : m_id(static_cast< id_type >(uninitialized)) Chris@16: { Chris@16: } Chris@16: /*! Chris@16: * Constructs an attribute name from the specified string Chris@16: * Chris@16: * \param name An attribute name Chris@16: * \pre \a name is not NULL and points to a zero-terminated string Chris@16: */ Chris@16: attribute_name(const char* name) : Chris@16: m_id(get_id_from_string(name)) Chris@16: { Chris@16: } Chris@16: /*! Chris@16: * Constructs an attribute name from the specified string Chris@16: * Chris@16: * \param name An attribute name Chris@16: */ Chris@16: attribute_name(string_type const& name) : Chris@16: m_id(get_id_from_string(name.c_str())) Chris@16: { Chris@16: } Chris@16: Chris@16: /*! Chris@16: * Compares the attribute names Chris@16: * Chris@16: * \return \c true if *this and \c that refer to the same attribute name, Chris@16: * and \c false otherwise. Chris@16: */ Chris@16: bool operator== (attribute_name const& that) const BOOST_NOEXCEPT { return m_id == that.m_id; } Chris@16: /*! Chris@16: * Compares the attribute names Chris@16: * Chris@16: * \return \c true if *this and \c that refer to different attribute names, Chris@16: * and \c false otherwise. Chris@16: */ Chris@16: bool operator!= (attribute_name const& that) const BOOST_NOEXCEPT { return m_id != that.m_id; } Chris@16: Chris@16: /*! Chris@16: * Compares the attribute names Chris@16: * Chris@16: * \return \c true if *this and \c that refer to the same attribute name, Chris@16: * and \c false otherwise. Chris@16: */ Chris@16: bool operator== (const char* that) const { return (m_id != static_cast< id_type >(uninitialized)) && (this->string() == that); } Chris@16: /*! Chris@16: * Compares the attribute names Chris@16: * Chris@16: * \return \c true if *this and \c that refer to different attribute names, Chris@16: * and \c false otherwise. Chris@16: */ Chris@16: bool operator!= (const char* that) const { return !operator== (that); } Chris@16: Chris@16: /*! Chris@16: * Compares the attribute names Chris@16: * Chris@16: * \return \c true if *this and \c that refer to the same attribute name, Chris@16: * and \c false otherwise. Chris@16: */ Chris@16: bool operator== (string_type const& that) const { return (m_id != static_cast< id_type >(uninitialized)) && (this->string() == that); } Chris@16: /*! Chris@16: * Compares the attribute names Chris@16: * Chris@16: * \return \c true if *this and \c that refer to different attribute names, Chris@16: * and \c false otherwise. Chris@16: */ Chris@16: bool operator!= (string_type const& that) const { return !operator== (that); } Chris@16: Chris@16: /*! Chris@16: * Checks if the object was default-constructed Chris@16: * Chris@16: * \return \c true if *this was constructed with an attribute name, \c false otherwise Chris@16: */ Chris@101: BOOST_EXPLICIT_OPERATOR_BOOL_NOEXCEPT() Chris@16: /*! Chris@16: * Checks if the object was default-constructed Chris@16: * Chris@16: * \return \c true if *this was default-constructed and does not refer to any attribute name, Chris@16: * \c false otherwise Chris@16: */ Chris@16: bool operator! () const BOOST_NOEXCEPT { return (m_id == static_cast< id_type >(uninitialized)); } Chris@16: Chris@16: /*! Chris@16: * \return The associated id value Chris@16: * \pre (!*this) == false Chris@16: */ Chris@16: id_type id() const BOOST_NOEXCEPT Chris@16: { Chris@16: BOOST_ASSERT(m_id != static_cast< id_type >(uninitialized)); Chris@16: return m_id; Chris@16: } Chris@16: /*! Chris@16: * \return The attribute name string that was used during the object construction Chris@16: * \pre (!*this) == false Chris@16: */ Chris@16: string_type const& string() const { return get_string_from_id(m_id); } Chris@16: Chris@16: private: Chris@16: #ifndef BOOST_LOG_DOXYGEN_PASS Chris@16: static BOOST_LOG_API id_type get_id_from_string(const char* name); Chris@16: static BOOST_LOG_API string_type const& get_string_from_id(id_type id); Chris@16: #endif Chris@16: }; Chris@16: Chris@16: template< typename CharT, typename TraitsT > Chris@16: BOOST_LOG_API std::basic_ostream< CharT, TraitsT >& operator<< ( Chris@16: std::basic_ostream< CharT, TraitsT >& strm, Chris@16: attribute_name const& name); Chris@16: Chris@16: BOOST_LOG_CLOSE_NAMESPACE // namespace log Chris@16: Chris@16: } // namespace boost Chris@16: Chris@16: #include Chris@16: Chris@16: #endif // BOOST_LOG_ATTRIBUTE_NAME_HPP_INCLUDED_