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: 2023-01-19 00:18:29 Functions: 3 4 75.0 %

          Line data    Source code
       1             : /* Copyright (C) 2021 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             : class CStrInternInternals;
      22             : 
      23             : /**
      24             :  * Interned 8-bit strings.
      25             :  * Each instance with the same string content is a pointer to the same piece of
      26             :  * memory, allowing very fast string comparisons.
      27             :  *
      28             :  * Since a CStrIntern is just a dumb pointer, copying is very fast,
      29             :  * and pass-by-value should be preferred over pass-by-reference.
      30             :  *
      31             :  * Memory allocated for strings will never be freed, so don't use this for
      32             :  * unbounded numbers of strings (e.g. text rendered by gameplay scripts) -
      33             :  * it's intended for a small number of short frequently-used strings.
      34             :  *
      35             :  * Not thread-safe - only allocate these strings from the main thread.
      36             :  */
      37             : class CStrIntern
      38             : {
      39             : public:
      40             :     CStrIntern();
      41             :     explicit CStrIntern(const char* str);
      42             :     explicit CStrIntern(const std::string& str);
      43             : 
      44             :     /**
      45             :      * Returns cached FNV1-A hash of the string.
      46             :      */
      47             :     u32 GetHash() const;
      48             : 
      49             :     /**
      50             :      * Returns null-terminated string.
      51             :      */
      52             :     const char* c_str() const;
      53             : 
      54             :     /**
      55             :      * Returns length of string in bytes.
      56             :      */
      57             :     size_t length() const;
      58             : 
      59             :     bool empty() const;
      60             : 
      61             :     /**
      62             :      * Returns as std::string.
      63             :      */
      64             :     const std::string& string() const;
      65             : 
      66             :     /**
      67             :      * String equality.
      68             :      */
      69        3168 :     bool operator==(const CStrIntern& b) const
      70             :     {
      71        3168 :         return m == b.m;
      72             :     }
      73             : 
      74           0 :     bool operator!=(const CStrIntern& b) const
      75             :     {
      76           0 :         return m != b.m;
      77             :     }
      78             : 
      79             :     /**
      80             :      * Compare with some arbitrary total order.
      81             :      * (In particular, this is not alphabetic order,
      82             :      * and is not consistent between runs of the game.)
      83             :      */
      84          48 :     bool operator<(const CStrIntern& b) const
      85             :     {
      86          48 :         return m < b.m;
      87             :     }
      88             : 
      89             : private:
      90             :     CStrInternInternals* m;
      91             : };
      92             : 
      93             : namespace std
      94             : {
      95             : template<>
      96             : struct hash<CStrIntern>
      97             : {
      98        3221 :     std::size_t operator()(const CStrIntern& str) const
      99             :     {
     100        3221 :         return str.GetHash();
     101             :     }
     102             : };
     103             : }
     104             : 
     105             : #endif // INCLUDED_CSTRINTERN

Generated by: LCOV version 1.13