cobia/cape_registry_key_writer.rs
1use crate::C;
2use crate::*;
3
4/// A writable COBIA registry key
5///
6/// This struct is used to write to a COBIA registry key. It is obtained
7/// from a `CapeRegistryWriter` and can be used to
8/// create subkeys and write values. It is valid only for the life
9/// span of the CapeRegistryWriter.
10///
11/// Values are not written to the registry until CapeRegistryWriter::commit() is
12/// called.
13
14pub struct CapeRegistryKeyWriter<'a> {
15 pub(crate) interface: *mut C::ICapeRegistryKeyWriter,
16 pub(crate) writer: &'a CapeRegistryWriter //cannot use the key after the writer is deleted....
17}
18
19impl<'a> CapeRegistryKeyWriter<'a> {
20
21 /// Create a subkey
22 ///
23 /// Create a subkey with the given name. The subkey is returned as a
24 /// CapeRegistryKeyWriter. The subkey can be used to write values and
25 /// create further subkeys. The subkey is not written to the registry
26 /// until CapeRegistryWriter::commit() is called.
27 ///
28 /// # Arguments
29 ///
30 /// * `key_name` - The name of the subkey to create
31 ///
32 /// # Returns
33 ///
34 /// A CapeRegistryKeyWriter for the new subkey
35 ///
36 /// # Example
37 ///
38 /// ```
39 /// use cobia::*;
40 /// cobia::cape_open_initialize().unwrap();
41 /// {
42 /// //create a key, then a sub key, and put a value in there
43 /// let writer = CapeRegistryWriter::new(false).unwrap();
44 /// let key=writer.create_key("/cobia_rust_create_sub_key").unwrap();
45 /// let sub_key=key.create_sub_key(&writer,"sub_key").unwrap();
46 /// sub_key.set_string_value("test_value", "test_value").unwrap();
47 /// writer.commit().unwrap();
48 /// }
49 /// //read the value back
50 /// let reader = CapeRegistryKey::from_path("/cobia_rust_create_sub_key/sub_key").unwrap();
51 /// assert_eq!(reader.get_string_value("test_value",None).unwrap(), "test_value".to_string());
52 /// // now delete the entire key
53 /// let writer = CapeRegistryWriter::new(false).unwrap();
54 /// writer.delete_key("/cobia_rust_create_sub_key").unwrap();
55 /// writer.commit().unwrap();
56 /// //validate that it was deleted
57 /// let reader = CapeRegistryKey::from_path("/cobia_rust_create_sub_key");
58 /// assert!(reader.is_err());
59 /// cobia::cape_open_cleanup();
60 /// ```
61
62 pub fn create_sub_key(&self, writer: &'a CapeRegistryWriter, key_name: &str) -> Result<CapeRegistryKeyWriter<'_>, COBIAError> {
63 let mut key: *mut C::ICapeRegistryKeyWriter = std::ptr::null_mut();
64 let result = unsafe {
65 ((*(*self.interface).vTbl).createSubKey.unwrap())(
66 (*self.interface).me,
67 CapeStringImpl::from_string(key_name)
68 .as_capechar_const()
69 .cast_mut(),
70 &mut key as *mut *mut C::ICapeRegistryKeyWriter,
71 )
72 };
73 if result == COBIAERR_NOERROR {
74 Ok(CapeRegistryKeyWriter { interface: key, writer })
75 } else {
76 Err(COBIAError::Code(result))
77 }
78 }
79
80 /// Delete a subkey
81 ///
82 /// Delete a subkey with the given name. The result is not written to the
83 /// registry until CapeRegistryWriter::commit() is called.
84 ///
85 /// # Arguments
86 ///
87 /// * `key_name` - The name of the subkey to delete
88 ///
89 /// # Example
90 ///
91 /// ```
92 /// use cobia::*;
93 /// cobia::cape_open_initialize().unwrap();
94 /// {
95 /// //create a key, then a sub key, and put a value in there
96 /// let writer = CapeRegistryWriter::new(false).unwrap();
97 /// let key=writer.create_key("/cobia_rust_delete_sub_key").unwrap();
98 /// let sub_key=key.create_sub_key(&writer,"sub_key").unwrap();
99 /// sub_key.set_string_value("test_value", "test_value").unwrap();
100 /// writer.commit().unwrap();
101 /// }
102 /// //read the value back
103 /// let reader = CapeRegistryKey::from_path("/cobia_rust_delete_sub_key/sub_key").unwrap();
104 /// assert_eq!(reader.get_string_value("test_value",None).unwrap(), "test_value".to_string());
105 /// // now delete the sub key
106 /// let writer = CapeRegistryWriter::new(false).unwrap();
107 /// let key=writer.create_key("/cobia_rust_delete_sub_key").unwrap();
108 /// key.delete_sub_key("sub_key").unwrap();
109 /// writer.commit().unwrap();
110 /// //validate that it was deleted
111 /// let reader = CapeRegistryKey::from_path("/cobia_rust_delete_sub_key/sub_key");
112 /// assert!(reader.is_err());
113 /// // now delete the entire key
114 /// let writer = CapeRegistryWriter::new(false).unwrap();
115 /// writer.delete_key("/cobia_rust_delete_sub_key").unwrap();
116 /// writer.commit().unwrap();
117 /// //validate that it was deleted
118 /// let reader = CapeRegistryKey::from_path("/cobia_rust_delete_sub_key");
119 /// assert!(reader.is_err());
120 /// cobia::cape_open_cleanup();
121 /// ```
122
123 pub fn delete_sub_key(&self, key_name: &str) -> Result<(), COBIAError> {
124 let result = unsafe {
125 ((*(*self.interface).vTbl).deleteSubKey.unwrap())(
126 (*self.interface).me,
127 CapeStringImpl::from_string(key_name)
128 .as_capechar_const()
129 .cast_mut(),
130 )
131 };
132 if result == COBIAERR_NOERROR {
133 Ok(())
134 } else {
135 Err(COBIAError::Code(result))
136 }
137 }
138
139 /// Delete a value
140 ///
141 /// Delete a value with the given name. The result is not written to the
142 /// registry until CapeRegistryWriter::commit() is called.
143 ///
144 /// # Arguments
145 ///
146 /// * `key_name` - The name of the subkey to delete; if None, the current key is used
147 /// * `value_name` - The name of the value to delete
148 ///
149 /// # Example
150 ///
151 /// ```
152 /// use cobia::*;
153 /// cobia::cape_open_initialize().unwrap();
154 /// {
155 /// //create a key, and put a value in there
156 /// let writer = CapeRegistryWriter::new(false).unwrap();
157 /// let key=writer.create_key("/cobia_rust_delete_value").unwrap();
158 /// key.set_string_value("test_string_value", "I like COBIA").unwrap();
159 /// writer.commit().unwrap();
160 /// }
161 /// //read the value back
162 /// let reader = CapeRegistryKey::from_path("/cobia_rust_delete_value").unwrap();
163 /// assert_eq!(reader.get_string_value("test_string_value",None).unwrap(), "I like COBIA".to_string());
164 /// // now delete the value
165 /// let writer = CapeRegistryWriter::new(false).unwrap();
166 /// let key=writer.create_key("/cobia_rust_delete_value").unwrap();
167 /// key.delete_value(None,"test_string_value").unwrap();
168 /// writer.commit().unwrap();
169 /// //validate that it was deleted
170 /// let reader = CapeRegistryKey::from_path("/cobia_rust_delete_value").unwrap();
171 /// let value = reader.get_string_value("test_string_value",None);
172 /// assert!(value.is_err());
173 /// // now delete the entire key
174 /// let writer = CapeRegistryWriter::new(false).unwrap();
175 /// writer.delete_key("/cobia_rust_delete_value").unwrap();
176 /// writer.commit().unwrap();
177 /// cobia::cape_open_cleanup();
178 /// ```
179
180 pub fn delete_value(&self, key_name: Option<&str>, value_name: &str) -> Result<(), COBIAError> {
181 let key_name = {
182 if let Some(key_name) = key_name {
183 CapeStringImpl::from_string(key_name).as_capechar_const()
184 } else {
185 std::ptr::null()
186 }
187 };
188 let result = unsafe {
189 ((*(*self.interface).vTbl).deleteValue.unwrap())(
190 (*self.interface).me,
191 key_name.cast_mut(),
192 CapeStringImpl::from_string(value_name)
193 .as_capechar_const()
194 .cast_mut(),
195 )
196 };
197 if result == COBIAERR_NOERROR {
198 Ok(())
199 } else {
200 Err(COBIAError::Code(result))
201 }
202 }
203
204 /// Write a string value
205 ///
206 /// Write a string value with the given name. The result is not written to the
207 /// registry until CapeRegistryWriter::commit() is called.
208 ///
209 /// # Arguments
210 ///
211 /// * `value_name` - The name of the value to write
212 /// * `value` - The value to write
213 ///
214 /// # Example
215 ///
216 /// ```
217 /// use cobia::*;
218 /// cobia::cape_open_initialize().unwrap();
219 /// {
220 /// //create a key, and put a value in there
221 /// let writer = CapeRegistryWriter::new(false).unwrap();
222 /// let key=writer.create_key("/cobia_rust_set_string_value").unwrap();
223 /// key.set_string_value("another_string_value", "CAPE-OPEN is the defacto interop standard for chemical engineering software components").unwrap();
224 /// writer.commit().unwrap();
225 /// }
226 /// //read the value back
227 /// let reader = CapeRegistryKey::from_path("/cobia_rust_set_string_value").unwrap();
228 /// assert_eq!(reader.get_string_value("another_string_value",None).unwrap(), "CAPE-OPEN is the defacto interop standard for chemical engineering software components".to_string());
229 /// // now delete the entire key
230 /// let writer = CapeRegistryWriter::new(false).unwrap();
231 /// writer.delete_key("/cobia_rust_set_string_value").unwrap();
232 /// writer.commit().unwrap();
233 /// cobia::cape_open_cleanup();
234 /// ```
235
236 pub fn set_string_value<T1: AsRef<str>,T2: AsRef<str>>(&self, value_name: T1, value: T2) -> Result<(), COBIAError> {
237 let value_name=value_name.as_ref();
238 let value=value.as_ref();
239 let result = unsafe {
240 ((*(*self.interface).vTbl).putStringValue.unwrap())(
241 (*self.interface).me,
242 CapeStringImpl::from_string(value_name)
243 .as_capechar_const()
244 .cast_mut(),
245 CapeStringImpl::from_string(value)
246 .as_capechar_const()
247 .cast_mut(),
248 )
249 };
250 if result == COBIAERR_NOERROR {
251 Ok(())
252 } else {
253 Err(COBIAError::Code(result))
254 }
255 }
256
257 /// Write an integer value
258 ///
259 /// Write an integer value with the given name. The result is not written to the
260 /// registry until CapeRegistryWriter::commit() is called.
261 ///
262 /// # Arguments
263 ///
264 /// * `value_name` - The name of the value to write
265 /// * `value` - The value to write
266 ///
267 /// # Example
268 ///
269 /// ```
270 /// use cobia::*;
271 /// cobia::cape_open_initialize().unwrap();
272 /// {
273 /// //create a key, and put a value in there
274 /// let writer = CapeRegistryWriter::new(false).unwrap();
275 /// let key=writer.create_key("/cobia_rust_put_integer_value").unwrap();
276 /// key.put_integer_value("my_integer_value", 14).unwrap();
277 /// writer.commit().unwrap();
278 /// }
279 /// //read the value back
280 /// assert_eq!(CapeRegistryKey::from_path("/cobia_rust_put_integer_value").unwrap().
281 /// get_integer_value("my_integer_value",None).unwrap(), 14);
282 /// // now delete the entire key
283 /// let writer = CapeRegistryWriter::new(false).unwrap();
284 /// writer.delete_key("/cobia_rust_put_integer_value").unwrap();
285 /// writer.commit().unwrap();
286 /// cobia::cape_open_cleanup();
287 /// ```
288
289 pub fn put_integer_value(&self, value_name: &str, value: i32) -> Result<(), COBIAError> {
290 let result = unsafe {
291 ((*(*self.interface).vTbl).putIntegerValue.unwrap())(
292 (*self.interface).me,
293 CapeStringImpl::from_string(value_name)
294 .as_capechar_const()
295 .cast_mut(),
296 value,
297 )
298 };
299 if result == COBIAERR_NOERROR {
300 Ok(())
301 } else {
302 Err(COBIAError::Code(result))
303 }
304 }
305
306 /// Write an uuid value
307 ///
308 /// Write an uuid value with the given name. The result is not written to the
309 /// registry until CapeRegistryWriter::commit() is called.
310 ///
311 /// # Arguments
312 ///
313 /// * `value_name` - The name of the value to write
314 /// * `value` - The value to write
315 ///
316 /// # Example
317 ///
318 /// ```
319 /// use cobia::*;
320 /// cobia::cape_open_initialize().unwrap();
321 /// let test_uuid = CapeUUID::new(); //generate unique uuid
322 /// {
323 /// //create a key, and put a value in there
324 /// let writer = CapeRegistryWriter::new(false).unwrap();
325 /// let key=writer.create_key("/cobia_rust_put_uuid_value").unwrap();
326 /// key.put_uuid_value("new_uuid", &test_uuid).unwrap();
327 /// writer.commit().unwrap();
328 /// }
329 /// //read the value back
330 /// assert_eq!(CapeRegistryKey::from_path("/cobia_rust_put_uuid_value").unwrap().
331 /// get_uuid_value("new_uuid",None).unwrap(), test_uuid);
332 /// // now delete the entire key
333 /// let writer = CapeRegistryWriter::new(false).unwrap();
334 /// writer.delete_key("/cobia_rust_put_uuid_value").unwrap();
335 /// writer.commit().unwrap();
336 /// cobia::cape_open_cleanup();
337 /// ```
338
339 pub fn put_uuid_value(&self, value_name: &str, value: &CapeUUID) -> Result<(), COBIAError> {
340 let result = unsafe {
341 ((*(*self.interface).vTbl).putUUIDValue.unwrap())(
342 (*self.interface).me,
343 CapeStringImpl::from_string(value_name)
344 .as_capechar_const()
345 .cast_mut(),
346 value as *const C::CapeUUID,
347 )
348 };
349 if result == COBIAERR_NOERROR {
350 Ok(())
351 } else {
352 Err(COBIAError::Code(result))
353 }
354 }
355
356 /// Write an empty value
357 ///
358 /// Write an uuid value with the given name. The result is not written to the
359 /// registry until CapeRegistryWriter::commit() is called.
360 ///
361 /// Empty values are typically used to denote an option that is either
362 /// set (value present) or not set (value not present).
363 ///
364 /// # Arguments
365 ///
366 /// * `value_name` - The name of the value to write
367 ///
368 /// # Example
369 ///
370 /// ```
371 /// use cobia::*;
372 /// cobia::cape_open_initialize().unwrap();
373 /// let test_uuid = CapeUUID::new(); //generate unique uuid
374 /// {
375 /// //create a key, and put a value in there
376 /// let writer = CapeRegistryWriter::new(false).unwrap();
377 /// let key=writer.create_key("/cobia_rust_put_empty_value").unwrap();
378 /// key.put_empty_value("test_empty_value").unwrap();
379 /// writer.commit().unwrap();
380 /// }
381 /// //check existence and type
382 /// assert_eq!(CapeRegistryKey::from_path("/cobia_rust_put_empty_value").unwrap().
383 /// get_value_type("test_empty_value",None).unwrap(), cobia::CapeRegistryValueType::Empty);
384 /// // now delete the entire key
385 /// let writer = CapeRegistryWriter::new(false).unwrap();
386 /// writer.delete_key("/cobia_rust_put_empty_value").unwrap();
387 /// writer.commit().unwrap();
388 /// cobia::cape_open_cleanup();
389 /// ```
390
391 pub fn put_empty_value(&self, value_name: &str) -> Result<(), COBIAError> {
392 let result = unsafe {
393 ((*(*self.interface).vTbl).putEmptyValue.unwrap())(
394 (*self.interface).me,
395 CapeStringImpl::from_string(value_name)
396 .as_capechar_const()
397 .cast_mut(),
398 )
399 };
400 if result == COBIAERR_NOERROR {
401 Ok(())
402 } else {
403 Err(COBIAError::Code(result))
404 }
405 }
406}
407
408impl<'a> CapeRegistryKeyReaderKey for CapeRegistryKeyWriter<'a> {
409 fn get_read_key(&self) -> *mut C::ICapeRegistryKey {
410 //cast
411 let p = self.interface as *mut C::ICapeRegistryKeyWriter;
412 unsafe {
413 std::mem::transmute::<*mut C::ICapeRegistryKeyWriter, *mut C::ICapeRegistryKey>(p)
414 }
415 }
416}
417
418impl<'a> CapeRegistryKeyReader for CapeRegistryKeyWriter<'a> {}
419
420/// Release pointer
421///
422/// ICapeRegistryKeyWriter derives from ICobiaBase, which contains
423/// addReference() and release(). The Drop trait calls release.
424
425impl<'a> Drop for CapeRegistryKeyWriter<'a> {
426 fn drop(&mut self) {
427 unsafe {
428 ((*(*self.interface).vTbl).base.base.release.unwrap())((*self.interface).me);
429 }
430 }
431}
432
433/// Add pointer reference
434///
435/// ICapeRegistryKeyWriter derives from ICobiaBase, which contains
436/// addReference() and release(). The Clone trait calls addReference.
437
438impl<'a> Clone for CapeRegistryKeyWriter<'a> {
439 fn clone(&self) -> Self {
440 unsafe {
441 ((*(*self.interface).vTbl).base.base.addReference.unwrap())((*self.interface).me);
442 }
443 CapeRegistryKeyWriter {
444 interface: self.interface,
445 writer: self.writer
446 }
447 }
448}