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