comparison host/tools/Utils/common/Properties.h @ 82:6736c52df507 ecos-sw-2000-04-14

Merge from eCos master repository on 2000-04-14-13:35:46-BST
author jlarmour
date Tue, 18 Apr 2000 21:51:55 +0000
parents 435cced73e2f
children
comparison
equal deleted inserted replaced
81:89fef2181d7d 82:6736c52df507
34 #endif // _MSC_VER > 1000 34 #endif // _MSC_VER > 1000
35 35
36 #include "eCosStd.h" 36 #include "eCosStd.h"
37 #include "Collections.h" 37 #include "Collections.h"
38 38
39 //////////////////////////////////////////////////////////////////////
40 // This class manages properties and their serialization.
41 // What you do is this:
42 // 1. Declare the CProperties object
43 // 2. Call one or more "Add" functions to associate variables with names
44 // 3. Call one of the "Load" or "Save" functions to load or save the values
45 // There are three ways in which the "Load" or "Save" functions operate
46 // a. Loading/saving to/from a file [LoadFromFile/SaveToFile]
47 // In this case the contents of the file will look like
48 // name1=value1
49 // name2=value2
50 // ...
51 // b. Loading/saving to/from a command string [LoadFromCommandString/MakeCommandString]
52 // In this case the contents of the string will look like
53 // -name1=value1 -name2=value2 ...
54 // c. Loading/saving to/from the registry [LoadFromRegistry/SaveToRegistry]
55 // In this case the registry will contain a set of values (name1, name2,...) each of whose
56 // value data is the corresponding value.
57 //////////////////////////////////////////////////////////////////////
58
39 class CProperties 59 class CProperties
40 { 60 {
41 public: 61 public:
42 // bool Remove (LPCTSTR pszName); 62 CProperties();
43 void RemoveAll (); 63 virtual ~CProperties();
64
44 // Declare various types of property. The functions associate names with 65 // Declare various types of property. The functions associate names with
45 // variables, but no values are assigned here. That comes later when a 66 // variables, but no values are assigned here. That comes later when a
46 // load function (such as LoadFromRegistry) is called. 67 // load function (such as LoadFromRegistry) is called.
47 void Add (LPCTSTR pszName,int &n,int nDefault=0); 68 void Add (LPCTSTR pszName,int &n);
48 void Add (LPCTSTR pszName,unsigned int &n,unsigned int nDefault=0); 69 void Add (LPCTSTR pszName,unsigned int &n);
49 void Add (LPCTSTR pszName,bool &b,bool bDefault=false); 70 void Add (LPCTSTR pszName,bool &b);
50 void Add (LPCTSTR pszName,char &c,char cDefault='\0'); 71 void Add (LPCTSTR pszName,char &c);
51 void Add (LPCTSTR pszName,unsigned char &c,unsigned char cDefault='\0'); 72 void Add (LPCTSTR pszName,unsigned char &c);
52 void Add (LPCTSTR pszName,short&s,short sDefault=0); 73 void Add (LPCTSTR pszName,short&s);
53 void Add (LPCTSTR pszName,unsigned short&s,unsigned short sDefault=0); 74 void Add (LPCTSTR pszName,unsigned short&s);
54 void Add (LPCTSTR pszName,float&f,float fDefault=0.0); 75 void Add (LPCTSTR pszName,float&f);
55 void Add (LPCTSTR pszName,double&f,double fDefault=0.0); 76 void Add (LPCTSTR pszName,double&f);
56 void Add (LPCTSTR pszName,void *d,unsigned int nLength,void *pvDefault=0); 77 void Add (LPCTSTR pszName,String &s);
57 void Add (LPCTSTR pszName,LPTSTR s,unsigned int nLength,LPCTSTR sDefault=_T("")); 78 void Add (LPCTSTR pszName,void *d,unsigned int nLength);
58 // String adding with caller defining get and put. This is here to preserve the neutrality of
59 // the class - cannot assume MFC's String, for example. Implementation of these fns might be:
60 // static LPCTSTR CALLBACK GetFn (void *pObj) { return (LPCTSTR)*(String *)pObj; }
61 // static void CALLBACK PutFn (void *pObj,LPCTSTR psz) { *(String *)pObj=psz; }
62
63 typedef void (CALLBACK putFn)(void *,LPCTSTR);
64 typedef LPCTSTR (CALLBACK getFn)(void *);
65 void Add (LPCTSTR pszName,void *pObj,getFn *pgetFn,putFn *pputFn,LPCTSTR pszDefault=_T(""));
66
67 // Set all Added variables to their default values
68 void SetDefaults();
69 // Load (from registry ro f
70 bool Load(LPCTSTR pszPrefix=_T(""));
71 bool Save(LPCTSTR pszPrefix=_T(""));
72 79
73 // Load from and save to a command string. 80 // Load from and save to a command string.
74 String MakeCommandString (LPCTSTR pszPrefix=_T("-")) const; // caller must delete [] 81 String MakeCommandString () const; // caller must delete []
75 bool LoadFromCommandString (LPCTSTR psz,LPCTSTR pszPrefix=_T("-")); 82 bool LoadFromCommandString (LPCTSTR psz);
76 83
77 CProperties(LPCTSTR pszFile,void *hKey=0); 84 // Load from and save to a given file. The format is name=value, one per line.
78 virtual ~CProperties(); 85 bool LoadFromFile(LPCTSTR pszFileName);
86 bool SaveToFile (LPCTSTR pszFileName) const;
87
79 #ifdef _WIN32 88 #ifdef _WIN32
89 bool LoadFromRegistry (HKEY,LPCTSTR);
90 bool SaveToRegistry(HKEY,LPCTSTR) const;
80 static bool CreateKey (LPCTSTR pszKey,HKEY hKey=HKEY_CURRENT_USER); 91 static bool CreateKey (LPCTSTR pszKey,HKEY hKey=HKEY_CURRENT_USER);
81 #endif 92 #endif
93
82 protected: 94 protected:
83 static bool CreatePathToFile(LPCTSTR pszDir); 95 static bool CreatePathToFile(LPCTSTR pszDir);
84 String m_strName;
85 96
86 // Load from and save to a given file. The format is name=value, one per line.
87 bool LoadFromFile(LPCTSTR pszFileName,LPCTSTR pszPrefix=_T(""));
88 bool SaveToFile (LPCTSTR pszFileName,LPCTSTR pszPrefix=_T("")) const;
89 // Load from and save to the registry
90 void *m_hKey;
91 #ifdef _WIN32
92 bool SaveToRegistry(HKEY,LPCTSTR,LPCTSTR pszPrefix=_T("")) const;
93 bool LoadFromRegistry (HKEY,LPCTSTR,LPCTSTR pszPrefix=_T(""));
94 #endif
95
96 PtrArray ar; // Holds declared properties
97 class CProperty { 97 class CProperty {
98 public: 98 public:
99 enum Typetype {Integer, szString, GPString, Bool, Char, Short, Float, Double, Void}; 99 enum Typetype {Integer, szString, Bool, Char, Short, Float, Double, Void};
100 CProperty(LPCTSTR pszName,Typetype type,void *_pData); 100 CProperty(LPCTSTR pszName,Typetype type,void *_pData);
101 virtual ~CProperty(); 101 virtual ~CProperty();
102 void SetDefault();
103 friend class CProperties; 102 friend class CProperties;
104 bool SetValue(int n); 103 bool SetValue(int n);
105 bool SetValue(double n); 104 bool SetValue(double n);
106 bool SetValue(void *d,int nLength); 105 bool SetValue(void *d,int nLength);
107 bool SetValue(LPCTSTR psz); 106 bool SetValue(LPCTSTR psz);
110 109
111 protected: 110 protected:
112 String strName; 111 String strName;
113 Typetype Type; 112 Typetype Type;
114 void *pData; 113 void *pData;
115 union { 114 unsigned int nLength; // for Void
116 int nDefault;
117 double dDefault;
118 void* pvDefault;
119 };
120 String strDefault; // can't be in union - has copy ctor
121 getFn *pgetFn;
122 putFn *pputFn;
123 unsigned int nLength; // for szString and Void
124 }; 115 };
125 CProperty * Lookup (LPCTSTR pszName); 116 CProperty * Lookup (LPCTSTR pszName);
117 std::vector<CProperty> ar; // Holds declared properties
126 }; 118 };
127 119
128 #endif // !defined(AFX_PROPERTIES_H__DA938D29_135A_11D3_A50B_00A0C949ADAC__INCLUDED_) 120 #endif // !defined(AFX_PROPERTIES_H__DA938D29_135A_11D3_A50B_00A0C949ADAC__INCLUDED_)