LCOV - code coverage report
Current view: top level - source/ps - CStrIntern.h (source / functions) Hit Total Coverage
Test: 0 A.D. test coverage report Lines: 6 8 75.0 %
Date: 2026-12-03 01:19:39 Functions: 0 0 -

          Line data    Source code
       1             : /* Copyright (C) 2026 Wildfire Games.
       2             :  * This file is part of 0 A.D.
       3             :  *
       4             :  * 0 A.D. is free software: you can redistribute it and/or modify
       5             :  * it under the terms of the GNU General Public License as published by
       6             :  * the Free Software Foundation, either version 2 of the License, or
       7             :  * (at your option) any later version.
       8             :  *
       9             :  * 0 A.D. is distributed in the hope that it will be useful,
      10             :  * but WITHOUT ANY WARRANTY; without even the implied warranty of
      11             :  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      12             :  * GNU General Public License for more details.
      13             :  *
      14             :  * You should have received a copy of the GNU General Public License
      15             :  * along with 0 A.D.  If not, see <http://www.gnu.org/licenses/>.
      16             :  */
      17             : 
      18             : #ifndef INCLUDED_CSTRINTERN
      19             : #define INCLUDED_CSTRINTERN
      20             : 
      21             : #include <cstddef>
      22             : #include <string>
      23             : #include <string_view>
      24             : 
      25             : class CStrInternInternals;
      26             : 
      27             : /**
      28             :  * Interned 8-bit strings.
      29             :  * Each instance with the same string content is a pointer to the same piece of
      30             :  * memory, allowing very fast string comparisons.
      31             :  *
      32             :  * Since a CStrIntern is just a dumb pointer, copying is very fast,
      33             :  * and pass-by-value should be preferred over pass-by-reference.
      34             :  *
      35             :  * Memory allocated for strings will never be freed, so don't use this for
      36             :  * unbounded numbers of strings (e.g. text rendered by gameplay scripts) -
      37             :  * it's intended for a small number of short frequently-used strings.
      38             :  *
      39             :  * Not thread-safe - only allocate these strings from the main thread.
      40             :  */
      41             : class CStrIntern
      42             : {
      43             : public:
      44             :     CStrIntern();
      45             :     explicit CStrIntern(const char* str);
      46             :     explicit CStrIntern(const std::string& str);
      47             :     explicit CStrIntern(const std::string_view str);
      48             : 
      49             :     /**
      50             :      * Returns cached FNV1-A hash of the string.
      51             :      */
      52             :     std::uint32_t GetHash() const;
      53             : 
      54             :     /**
      55             :      * Returns null-terminated string.
      56             :      */
      57             :     const char* c_str() const;
      58             : 
      59             :     /**
      60             :      * Returns length of string in bytes.
      61             :      */
      62             :     size_t length() const;
      63             : 
      64             :     bool empty() const;
      65             : 
      66             :     /**
      67             :      * Returns as std::string.
      68             :      */
      69             :     const std::string& string() const;
      70             : 
      71             :     /**
      72             :      * String equality.
      73             :      */
      74        2809 :     bool operator==(const CStrIntern& b) const
      75             :     {
      76        2742 :         return m == b.m;
      77             :     }
      78             : 
      79           0 :     bool operator!=(const CStrIntern& b) const
      80             :     {
      81           0 :         return m != b.m;
      82             :     }
      83             : 
      84             :     /**
      85             :      * Compare with some arbitrary total order.
      86             :      * (In particular, this is not alphabetic order,
      87             :      * and is not consistent between runs of the game.)
      88             :      */
      89         207 :     bool operator<(const CStrIntern& b) const
      90             :     {
      91         182 :         return m < b.m;
      92             :     }
      93             : 
      94             : private:
      95             :     CStrInternInternals* m;
      96             : };
      97             : 
      98             : namespace std
      99             : {
     100             : template<>
     101             : struct hash<CStrIntern>
     102             : {
     103        2744 :     std::size_t operator()(const CStrIntern& str) const
     104             :     {
     105        2748 :         return str.GetHash();
     106             :     }
     107             : };
     108             : }
     109             : 
     110             : #endif // INCLUDED_CSTRINTERN

Generated by: LCOV version 1.16