1
0
Fork 0
mirror of https://github.com/OpenMW/openmw.git synced 2025-02-01 01:45:36 +00:00
openmw/components/esm/refid.hpp
florent.teppe 65cdd489fb create a specific esm reader function for RefID to avoid allocation for string and then again for RefId
Fixed some types

removed useless header

applied clang format

fixed compile tests

fixed clang tidy, and closer to logic before this MR

Removed hardcoded refids

unless there is a returned value we don't use static RefIds
can use == between RefId and hardcoded string

Fix clang format

Fixed a few instances where std::string was used, when only const std::string& was needed

removed unused variable
2022-12-27 19:15:57 +01:00

51 lines
1.5 KiB
C++

#ifndef OPENMW_COMPONENTS_ESM_REFID_HPP
#define OPENMW_COMPONENTS_ESM_REFID_HPP
#include <compare>
#include <functional>
#include <iosfwd>
#include <string>
#include <string_view>
namespace ESM
{
struct RefId
{
const static RefId sEmpty;
bool empty() const { return mId.empty(); }
void swap(RefId& rhs) { mId.swap(rhs.mId); }
bool operator==(const RefId& rhs) const;
bool operator<(const RefId& rhs) const;
bool operator>(const RefId& rhs) const;
friend std::ostream& operator<<(std::ostream& os, const RefId& dt);
// The 2 following functions are used to move back and forth between string and RefID. Used for hard coded
// RefIds that are as string in the code. For serialization, and display. Using explicit conversions make it
// very clear where in the code we need to convert from string to RefId and Vice versa.
static RefId stringRefId(std::string_view id);
const std::string& getRefIdString() const { return mId; }
private:
std::string mId;
bool operator==(std::string_view rhs) const;
public:
template <std::size_t size>
bool operator==(const char (&rhs)[size]) const
{
return *this == std::string_view(rhs);
}
};
}
namespace std
{
template <>
struct hash<ESM::RefId>
{
std::size_t operator()(const ESM::RefId& k) const { return std::hash<std::string>()(k.getRefIdString()); }
};
}
#endif