Alex Deymo | 81f30e8 | 2014-01-08 14:33:06 -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 | |
Gilad Arnold | b33e198 | 2014-01-27 14:46:27 -0800 | [diff] [blame] | 5 | // Generic and provider-independent Variable subclasses. These variables can be |
Alex Deymo | 81f30e8 | 2014-01-08 14:33:06 -0800 | [diff] [blame] | 6 | // used by any state provider to implement simple variables to avoid repeat the |
| 7 | // same common code on different state providers. |
| 8 | |
Gilad Arnold | 2cbb385 | 2014-03-07 12:40:50 -0800 | [diff] [blame^] | 9 | #ifndef CHROMEOS_PLATFORM_UPDATE_ENGINE_POLICY_MANAGER_GENERIC_VARIABLES_H_ |
| 10 | #define CHROMEOS_PLATFORM_UPDATE_ENGINE_POLICY_MANAGER_GENERIC_VARIABLES_H_ |
Alex Deymo | 81f30e8 | 2014-01-08 14:33:06 -0800 | [diff] [blame] | 11 | |
Alex Deymo | 6e97bb2 | 2014-02-05 16:46:16 -0800 | [diff] [blame] | 12 | #include "update_engine/policy_manager/variable.h" |
Alex Deymo | 81f30e8 | 2014-01-08 14:33:06 -0800 | [diff] [blame] | 13 | |
| 14 | namespace chromeos_policy_manager { |
| 15 | |
| 16 | // Variable class returning a copy of a given object using the copy constructor. |
| 17 | // This template class can be used to define variables that expose as a variable |
| 18 | // any fixed object, such as the a provider's private member. The variable will |
| 19 | // create copies of the provided object using the copy constructor of that |
| 20 | // class. |
| 21 | // |
Gilad Arnold | b33e198 | 2014-01-27 14:46:27 -0800 | [diff] [blame] | 22 | // For example, a state provider exposing a private member as a variable can |
| 23 | // implement this as follows: |
Alex Deymo | 81f30e8 | 2014-01-08 14:33:06 -0800 | [diff] [blame] | 24 | // |
Alex Deymo | 81f30e8 | 2014-01-08 14:33:06 -0800 | [diff] [blame] | 25 | // class SomethingProvider { |
| 26 | // public: |
| 27 | // SomethingProvider(...) { |
Gilad Arnold | b33e198 | 2014-01-27 14:46:27 -0800 | [diff] [blame] | 28 | // var_something_foo = new CopyVariable<MyType>(foo_); |
Alex Deymo | 81f30e8 | 2014-01-08 14:33:06 -0800 | [diff] [blame] | 29 | // } |
Gilad Arnold | b33e198 | 2014-01-27 14:46:27 -0800 | [diff] [blame] | 30 | // ... |
Alex Deymo | 81f30e8 | 2014-01-08 14:33:06 -0800 | [diff] [blame] | 31 | // private: |
Gilad Arnold | b33e198 | 2014-01-27 14:46:27 -0800 | [diff] [blame] | 32 | // MyType foo_; |
Alex Deymo | 81f30e8 | 2014-01-08 14:33:06 -0800 | [diff] [blame] | 33 | // }; |
| 34 | template<typename T> |
| 35 | class CopyVariable : public Variable<T> { |
| 36 | public: |
| 37 | // Creates the variable returning copies of the passed |obj| reference. The |
| 38 | // reference to this object is kept and it should be available whenever the |
| 39 | // GetValue() method is called. |
Alex Deymo | 0e43369 | 2014-02-20 07:23:03 -0800 | [diff] [blame] | 40 | CopyVariable(const std::string& name, VariableMode mode, const T& ref) |
| 41 | : Variable<T>(name, mode), ref_(ref) {} |
Alex Deymo | a803393 | 2014-02-25 10:33:13 -0800 | [diff] [blame] | 42 | CopyVariable(const std::string& name, const base::TimeDelta& poll_interval, |
| 43 | const T& ref) |
| 44 | : Variable<T>(name, poll_interval), ref_(ref) {} |
Alex Deymo | 81f30e8 | 2014-01-08 14:33:06 -0800 | [diff] [blame] | 45 | |
Alex Deymo | 81f30e8 | 2014-01-08 14:33:06 -0800 | [diff] [blame] | 46 | protected: |
Gilad Arnold | b33e198 | 2014-01-27 14:46:27 -0800 | [diff] [blame] | 47 | friend class PmCopyVariableTest; |
| 48 | FRIEND_TEST(PmCopyVariableTest, SimpleTest); |
| 49 | FRIEND_TEST(PmCopyVariableTest, UseCopyConstructorTest); |
Alex Deymo | 81f30e8 | 2014-01-08 14:33:06 -0800 | [diff] [blame] | 50 | |
| 51 | // Variable override. |
Gilad Arnold | b33e198 | 2014-01-27 14:46:27 -0800 | [diff] [blame] | 52 | virtual const T* GetValue(base::TimeDelta /* timeout */, |
| 53 | std::string* /* errmsg */) { |
| 54 | return new T(ref_); |
| 55 | } |
Alex Deymo | 81f30e8 | 2014-01-08 14:33:06 -0800 | [diff] [blame] | 56 | |
| 57 | private: |
| 58 | // Reference to the object to be copied by GetValue(). |
| 59 | const T& ref_; |
| 60 | }; |
| 61 | |
| 62 | } // namespace chromeos_policy_manager |
| 63 | |
Gilad Arnold | 2cbb385 | 2014-03-07 12:40:50 -0800 | [diff] [blame^] | 64 | #endif // CHROMEOS_PLATFORM_UPDATE_ENGINE_POLICY_MANAGER_GENERIC_VARIABLES_H_ |