Team Fortress 2 Source Code as on 22/4/2020
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

76 lines
1.5 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. // $NoKeywords: $
  6. //
  7. //=============================================================================//
  8. #ifndef FILEMEMCACHE_H
  9. #define FILEMEMCACHE_H
  10. #ifdef _WIN32
  11. #pragma once
  12. #endif
  13. #include "tier0/platform.h"
  14. #include "tier1/generichash.h"
  15. #include <unordered_map>
  16. #pragma warning ( disable : 4200 )
  17. class CachedFileData
  18. {
  19. friend class FileCache;
  20. protected: // Constructed by FileCache
  21. CachedFileData() {}
  22. static CachedFileData *Create( char const *szFilename );
  23. void Free( void );
  24. public:
  25. static CachedFileData *GetByDataPtr( void const *pvDataPtr );
  26. char const * GetFileName() const;
  27. void const * GetDataPtr() const;
  28. int GetDataLen() const;
  29. int UpdateRefCount( int iDeltaRefCount ) { return m_numRefs += iDeltaRefCount; }
  30. bool IsValid() const;
  31. protected:
  32. enum { eHeaderSize = 256 };
  33. char m_chFilename[256 - 12];
  34. int m_numRefs;
  35. int m_numDataBytes;
  36. int m_signature;
  37. unsigned char m_data[0]; // file data spans further
  38. };
  39. class FileCache
  40. {
  41. public:
  42. FileCache();
  43. ~FileCache() { Clear(); }
  44. public:
  45. CachedFileData *Get( char const *szFilename );
  46. void Clear( void );
  47. protected:
  48. struct eqstr {
  49. inline size_t operator()( const char *s ) const {
  50. if ( !s )
  51. return 0;
  52. return HashString( s );
  53. }
  54. inline bool operator()( const char *s1, const char *s2 ) const {
  55. return _stricmp( s1, s2 ) < 0;
  56. }
  57. };
  58. typedef std::unordered_map< const char *, CachedFileData *, eqstr, eqstr > Mapping;
  59. Mapping m_map;
  60. };
  61. #endif // #ifndef FILEMEMCACHE_H