AU/PM: Easier destruction of provider variables
This affords as simple, automated and safe destruction of global
Variable pointer.
* We define a ScopedPtrVarCloser<T> class, which frees and nullifies the
pointer it is pointing to. This is placed in provider_utils.h (for
now), for use by provider implementations.
* Accordingly, it is straightforward to define a closer member in
a concrete provider implementation via the typeof directive (see
random_provider.h).
* It's now safe to remove trivial desctructors in Providers, e.g. the
one of RandomProvider.
BUG=None
TEST=Builds and passes unit tests.
Change-Id: I956a0b28a6ae6d1e22e669fdc4f28e9ac5c67415
Reviewed-on: https://chromium-review.googlesource.com/184353
Tested-by: Gilad Arnold <garnold@chromium.org>
Reviewed-by: Alex Deymo <deymo@chromium.org>
Commit-Queue: Gilad Arnold <garnold@chromium.org>
diff --git a/policy_manager/provider_utils.h b/policy_manager/provider_utils.h
new file mode 100644
index 0000000..8786c90
--- /dev/null
+++ b/policy_manager/provider_utils.h
@@ -0,0 +1,35 @@
+// Copyright (c) 2014 The Chromium OS Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#ifndef CHROMEOS_PLATFORM_UPDATE_ENGINE_POLICY_MANAGER_PROVIDER_UTILS_H
+#define CHROMEOS_PLATFORM_UPDATE_ENGINE_POLICY_MANAGER_PROVIDER_UTILS_H
+
+namespace chromeos_policy_manager {
+
+// Scoped closer for a pointer variable. It is initialized with a reference to
+// a pointer variable. Upon destruction, it will destruct the object pointed to
+// by the variable and nullify the variable. This template can be easily
+// instantiated via 'typeof' of the variable that is being scoped:
+//
+// ScopedPtrVarCloser<typeof(foo)> foo_closer(&foo);
+//
+// where 'foo' is pointer variable of some type.
+template<typename T>
+class ScopedPtrVarCloser {
+ public:
+ ScopedPtrVarCloser(T* ptr_var_p) : ptr_var_p_(ptr_var_p) {}
+ ~ScopedPtrVarCloser() {
+ if (ptr_var_p_) {
+ delete *ptr_var_p_;
+ *ptr_var_p_ = NULL;
+ }
+ }
+
+ private:
+ T* ptr_var_p_;
+};
+
+} // namespace chromeos_policy_manager
+
+#endif // CHROMEOS_PLATFORM_UPDATE_ENGINE_POLICY_MANAGER_PROVIDER_UTILS_H