| 1 | #ifndef ENGINE_SHARED_UUID_MANAGER_H |
| 2 | #define ENGINE_SHARED_UUID_MANAGER_H |
| 3 | |
| 4 | #include <cstdint> |
| 5 | #include <vector> |
| 6 | |
| 7 | class CUuid |
| 8 | { |
| 9 | public: |
| 10 | unsigned char m_aData[16]; |
| 11 | |
| 12 | bool operator==(const CUuid &Other) const; |
| 13 | bool operator!=(const CUuid &Other) const; |
| 14 | bool operator<(const CUuid &Other) const; |
| 15 | }; |
| 16 | |
| 17 | enum |
| 18 | { |
| 19 | UUID_MAXSTRSIZE = 37, // 12345678-0123-5678-0123-567890123456 |
| 20 | |
| 21 | UUID_INVALID = -2, |
| 22 | UUID_UNKNOWN = -1, |
| 23 | |
| 24 | UUID_INT_SIZE = sizeof(CUuid) / sizeof(int32_t), |
| 25 | OFFSET_UUID = 1 << 16, |
| 26 | }; |
| 27 | |
| 28 | extern const CUuid UUID_ZEROED; |
| 29 | |
| 30 | CUuid RandomUuid(); |
| 31 | CUuid CalculateUuid(const char *pName); |
| 32 | // The buffer length should be at least UUID_MAXSTRSIZE. |
| 33 | void FormatUuid(CUuid Uuid, char *pBuffer, unsigned BufferLength); |
| 34 | // Returns nonzero on failure. |
| 35 | int ParseUuid(CUuid *pUuid, const char *pBuffer); |
| 36 | // Converts a UUID into an array of UUID_INT_SIZE big-endian 32-bit ints. |
| 37 | void UuidToInts(const CUuid &Uuid, int *pInts); |
| 38 | |
| 39 | template<int N> |
| 40 | void UuidToInts(const CUuid &Uuid, int (&aInts)[N]) |
| 41 | { |
| 42 | static_assert(N == UUID_INT_SIZE, "Array has wrong size" ); |
| 43 | UuidToInts(Uuid, pInts: static_cast<int *>(aInts)); |
| 44 | } |
| 45 | |
| 46 | // Converts an array of UUID_INT_SIZE integers, each holding one big-endian 32-bit int, into a UUID. |
| 47 | CUuid UuidFromInts(const int *pInts); |
| 48 | |
| 49 | template<int N> |
| 50 | CUuid UuidFromInts(const int (&aInts)[N]) |
| 51 | { |
| 52 | static_assert(N == UUID_INT_SIZE, "Array has wrong size" ); |
| 53 | return UuidFromInts(pInts: static_cast<const int *>(aInts)); |
| 54 | } |
| 55 | |
| 56 | class CName |
| 57 | { |
| 58 | public: |
| 59 | CUuid m_Uuid; |
| 60 | const char *m_pName; |
| 61 | }; |
| 62 | |
| 63 | class CNameIndexed |
| 64 | { |
| 65 | public: |
| 66 | CUuid m_Uuid; |
| 67 | int m_Id; |
| 68 | |
| 69 | bool operator<(const CNameIndexed &Other) const { return m_Uuid < Other.m_Uuid; } |
| 70 | bool operator==(const CNameIndexed &Other) const { return m_Uuid == Other.m_Uuid; } |
| 71 | }; |
| 72 | |
| 73 | class CPacker; |
| 74 | class CUnpacker; |
| 75 | |
| 76 | class CUuidManager |
| 77 | { |
| 78 | std::vector<CName> m_vNames; |
| 79 | std::vector<CNameIndexed> m_vNamesSorted; |
| 80 | |
| 81 | public: |
| 82 | void RegisterName(int Id, const char *pName); |
| 83 | CUuid GetUuid(int Id) const; |
| 84 | const char *GetName(int Id) const; |
| 85 | int LookupUuid(CUuid Uuid) const; |
| 86 | int NumUuids() const; |
| 87 | |
| 88 | int UnpackUuid(CUnpacker *pUnpacker) const; |
| 89 | int UnpackUuid(CUnpacker *pUnpacker, CUuid *pOut) const; |
| 90 | void PackUuid(int Id, CPacker *pPacker) const; |
| 91 | |
| 92 | void DebugDump() const; |
| 93 | }; |
| 94 | |
| 95 | extern CUuidManager g_UuidManager; |
| 96 | |
| 97 | #endif // ENGINE_SHARED_UUID_MANAGER_H |
| 98 | |