blob: 5284ab2d1c5cd2c17f6d911df703cd24aa95bab2 [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
Gilad Arnold1c9b67b2014-01-29 15:14:31 -08008// Convenience macro for declaring policy variable closers.
9//
10// TODO(garnold) If/when we switch to C++11, this can already initialize the
11// closer with the variable's address and save us further work in the ctor.
12//
13// TODO(garnold) This is likely unnecessary once we introduce a non-template
14// variable base class.
15#define DECLARE_VAR_CLOSER(closer_name, var_name) \
16 ScopedPtrVarCloser<typeof(var_name)> closer_name
17
Gilad Arnold63e726a2014-01-28 22:28:19 -080018namespace chromeos_policy_manager {
19
20// Scoped closer for a pointer variable. It is initialized with a reference to
21// a pointer variable. Upon destruction, it will destruct the object pointed to
22// by the variable and nullify the variable. This template can be easily
23// instantiated via 'typeof' of the variable that is being scoped:
24//
25// ScopedPtrVarCloser<typeof(foo)> foo_closer(&foo);
26//
27// where 'foo' is pointer variable of some type.
28template<typename T>
29class ScopedPtrVarCloser {
30 public:
31 ScopedPtrVarCloser(T* ptr_var_p) : ptr_var_p_(ptr_var_p) {}
32 ~ScopedPtrVarCloser() {
33 if (ptr_var_p_) {
34 delete *ptr_var_p_;
35 *ptr_var_p_ = NULL;
36 }
37 }
38
39 private:
40 T* ptr_var_p_;
41};
42
43} // namespace chromeos_policy_manager
44
45#endif // CHROMEOS_PLATFORM_UPDATE_ENGINE_POLICY_MANAGER_PROVIDER_UTILS_H