| 1 | #ifndef ENGINE_SHARED_UUID_MANAGER_H |
| 2 | #define ENGINE_SHARED_UUID_MANAGER_H |
| 3 | |
| 4 | #include <memory> |
| 5 | #include <vector> |
| 6 | |
| 7 | enum |
| 8 | { |
| 9 | UUID_MAXSTRSIZE = 37, // 12345678-0123-5678-0123-567890123456 |
| 10 | |
| 11 | UUID_INVALID = -2, |
| 12 | UUID_UNKNOWN = -1, |
| 13 | |
| 14 | OFFSET_UUID = 1 << 16, |
| 15 | }; |
| 16 | |
| 17 | struct CUuid |
| 18 | { |
| 19 | unsigned char m_aData[16]; |
| 20 | |
| 21 | bool operator==(const CUuid &Other) const; |
| 22 | bool operator!=(const CUuid &Other) const; |
| 23 | bool operator<(const CUuid &Other) const; |
| 24 | }; |
| 25 | |
| 26 | extern const CUuid UUID_ZEROED; |
| 27 | |
| 28 | CUuid RandomUuid(); |
| 29 | CUuid CalculateUuid(const char *pName); |
| 30 | // The buffer length should be at least UUID_MAXSTRSIZE. |
| 31 | void FormatUuid(CUuid Uuid, char *pBuffer, unsigned BufferLength); |
| 32 | // Returns nonzero on failure. |
| 33 | int ParseUuid(CUuid *pUuid, const char *pBuffer); |
| 34 | |
| 35 | struct CName |
| 36 | { |
| 37 | CUuid m_Uuid; |
| 38 | const char *m_pName; |
| 39 | }; |
| 40 | |
| 41 | struct CNameIndexed |
| 42 | { |
| 43 | CUuid m_Uuid; |
| 44 | int m_Id; |
| 45 | |
| 46 | bool operator<(const CNameIndexed &Other) const { return m_Uuid < Other.m_Uuid; } |
| 47 | bool operator==(const CNameIndexed &Other) const { return m_Uuid == Other.m_Uuid; } |
| 48 | }; |
| 49 | |
| 50 | class CPacker; |
| 51 | class CUnpacker; |
| 52 | |
| 53 | class CUuidManager |
| 54 | { |
| 55 | std::vector<CName> m_vNames; |
| 56 | std::vector<CNameIndexed> m_vNamesSorted; |
| 57 | |
| 58 | public: |
| 59 | void RegisterName(int Id, const char *pName); |
| 60 | CUuid GetUuid(int Id) const; |
| 61 | const char *GetName(int Id) const; |
| 62 | int LookupUuid(CUuid Uuid) const; |
| 63 | int NumUuids() const; |
| 64 | |
| 65 | int UnpackUuid(CUnpacker *pUnpacker) const; |
| 66 | int UnpackUuid(CUnpacker *pUnpacker, CUuid *pOut) const; |
| 67 | void PackUuid(int Id, CPacker *pPacker) const; |
| 68 | |
| 69 | void DebugDump() const; |
| 70 | }; |
| 71 | |
| 72 | extern CUuidManager g_UuidManager; |
| 73 | |
| 74 | std::unique_ptr<CUuidManager> CUuidManager_New(); |
| 75 | const CUuidManager &CUuidManager_Global(); |
| 76 | |
| 77 | #endif // ENGINE_SHARED_UUID_MANAGER_H |
| 78 | |