Gilad Arnold | 63e726a | 2014-01-28 22:28:19 -0800 | [diff] [blame] | 1 | // 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 Arnold | 1c9b67b | 2014-01-29 15:14:31 -0800 | [diff] [blame] | 8 | // 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 Arnold | 63e726a | 2014-01-28 22:28:19 -0800 | [diff] [blame] | 18 | namespace 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. |
| 28 | template<typename T> |
| 29 | class 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 |