forked from hitmen047/Source-PlusPlus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathp4helpers.h
179 lines (141 loc) · 3.82 KB
/
p4helpers.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
//========= Copyright Valve Corporation, All rights reserved. ============//
//
// Purpose:
//
// $NoKeywords: $
//
//=============================================================================//
#ifndef P4HELPERS_H
#define P4HELPERS_H
#ifdef _WIN32
#pragma once
#endif
#include "tier1/utlstring.h"
#include "tier1/smartptr.h"
//
// Class representing file operations
//
class CP4File
{
public:
explicit CP4File( char const *szFilename );
virtual ~CP4File();
public:
// Opens the file for edit
virtual bool Edit( void );
// Opens the file for add
virtual bool Add( void );
// Reverts the file
virtual bool Revert( void );
// Is the file in perforce?
virtual bool IsFileInPerforce();
// Changes the file to the specified filetype.
virtual bool SetFileType( const CUtlString& desiredFileType );
protected:
// The filename that this class instance represents
CUtlString m_sFilename;
};
//
// An override of CP4File performing no Perforce interaction
//
class CP4File_Dummy : public CP4File
{
public:
explicit CP4File_Dummy( char const *szFilename ) : CP4File( szFilename ) {}
public:
virtual bool Edit( void ) { return true; }
virtual bool Add( void ) { return true; }
virtual bool IsFileInPerforce() { return false; }
virtual bool SetFileType(const CUtlString& desiredFileType) { return true; }
};
//
// Class representing a factory for creating other helper objects
//
class CP4Factory
{
public:
CP4Factory();
~CP4Factory();
public:
// Sets whether dummy objects are created by the factory.
// Returns the old state of the dummy mode.
bool SetDummyMode( bool bDummyMode );
public:
// Sets the name of the changelist to open files under,
// NULL for "Default" changelist.
void SetOpenFileChangeList( const char *szChangeListName );
public:
// Creates a file access object for the given filename.
CP4File *AccessFile( char const *szFilename ) const;
protected:
// Whether the factory is in the "dummy mode" and is creating dummy objects
bool m_bDummyMode;
};
// Default p4 factory
extern CP4Factory *g_p4factory;
//
// CP4AutoEditFile - edits the file upon construction
//
class CP4AutoEditFile
{
public:
explicit CP4AutoEditFile( char const *szFilename ) : m_spImpl( g_p4factory->AccessFile( szFilename ) ) { m_spImpl->Edit(); }
CP4File * File() const { return m_spImpl.Get(); }
protected:
CPlainAutoPtr< CP4File > m_spImpl;
};
//
// CP4AutoAddFile - adds the file upon construction
//
class CP4AutoAddFile
{
public:
explicit CP4AutoAddFile( char const *szFilename ) : m_spImpl( g_p4factory->AccessFile( szFilename ) ) { m_spImpl->Add(); }
CP4File * File() const { return m_spImpl.Get(); }
protected:
CPlainAutoPtr< CP4File > m_spImpl;
};
//
// CP4AutoEditAddFile - edits the file upon construction / adds upon destruction
//
class CP4AutoEditAddFile
{
public:
explicit CP4AutoEditAddFile( char const *szFilename )
: m_spImpl( g_p4factory->AccessFile( szFilename ) )
, m_bHasDesiredFileType( false )
{
m_spImpl->Edit();
}
explicit CP4AutoEditAddFile( char const *szFilename, const char *szFiletype )
: m_spImpl( g_p4factory->AccessFile( szFilename ) )
, m_sFileType(szFiletype)
, m_bHasDesiredFileType( true )
{
m_spImpl->Edit();
m_spImpl->SetFileType( m_sFileType );
}
~CP4AutoEditAddFile( void )
{
m_spImpl->Add();
if ( m_bHasDesiredFileType )
m_spImpl->SetFileType( m_sFileType );
}
CP4File * File() const { return m_spImpl.Get(); }
protected:
CPlainAutoPtr< CP4File > m_spImpl;
CUtlString m_sFileType;
bool m_bHasDesiredFileType;
};
//
// CP4AutoRevert - reverts the file upon construction
//
class CP4AutoRevertFile
{
public:
explicit CP4AutoRevertFile( char const *szFilename ) : m_spImpl( g_p4factory->AccessFile( szFilename ) ) { m_spImpl->Revert(); }
CP4File * File() const { return m_spImpl.Get(); }
protected:
CPlainAutoPtr< CP4File > m_spImpl;
};
#endif // #ifndef P4HELPERS_H