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.

179 lines
3.8 KiB

  1. //========= Copyright 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. // Reverts the file
  29. virtual bool Revert( void );
  30. // Is the file in perforce?
  31. virtual bool IsFileInPerforce();
  32. // Changes the file to the specified filetype.
  33. virtual bool SetFileType( const CUtlString& desiredFileType );
  34. protected:
  35. // The filename that this class instance represents
  36. CUtlString m_sFilename;
  37. };
  38. //
  39. // An override of CP4File performing no Perforce interaction
  40. //
  41. class CP4File_Dummy : public CP4File
  42. {
  43. public:
  44. explicit CP4File_Dummy( char const *szFilename ) : CP4File( szFilename ) {}
  45. public:
  46. virtual bool Edit( void ) { return true; }
  47. virtual bool Add( void ) { return true; }
  48. virtual bool IsFileInPerforce() { return false; }
  49. virtual bool SetFileType(const CUtlString& desiredFileType) { return true; }
  50. };
  51. //
  52. // Class representing a factory for creating other helper objects
  53. //
  54. class CP4Factory
  55. {
  56. public:
  57. CP4Factory();
  58. ~CP4Factory();
  59. public:
  60. // Sets whether dummy objects are created by the factory.
  61. // Returns the old state of the dummy mode.
  62. bool SetDummyMode( bool bDummyMode );
  63. public:
  64. // Sets the name of the changelist to open files under,
  65. // NULL for "Default" changelist.
  66. void SetOpenFileChangeList( const char *szChangeListName );
  67. public:
  68. // Creates a file access object for the given filename.
  69. CP4File *AccessFile( char const *szFilename ) const;
  70. protected:
  71. // Whether the factory is in the "dummy mode" and is creating dummy objects
  72. bool m_bDummyMode;
  73. };
  74. // Default p4 factory
  75. extern CP4Factory *g_p4factory;
  76. //
  77. // CP4AutoEditFile - edits the file upon construction
  78. //
  79. class CP4AutoEditFile
  80. {
  81. public:
  82. explicit CP4AutoEditFile( char const *szFilename ) : m_spImpl( g_p4factory->AccessFile( szFilename ) ) { m_spImpl->Edit(); }
  83. CP4File * File() const { return m_spImpl.Get(); }
  84. protected:
  85. CPlainAutoPtr< CP4File > m_spImpl;
  86. };
  87. //
  88. // CP4AutoAddFile - adds the file upon construction
  89. //
  90. class CP4AutoAddFile
  91. {
  92. public:
  93. explicit CP4AutoAddFile( char const *szFilename ) : m_spImpl( g_p4factory->AccessFile( szFilename ) ) { m_spImpl->Add(); }
  94. CP4File * File() const { return m_spImpl.Get(); }
  95. protected:
  96. CPlainAutoPtr< CP4File > m_spImpl;
  97. };
  98. //
  99. // CP4AutoEditAddFile - edits the file upon construction / adds upon destruction
  100. //
  101. class CP4AutoEditAddFile
  102. {
  103. public:
  104. explicit CP4AutoEditAddFile( char const *szFilename )
  105. : m_spImpl( g_p4factory->AccessFile( szFilename ) )
  106. , m_bHasDesiredFileType( false )
  107. {
  108. m_spImpl->Edit();
  109. }
  110. explicit CP4AutoEditAddFile( char const *szFilename, const char *szFiletype )
  111. : m_spImpl( g_p4factory->AccessFile( szFilename ) )
  112. , m_sFileType(szFiletype)
  113. , m_bHasDesiredFileType( true )
  114. {
  115. m_spImpl->Edit();
  116. m_spImpl->SetFileType( m_sFileType );
  117. }
  118. ~CP4AutoEditAddFile( void )
  119. {
  120. m_spImpl->Add();
  121. if ( m_bHasDesiredFileType )
  122. m_spImpl->SetFileType( m_sFileType );
  123. }
  124. CP4File * File() const { return m_spImpl.Get(); }
  125. protected:
  126. CPlainAutoPtr< CP4File > m_spImpl;
  127. CUtlString m_sFileType;
  128. bool m_bHasDesiredFileType;
  129. };
  130. //
  131. // CP4AutoRevert - reverts the file upon construction
  132. //
  133. class CP4AutoRevertFile
  134. {
  135. public:
  136. explicit CP4AutoRevertFile( char const *szFilename ) : m_spImpl( g_p4factory->AccessFile( szFilename ) ) { m_spImpl->Revert(); }
  137. CP4File * File() const { return m_spImpl.Get(); }
  138. protected:
  139. CPlainAutoPtr< CP4File > m_spImpl;
  140. };
  141. #endif // #ifndef P4HELPERS_H