blob: 8786c90cd8783947940179fc11f032c505218149 [file] [log] [blame]
Gilad Arnold63e726a2014-01-28 22:28:19 -08001// Copyright (c) 2014 The Chromium OS Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#ifndef CHROMEOS_PLATFORM_UPDATE_ENGINE_POLICY_MANAGER_PROVIDER_UTILS_H
6#define CHROMEOS_PLATFORM_UPDATE_ENGINE_POLICY_MANAGER_PROVIDER_UTILS_H
7
8namespace chromeos_policy_manager {
9
10// Scoped closer for a pointer variable. It is initialized with a reference to
11// a pointer variable. Upon destruction, it will destruct the object pointed to
12// by the variable and nullify the variable. This template can be easily
13// instantiated via 'typeof' of the variable that is being scoped:
14//
15// ScopedPtrVarCloser<typeof(foo)> foo_closer(&foo);
16//
17// where 'foo' is pointer variable of some type.
18template<typename T>
19class ScopedPtrVarCloser {
20 public:
21 ScopedPtrVarCloser(T* ptr_var_p) : ptr_var_p_(ptr_var_p) {}
22 ~ScopedPtrVarCloser() {
23 if (ptr_var_p_) {
24 delete *ptr_var_p_;
25 *ptr_var_p_ = NULL;
26 }
27 }
28
29 private:
30 T* ptr_var_p_;
31};
32
33} // namespace chromeos_policy_manager
34
35#endif // CHROMEOS_PLATFORM_UPDATE_ENGINE_POLICY_MANAGER_PROVIDER_UTILS_H