Goose  Artifact [e3d5cd5a50]

Artifact e3d5cd5a507238e42e2d5568fe59dcc0eaecd255b581888d18e4702ad36669f0:

  • File bs/util/stringid.h — part of check-in [932f41fd6a] at 2019-08-20 02:09:56 on branch trunk —
    • Implemented the ct_char type.
    • Implemented character literals.
    • Replaced the ridiculous hash based stringid implementation to just use string interning instead, which is something I remembered exists (duh)
    (user: achavasse size: 2230)

#ifndef EMPATHY_UTIL_STRINGID_H
#define EMPATHY_UTIL_STRINGID_H

namespace empathy::util
{
    class StringId
    {
        public:
            StringId() {}
            explicit StringId( const string& str );
            explicit StringId( string&& str );

            explicit StringId( const char* pString ) :
                StringId( string( pString ) )
            {}

            explicit StringId( const char* pString, size_t size ) :
                StringId( string( pString, size ) )
            {}

            explicit StringId( uint32_t uniqueId ) :
                StringId( format( "${0:8x}", uniqueId ) )
            {}

            constexpr auto operator==( const StringId& rhs ) const
            {
                return m_pString == rhs.m_pString;
            }

            constexpr auto operator!=( const StringId& rhs ) const
            {
                return m_pString != rhs.m_pString;
            }

            constexpr auto operator<( const StringId& rhs ) const
            {
                return m_pString < rhs.m_pString;
            }

            constexpr auto operator>=( const StringId& rhs ) const
            {
                return m_pString >= rhs.m_pString;
            }

            constexpr auto operator<=( const StringId& rhs ) const
            {
                return m_pString <= rhs.m_pString;
            }

            constexpr auto operator>( const StringId& rhs ) const
            {
                return m_pString > rhs.m_pString;
            }

            const char* c_str() const { return m_pString->c_str(); }

            friend ostream& operator<<( ostream& out, const StringId& sid )
            {
                return out << *sid.m_pString;
            }

        private:
            const string* m_pString = nullptr;
            static unordered_set< string > m_strings;
    };
}

namespace std
{
    template<> struct hash< empathy::util::StringId >
    {
        size_t operator()( const empathy::util::StringId& x ) const
        {
            return hash< const char* >()( x.c_str() );
        }
    };
}

static inline auto operator "" _sid( const char* pString, std::size_t s )
{
    return empathy::util::StringId( pString, s );
}

#endif