File: Persistent.h

package info (click to toggle)
gcvs 1.0final-12
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 12,248 kB
  • ctags: 10,631
  • sloc: ansic: 71,709; cpp: 39,780; sh: 18,434; makefile: 1,915; yacc: 1,299; tcl: 1,283; perl: 910; lex: 249; csh: 185; lisp: 7
file content (170 lines) | stat: -rwxr-xr-x 4,368 bytes parent folder | download | duplicates (2)
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
/*
** This program is free software; you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation; either version 1, or (at your option)
** any later version.

** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
** GNU General Public License for more details.

** You should have received a copy of the GNU General Public License
** along with this program; if not, write to the Free Software
** Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/

/*
 * Author : Alexandre Parenteau <aubonbeurre@hotmail.com> --- December 1997
 */

/*
 * Persistent.h --- utilities to store persitent values
 */

#ifndef PERSISTENT_H
#define PERSISTENT_H

#include <string.h>
#include <vector>
#include "uobject.h"

#ifdef macintosh
#	include <Files.h>
#endif /* macintosh */

typedef enum
{
	kNoClass = 0x00,
	kAddSettings = 0x01
} kClassPersistent;

// virtual class to access the persistent data to load/save
class CPersistent : public UObject
{
	UDECLARE_DYNAMIC(CPersistent)
public:
	CPersistent(const char *uniqueName, kClassPersistent pclass);
	virtual ~CPersistent();

	virtual unsigned int SizeOf(void) const = 0;
	virtual const void *GetData(void) const = 0;
	virtual void SetData(const void *ptr, unsigned int size) = 0;
		// virtual access
	
	static void SaveAll(void);
	static bool LoadAll(void);
		// save/load the registred values

	static bool SaveAllSettings(const char *settingFileName, const char *forPath);
		// save all the persistent values with the class 'kAddSettings'

	struct CPersistentEntry
	{
		const char *fUniqueName;
		CPersistent *fValue;
		kClassPersistent fClass;
	};

	static CPersistentEntry * Find(const char *uniqueName);

	// handle modification flag (see if we really need to save it)
	inline void ResetTimeStamp() { fTimeStamp = 0; }
	inline long GetTimeStamp() const { return fTimeStamp; }
	inline void TouchTimeStamp() { fTimeStamp++; }

	static bool NeedSave(bool onlySettings = false);

private:
	static void Register(CPersistent *value, const char *uniqueName,
		kClassPersistent pclass);
	static void UnRegister(CPersistent *value);
		// register/unregister a value to store/load
	
protected:
	long fTimeStamp;
	static std::vector<CPersistentEntry> *fAllEntries;
};

#define PERSISTENT_CAST(T) \
	inline operator const T &() const {return fValue;} \
	inline const T * operator ->() const {return &fValue;} \
	inline T & operator=(const T & val) { TouchTimeStamp(); return fValue = val;} \
	inline bool operator==(const T & val) const {return fValue == val;}

// this is OK for most of the persistant values

template <class T>
class CPersistentT : public CPersistent
{
public:
	CPersistentT(const char *uniqueName, T defValue, kClassPersistent pclass) : CPersistent(uniqueName, pclass)
	{
		fValue = fDefValue = defValue;
	}
	virtual ~CPersistentT()
	{
	}

	PERSISTENT_CAST(T)

	virtual unsigned int SizeOf(void) const {return sizeof(T);}
	virtual const void *GetData(void) const {return &fValue;}
	virtual void SetData(const void *ptr, unsigned int size)
	{
		if(size == sizeof(T))
			memcpy(&fValue, ptr, size);
	}
	
protected:
	T fDefValue, fValue;
};

class CPersistentBool : public CPersistentT<bool>
{
	UDECLARE_DYNAMIC(CPersistentBool)
public:
	CPersistentBool(const char *uniqueName, bool defValue,
		kClassPersistent pclass = kNoClass) :
	  CPersistentT<bool>(uniqueName, defValue, pclass)
	{
	}
	virtual ~CPersistentBool()
	{
	}

	PERSISTENT_CAST(bool)
};

class CPersistentInt : public CPersistentT<int>
{
	UDECLARE_DYNAMIC(CPersistentInt)
public:
	CPersistentInt(const char *uniqueName, int defValue,
		kClassPersistent pclass = kNoClass) :
			CPersistentT<int>(uniqueName, defValue, pclass)
	{
	}
	virtual ~CPersistentInt()
	{
	}

	PERSISTENT_CAST(int)
};

#ifdef WIN32
#	define PROFILE_NAME "CVS settings"
#endif /* WIN32 */

#if TARGET_OS_MAC
#	define kCVS_prefID 'cprf'
#	define kCVS_prefName "\pMacCvs Prefs"
#endif

// Persistent settings API
void LoadPersistentSettings(const char *path, bool quiet = false);
bool HasPersistentSettings(const char *path);
void AskCreatePersistentSettings(const char *path);
void SavePersistentSettings(const char *path);

#endif /* PERSISTENT_H */