1*2c7984eaSAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*2c7984eaSAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*2c7984eaSAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*2c7984eaSAndrew Rist  * distributed with this work for additional information
6*2c7984eaSAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*2c7984eaSAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*2c7984eaSAndrew Rist  * "License"); you may not use this file except in compliance
9*2c7984eaSAndrew Rist  * with the License.  You may obtain a copy of the License at
10*2c7984eaSAndrew Rist  *
11*2c7984eaSAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*2c7984eaSAndrew Rist  *
13*2c7984eaSAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*2c7984eaSAndrew Rist  * software distributed under the License is distributed on an
15*2c7984eaSAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*2c7984eaSAndrew Rist  * KIND, either express or implied.  See the License for the
17*2c7984eaSAndrew Rist  * specific language governing permissions and limitations
18*2c7984eaSAndrew Rist  * under the License.
19*2c7984eaSAndrew Rist  *
20*2c7984eaSAndrew Rist  *************************************************************/
21*2c7984eaSAndrew Rist 
22*2c7984eaSAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir #ifndef _REGISTRY_HXX_
25cdf0e10cSrcweir #define _REGISTRY_HXX_
26cdf0e10cSrcweir 
27cdf0e10cSrcweir #ifdef _MSC_VER
28cdf0e10cSrcweir #pragma warning(push, 1) /* disable warnings within system headers */
29cdf0e10cSrcweir #endif
30cdf0e10cSrcweir #include <windows.h>
31cdf0e10cSrcweir #ifdef _MSC_VER
32cdf0e10cSrcweir #pragma warning(pop)
33cdf0e10cSrcweir #endif
34cdf0e10cSrcweir 
35cdf0e10cSrcweir #include <memory>
36cdf0e10cSrcweir #include <vector>
37cdf0e10cSrcweir #include <string>
38cdf0e10cSrcweir 
39cdf0e10cSrcweir #include "registryvalueimpl.hxx"
40cdf0e10cSrcweir 
41cdf0e10cSrcweir //---------------------------------------
42cdf0e10cSrcweir // forward declaration
43cdf0e10cSrcweir //---------------------------------------
44cdf0e10cSrcweir 
45cdf0e10cSrcweir class RegistryKeyImpl;
46cdf0e10cSrcweir 
47cdf0e10cSrcweir //---------------------------------------
48cdf0e10cSrcweir // typedefs
49cdf0e10cSrcweir //---------------------------------------
50cdf0e10cSrcweir 
51cdf0e10cSrcweir typedef std::auto_ptr<RegistryKeyImpl>  	RegistryKey;
52cdf0e10cSrcweir typedef std::vector<std::wstring>				StringList;
53cdf0e10cSrcweir typedef std::auto_ptr<StringList>				StringListPtr;
54cdf0e10cSrcweir 
55cdf0e10cSrcweir //---------------------------------------
56cdf0e10cSrcweir //
57cdf0e10cSrcweir //---------------------------------------
58cdf0e10cSrcweir 
59cdf0e10cSrcweir class RegistryKeyImpl
60cdf0e10cSrcweir {
61cdf0e10cSrcweir public:
62cdf0e10cSrcweir 
63cdf0e10cSrcweir 	//############################################
64cdf0e10cSrcweir 	// Destruction
65cdf0e10cSrcweir 	//############################################
66cdf0e10cSrcweir 
67cdf0e10cSrcweir     virtual ~RegistryKeyImpl();
68cdf0e10cSrcweir 
69cdf0e10cSrcweir 
70cdf0e10cSrcweir 	//############################################
71cdf0e10cSrcweir 	// Queries
72cdf0e10cSrcweir 	//############################################
73cdf0e10cSrcweir 
74cdf0e10cSrcweir 
75cdf0e10cSrcweir 	/** The name of the key at hand, maybe empty
76cdf0e10cSrcweir 		if this is any of the root keys
77cdf0e10cSrcweir 	*/
78cdf0e10cSrcweir 	std::wstring GetName() const;
79cdf0e10cSrcweir 
80cdf0e10cSrcweir 	/** The number of sub values of the key at hand
81cdf0e10cSrcweir 
82cdf0e10cSrcweir 		@precond IsOpen = true
83cdf0e10cSrcweir 
84cdf0e10cSrcweir 		@throws
85cdf0e10cSrcweir 	*/
86cdf0e10cSrcweir 	virtual size_t GetSubValueCount() const = 0;
87cdf0e10cSrcweir 
88cdf0e10cSrcweir 	/** The number of sub-keys of the key at hand
89cdf0e10cSrcweir 
90cdf0e10cSrcweir 		@precond IsOpen = true
91cdf0e10cSrcweir 
92cdf0e10cSrcweir 		@throws
93cdf0e10cSrcweir 	*/
94cdf0e10cSrcweir 	virtual size_t GetSubKeyCount() const = 0;
95cdf0e10cSrcweir 
96cdf0e10cSrcweir 	bool IsOpen() const;
97cdf0e10cSrcweir 
98cdf0e10cSrcweir 	/** Do we have write access on the key at hand
99cdf0e10cSrcweir 	*/
100cdf0e10cSrcweir 	bool IsWriteable() const;
101cdf0e10cSrcweir 
102cdf0e10cSrcweir 	/** The StringList will be allocated on the heap,
103cdf0e10cSrcweir 		so this is in fact a transfer of ownership
104cdf0e10cSrcweir 		to the caller
105cdf0e10cSrcweir 
106cdf0e10cSrcweir 		@precond IsOpen = true
107cdf0e10cSrcweir 
108cdf0e10cSrcweir 		@throws RegistryIOException
109cdf0e10cSrcweir 	*/
110cdf0e10cSrcweir 	virtual StringListPtr GetSubKeyNames() const = 0;
111cdf0e10cSrcweir 
112cdf0e10cSrcweir 	/** The StringList will be allocated on the heap,
113cdf0e10cSrcweir 		so this is in fact a transfer of ownership
114cdf0e10cSrcweir 		to the caller
115cdf0e10cSrcweir 
116cdf0e10cSrcweir 		@precond IsOpen = true
117cdf0e10cSrcweir 
118cdf0e10cSrcweir 		@throws RegistryIOException
119cdf0e10cSrcweir 	*/
120cdf0e10cSrcweir 	virtual StringListPtr GetSubValueNames() const = 0;
121cdf0e10cSrcweir 
122cdf0e10cSrcweir 	/** Get the specified registry value
123cdf0e10cSrcweir 
124cdf0e10cSrcweir 		@precond IsOpen = true
125cdf0e10cSrcweir 	*/
126cdf0e10cSrcweir 	virtual RegistryValue GetValue(const std::wstring& Name) const = 0;
127cdf0e10cSrcweir 
128cdf0e10cSrcweir 	/** Get the specified registry value, return the given
129cdf0e10cSrcweir 		default value if value not found
130cdf0e10cSrcweir 
131cdf0e10cSrcweir 		@precond IsOpen = true
132cdf0e10cSrcweir 	*/
133cdf0e10cSrcweir 	virtual RegistryValue GetValue(const std::wstring& Name, const RegistryValue& Default) const = 0;
134cdf0e10cSrcweir 
135cdf0e10cSrcweir 	/** Convenience function to determine if the
136cdf0e10cSrcweir 		Registry key at hand has the specified
137cdf0e10cSrcweir 		value
138cdf0e10cSrcweir 
139cdf0e10cSrcweir 		@precond IsOpen = true
140cdf0e10cSrcweir 
141cdf0e10cSrcweir 		throws RegistryAccessDenyException
142cdf0e10cSrcweir 	*/
143cdf0e10cSrcweir 	bool HasValue(const std::wstring& Name) const;
144cdf0e10cSrcweir 
145cdf0e10cSrcweir 	/** Convenience function to determine if the
146cdf0e10cSrcweir 		Registry key at hand has the specified
147cdf0e10cSrcweir 		sub-key
148cdf0e10cSrcweir 
149cdf0e10cSrcweir 		@precond IsOpen = true
150cdf0e10cSrcweir 
151cdf0e10cSrcweir 		throws RegistryAccessDenyException
152cdf0e10cSrcweir 	*/
153cdf0e10cSrcweir 	bool HasSubKey(const std::wstring& Name) const;
154cdf0e10cSrcweir 
155cdf0e10cSrcweir 
156cdf0e10cSrcweir 	//############################################
157cdf0e10cSrcweir 	// Commands
158cdf0e10cSrcweir 	//############################################
159cdf0e10cSrcweir 
160cdf0e10cSrcweir 
161cdf0e10cSrcweir 	/** Open the registry key, has no effect if
162cdf0e10cSrcweir 		the key is already open
163cdf0e10cSrcweir 
164cdf0e10cSrcweir 		@precond IsOpen = false
165cdf0e10cSrcweir 
166cdf0e10cSrcweir 		@throws RegistryWriteAccessDenyException
167cdf0e10cSrcweir 				RegistryAccessDenyException
168cdf0e10cSrcweir 	*/
169cdf0e10cSrcweir 	virtual void Open(bool Writeable = true) = 0;
170cdf0e10cSrcweir 
171cdf0e10cSrcweir 	/** Close the registry key at hand, further
172cdf0e10cSrcweir 		using it without re-opening may cause
173cdf0e10cSrcweir 		RegistryIOExceptions to be thrown
174cdf0e10cSrcweir 
175cdf0e10cSrcweir 		This is a template method that calls
176cdf0e10cSrcweir 		ImplClose which has to be overwritten
177cdf0e10cSrcweir 		by sub-classes
178cdf0e10cSrcweir 	*/
179cdf0e10cSrcweir 	void Close();
180cdf0e10cSrcweir 
181cdf0e10cSrcweir 	/** Open the specified sub-key of the registry key
182cdf0e10cSrcweir 		at hand
183cdf0e10cSrcweir 
184cdf0e10cSrcweir 		@precond IsOpen = true
185cdf0e10cSrcweir 				 HasSubKey(Name) = true
186cdf0e10cSrcweir 
187cdf0e10cSrcweir 		@throws RegistryIOException
188cdf0e10cSrcweir 				RegistryKeyNotFoundException
189cdf0e10cSrcweir 				RegistryAccessDeniedException
190cdf0e10cSrcweir 	*/
191cdf0e10cSrcweir 	virtual RegistryKey OpenSubKey(const std::wstring& Name, bool Writeable = true) = 0;
192cdf0e10cSrcweir 
193cdf0e10cSrcweir 	/** Creates a new sub-key below the key at hand
194cdf0e10cSrcweir 
195cdf0e10cSrcweir 		@precond IsOpen = true
196cdf0e10cSrcweir 				 IsWriteable = true
197cdf0e10cSrcweir 
198cdf0e10cSrcweir 		@throws  RegistryIOException
199cdf0e10cSrcweir 				 RegistryWriteAccessDenyException
200cdf0e10cSrcweir 	*/
201cdf0e10cSrcweir 	virtual RegistryKey CreateSubKey(const std::wstring& Name) = 0;
202cdf0e10cSrcweir 
203cdf0e10cSrcweir 	/** Deletes a sub-key below the key at hand, the
204cdf0e10cSrcweir 		key must not have sub-keys
205cdf0e10cSrcweir 
206cdf0e10cSrcweir 		@precond IsOpen = true
207cdf0e10cSrcweir 				 IsWriteable = true
208cdf0e10cSrcweir 
209cdf0e10cSrcweir 		@throws  RegistryIOException
210cdf0e10cSrcweir 				 RegistryWriteAccessDenyException
211cdf0e10cSrcweir 	*/
212cdf0e10cSrcweir 	virtual void DeleteSubKey(const std::wstring& Name) = 0;
213cdf0e10cSrcweir 
214cdf0e10cSrcweir 	/** Deletes a sub-key below the key at hand with all
215cdf0e10cSrcweir 		its sub-keys
216cdf0e10cSrcweir 
217cdf0e10cSrcweir 		@precond IsOpen = true
218cdf0e10cSrcweir 				 IsWriteable = true;
219cdf0e10cSrcweir 
220cdf0e10cSrcweir 		@throws  RegistryIOException
221cdf0e10cSrcweir 				 RegistryWriteAccessDenyException
222cdf0e10cSrcweir 	*/
223cdf0e10cSrcweir 	virtual void DeleteSubKeyTree(const std::wstring& Name) = 0;
224cdf0e10cSrcweir 
225cdf0e10cSrcweir 	/** Delete the specified value
226cdf0e10cSrcweir 
227cdf0e10cSrcweir 		@precond IsOpen = true
228cdf0e10cSrcweir 				 IsWriteable = true
229cdf0e10cSrcweir 				 HasValue(Name) = true
230cdf0e10cSrcweir 
231cdf0e10cSrcweir 		@throws	RegistryIOException
232cdf0e10cSrcweir 				RegistryWriteAccessDeniedException
233cdf0e10cSrcweir 				RegistryValueNotFoundException
234cdf0e10cSrcweir 	*/
235cdf0e10cSrcweir 	virtual void DeleteValue(const std::wstring& Name) = 0;
236cdf0e10cSrcweir 
237cdf0e10cSrcweir 	/** Set the specified registry value
238cdf0e10cSrcweir 
239cdf0e10cSrcweir 		@precond IsOpen = true
240cdf0e10cSrcweir 				 IsWriteable = true
241cdf0e10cSrcweir 
242cdf0e10cSrcweir 		@throws  RegistryIOException
243cdf0e10cSrcweir 				 RegistryWriteAccessDenyException
244cdf0e10cSrcweir 	*/
245cdf0e10cSrcweir 	virtual void SetValue(const RegistryValue& Value) = 0;
246cdf0e10cSrcweir 
247cdf0e10cSrcweir 
248cdf0e10cSrcweir 	/** Copies the specified value from RegistryKey to
249cdf0e10cSrcweir 		the registry key at hand, if a value with this
250cdf0e10cSrcweir 		name already exist under the registry key at hand
251cdf0e10cSrcweir 		it will be overwritten
252cdf0e10cSrcweir 
253cdf0e10cSrcweir 		@precond IsOpen = true
254cdf0e10cSrcweir 				 IsWriteable = true
255cdf0e10cSrcweir 				 RegistryKey.HasSubValue(Name) = true
256cdf0e10cSrcweir 
257cdf0e10cSrcweir 		@throws RegistryIOException
258cdf0e10cSrcweir 				RegistryWriteAccessDeniedException
259cdf0e10cSrcweir 				RegistryValueNotFoundException
260cdf0e10cSrcweir 	*/
261cdf0e10cSrcweir 	virtual void CopyValue(const RegistryKey& RegistryKey, const std::wstring& Name);
262cdf0e10cSrcweir 
263cdf0e10cSrcweir 	/** Copies the specified value from RegistryKey to
264cdf0e10cSrcweir 		the registry key at hand under a new name,
265cdf0e10cSrcweir 		if a value with this name already exist there
266cdf0e10cSrcweir 		it will be overwritten
267cdf0e10cSrcweir 
268cdf0e10cSrcweir 		@precond IsOpen = true
269cdf0e10cSrcweir 				 IsWriteable = true
270cdf0e10cSrcweir 				 RegistryKey.HasSubValue(Name) = true
271cdf0e10cSrcweir 
272cdf0e10cSrcweir 		@throws RegistryIOException
273cdf0e10cSrcweir 				RegistryWriteAccessDeniedException
274cdf0e10cSrcweir 				RegistryValueNotFoundException
275cdf0e10cSrcweir 	*/
276cdf0e10cSrcweir 	virtual void CopyValue(const RegistryKey& RegistryKey, const std::wstring& Name, const std::wstring& NewName);
277cdf0e10cSrcweir 
278cdf0e10cSrcweir 	//############################################
279cdf0e10cSrcweir 	// Creation
280cdf0e10cSrcweir 	// only possible through WindowsRegistry class
281cdf0e10cSrcweir 	//############################################
282cdf0e10cSrcweir 
283cdf0e10cSrcweir 
284cdf0e10cSrcweir protected:
285cdf0e10cSrcweir 	/** Create instance of the specified Registry key
286cdf0e10cSrcweir 
287cdf0e10cSrcweir 		@throws  RegistryWriteAccessDenyException
288cdf0e10cSrcweir 				 RegistryAccessDenyException
289cdf0e10cSrcweir 				 RegistryKeyNotFoundException
290cdf0e10cSrcweir 	*/
291cdf0e10cSrcweir     RegistryKeyImpl(HKEY RootKey, const std::wstring& KeyName);
292cdf0e10cSrcweir 
293cdf0e10cSrcweir 	/** Create instance of the specified Registry key.
294cdf0e10cSrcweir 		RootKey should only one of the predefined
295cdf0e10cSrcweir 		keys HKEY_CLASSES_ROOT, HKEY_CURRENT_USER,
296cdf0e10cSrcweir 		HKEY_LOCAL_MACHINE, HKEY_USERS
297cdf0e10cSrcweir 
298cdf0e10cSrcweir 		@throws  RegistryWriteAccessDenyException
299cdf0e10cSrcweir 				 RegistryAccessDenyException
300cdf0e10cSrcweir 				 RegistryKeyNotFoundException
301cdf0e10cSrcweir 	*/
302cdf0e10cSrcweir 	RegistryKeyImpl(HKEY RootKey);
303cdf0e10cSrcweir 
304cdf0e10cSrcweir 	/** Create an instances of the specified Registry key,
305cdf0e10cSrcweir 		the key is assumed to be already opened.
306cdf0e10cSrcweir 	*/
307cdf0e10cSrcweir 	RegistryKeyImpl(HKEY RootKey, HKEY SubKey, const std::wstring& KeyName, bool Writeable = true);
308cdf0e10cSrcweir 
309cdf0e10cSrcweir 	/** Is this one of the root keys
310cdf0e10cSrcweir 		HKEY_CLASSES_ROOT
311cdf0e10cSrcweir 		HKEY_CURRENT_USER
312cdf0e10cSrcweir 		etc.
313cdf0e10cSrcweir 	*/
314cdf0e10cSrcweir 	bool IsRootKey() const;
315cdf0e10cSrcweir 
316cdf0e10cSrcweir protected:
317cdf0e10cSrcweir     HKEY			m_hRootKey;
318cdf0e10cSrcweir     HKEY		    m_hSubKey;
319cdf0e10cSrcweir 	std::wstring	m_KeyName;
320cdf0e10cSrcweir 	bool				m_IsWriteable;
321cdf0e10cSrcweir 
322cdf0e10cSrcweir // prevent copy and assignment
323cdf0e10cSrcweir private:
324cdf0e10cSrcweir     RegistryKeyImpl(const RegistryKeyImpl&);
325cdf0e10cSrcweir     RegistryKeyImpl& operator=(const RegistryKeyImpl&);
326cdf0e10cSrcweir 
327cdf0e10cSrcweir //######################################
328cdf0e10cSrcweir // Friend declarations
329cdf0e10cSrcweir //######################################
330cdf0e10cSrcweir 
331cdf0e10cSrcweir friend class WindowsRegistry;
332cdf0e10cSrcweir };
333cdf0e10cSrcweir 
334cdf0e10cSrcweir #endif
335