Counter Strike : Global Offensive Source Code
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.

138 lines
2.8 KiB

  1. //====== Copyright c 1996-2007, Valve Corporation, All rights reserved. =======//
  2. //
  3. // Purpose:
  4. //
  5. // $NoKeywords: $
  6. //
  7. //=============================================================================//
  8. #ifndef P4HELPERS_H
  9. #define P4HELPERS_H
  10. #ifdef _WIN32
  11. #pragma once
  12. #endif
  13. #include "tier1/utlstring.h"
  14. #include "tier1/smartptr.h"
  15. //
  16. // Class representing file operations
  17. //
  18. class CP4File
  19. {
  20. public:
  21. explicit CP4File( char const *szFilename );
  22. virtual ~CP4File();
  23. public:
  24. // Opens the file for edit
  25. virtual bool Edit( void );
  26. // Opens the file for add
  27. virtual bool Add( void );
  28. // Is the file in perforce?
  29. virtual bool IsFileInPerforce();
  30. protected:
  31. // The filename that this class instance represents
  32. CUtlString m_sFilename;
  33. };
  34. //
  35. // An override of CP4File performing no Perforce interaction
  36. //
  37. class CP4File_Dummy : public CP4File
  38. {
  39. public:
  40. explicit CP4File_Dummy( char const *szFilename ) : CP4File( szFilename ) {}
  41. public:
  42. virtual bool Edit( void ) { return true; }
  43. virtual bool Add( void ) { return true; }
  44. virtual bool IsFileInPerforce() { return false; }
  45. };
  46. //
  47. // Class representing a factory for creating other helper objects
  48. //
  49. class CP4Factory
  50. {
  51. public:
  52. CP4Factory();
  53. ~CP4Factory();
  54. public:
  55. // Sets whether dummy objects are created by the factory.
  56. // Returns the old state of the dummy mode.
  57. bool SetDummyMode( bool bDummyMode );
  58. public:
  59. // Sets the name of the changelist to open files under,
  60. // NULL for "Default" changelist.
  61. void SetOpenFileChangeList( const char *szChangeListName );
  62. public:
  63. // Creates a file access object for the given filename.
  64. CP4File *AccessFile( char const *szFilename ) const;
  65. protected:
  66. // Whether the factory is in the "dummy mode" and is creating dummy objects
  67. bool m_bDummyMode;
  68. };
  69. // Default p4 factory
  70. extern CP4Factory *g_p4factory;
  71. //
  72. // CP4AutoEditFile - edits the file upon construction
  73. //
  74. class CP4AutoEditFile
  75. {
  76. public:
  77. explicit CP4AutoEditFile( char const *szFilename ) : m_spImpl( g_p4factory->AccessFile( szFilename ) ) { m_spImpl->Edit(); }
  78. CP4File * File() const { return m_spImpl.Get(); }
  79. protected:
  80. CPlainAutoPtr< CP4File > m_spImpl;
  81. };
  82. //
  83. // CP4AutoAddFile - adds the file upon construction
  84. //
  85. class CP4AutoAddFile
  86. {
  87. public:
  88. explicit CP4AutoAddFile( char const *szFilename ) : m_spImpl( g_p4factory->AccessFile( szFilename ) ) { m_spImpl->Add(); }
  89. CP4File * File() const { return m_spImpl.Get(); }
  90. protected:
  91. CPlainAutoPtr< CP4File > m_spImpl;
  92. };
  93. //
  94. // CP4AutoEditAddFile - edits the file upon construction / adds upon destruction
  95. //
  96. class CP4AutoEditAddFile
  97. {
  98. public:
  99. explicit CP4AutoEditAddFile( char const *szFilename ) : m_spImpl( g_p4factory->AccessFile( szFilename ) )
  100. {
  101. m_spImpl->Edit();
  102. }
  103. ~CP4AutoEditAddFile( void ) { m_spImpl->Add(); }
  104. CP4File * File() const { return m_spImpl.Get(); }
  105. protected:
  106. CPlainAutoPtr< CP4File > m_spImpl;
  107. };
  108. #endif // #ifndef P4HELPERS_H