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 | |
| 5 | // Generic and provider independent Variable subclasses. These variables can be |
| 6 | // used by any state provider to implement simple variables to avoid repeat the |
| 7 | // same common code on different state providers. |
| 8 | |
| 9 | #ifndef CHROMEOS_PLATFORM_UPDATE_ENGINE_POLICY_MANAGER_GENERIC_VARIABLES_H |
| 10 | #define CHROMEOS_PLATFORM_UPDATE_ENGINE_POLICY_MANAGER_GENERIC_VARIABLES_H |
| 11 | |
| 12 | #include "policy_manager/variable.h" |
| 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 | // |
| 22 | // For example, a state provider exposing a private method as a variable could |
| 23 | // be implemented in this way: |
| 24 | // |
| 25 | // * On something_vars.h: |
| 26 | // Variable<MyType>* var_something; |
| 27 | // |
| 28 | // * On something_provider: |
| 29 | // class SomethingProvider { |
| 30 | // public: |
| 31 | // SomethingProvider(...) { |
| 32 | // var_something = new CopyVariable<MyType>(priv_object_); |
| 33 | // } |
| 34 | // ~SomethingProvider() { |
| 35 | // delete var_something; |
| 36 | // var_something = NULL; |
| 37 | // } |
| 38 | // private: |
| 39 | // MyType priv_object_; |
| 40 | // }; |
| 41 | template<typename T> |
| 42 | class CopyVariable : public Variable<T> { |
| 43 | public: |
| 44 | // Creates the variable returning copies of the passed |obj| reference. The |
| 45 | // reference to this object is kept and it should be available whenever the |
| 46 | // GetValue() method is called. |
| 47 | CopyVariable(const T& obj); |
| 48 | |
| 49 | virtual ~CopyVariable() {} |
| 50 | |
| 51 | protected: |
| 52 | friend class PMCopyVariableTest; |
| 53 | FRIEND_TEST(PMCopyVariableTest, SimpleTest); |
| 54 | FRIEND_TEST(PMCopyVariableTest, UseCopyConstructorTest); |
| 55 | |
| 56 | // Variable override. |
| 57 | virtual const T* GetValue(base::TimeDelta timeout, std::string* errmsg); |
| 58 | |
| 59 | private: |
| 60 | // Reference to the object to be copied by GetValue(). |
| 61 | const T& ref_; |
| 62 | }; |
| 63 | |
| 64 | } // namespace chromeos_policy_manager |
| 65 | |
| 66 | // Include implementation on header files for templates. |
| 67 | #include "policy_manager/generic_variables-inl.h" |
| 68 | |
| 69 | #endif // CHROMEOS_PLATFORM_UPDATE_ENGINE_POLICY_MANAGER_GENERIC_VARIABLES_H |