blob: 2f9b99669e90b8ed43a3d94245c134432505c2d7 [file] [log] [blame]
Alex Deymo81f30e82014-01-08 14:33:06 -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
Gilad Arnoldb33e1982014-01-27 14:46:27 -08005// Generic and provider-independent Variable subclasses. These variables can be
Alex Deymo81f30e82014-01-08 14:33:06 -08006// used by any state provider to implement simple variables to avoid repeat the
7// same common code on different state providers.
8
Gilad Arnold2cbb3852014-03-07 12:40:50 -08009#ifndef CHROMEOS_PLATFORM_UPDATE_ENGINE_POLICY_MANAGER_GENERIC_VARIABLES_H_
10#define CHROMEOS_PLATFORM_UPDATE_ENGINE_POLICY_MANAGER_GENERIC_VARIABLES_H_
Alex Deymo81f30e82014-01-08 14:33:06 -080011
Alex Deymo6e97bb22014-02-05 16:46:16 -080012#include "update_engine/policy_manager/variable.h"
Alex Deymo81f30e82014-01-08 14:33:06 -080013
14namespace 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 Arnoldb33e1982014-01-27 14:46:27 -080022// For example, a state provider exposing a private member as a variable can
23// implement this as follows:
Alex Deymo81f30e82014-01-08 14:33:06 -080024//
Alex Deymo81f30e82014-01-08 14:33:06 -080025// class SomethingProvider {
26// public:
27// SomethingProvider(...) {
Gilad Arnoldb33e1982014-01-27 14:46:27 -080028// var_something_foo = new CopyVariable<MyType>(foo_);
Alex Deymo81f30e82014-01-08 14:33:06 -080029// }
Gilad Arnoldb33e1982014-01-27 14:46:27 -080030// ...
Alex Deymo81f30e82014-01-08 14:33:06 -080031// private:
Gilad Arnoldb33e1982014-01-27 14:46:27 -080032// MyType foo_;
Alex Deymo81f30e82014-01-08 14:33:06 -080033// };
34template<typename T>
35class 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 Deymo0e433692014-02-20 07:23:03 -080040 CopyVariable(const std::string& name, VariableMode mode, const T& ref)
41 : Variable<T>(name, mode), ref_(ref) {}
Alex Deymoa8033932014-02-25 10:33:13 -080042 CopyVariable(const std::string& name, const base::TimeDelta& poll_interval,
43 const T& ref)
44 : Variable<T>(name, poll_interval), ref_(ref) {}
Alex Deymo81f30e82014-01-08 14:33:06 -080045
Alex Deymo81f30e82014-01-08 14:33:06 -080046 protected:
Gilad Arnoldb33e1982014-01-27 14:46:27 -080047 friend class PmCopyVariableTest;
48 FRIEND_TEST(PmCopyVariableTest, SimpleTest);
49 FRIEND_TEST(PmCopyVariableTest, UseCopyConstructorTest);
Alex Deymo81f30e82014-01-08 14:33:06 -080050
51 // Variable override.
Gilad Arnoldb33e1982014-01-27 14:46:27 -080052 virtual const T* GetValue(base::TimeDelta /* timeout */,
53 std::string* /* errmsg */) {
54 return new T(ref_);
55 }
Alex Deymo81f30e82014-01-08 14:33:06 -080056
57 private:
58 // Reference to the object to be copied by GetValue().
59 const T& ref_;
60};
61
Alex Deymobd04b142014-03-18 15:00:05 -070062// Variable class returning a constant value that is cached on the variable when
63// it is created.
64template<typename T>
65class ConstCopyVariable : public Variable<T> {
66 public:
67 // Creates the variable returning copies of the passed |obj|. The value passed
68 // is copied in this variable, and new copies of it will be returned by
69 // GetValue().
70 ConstCopyVariable(const std::string& name, const T& obj)
71 : Variable<T>(name, kVariableModeConst), obj_(obj) {}
72
73 protected:
74 friend class PmConstCopyVariableTest;
75 FRIEND_TEST(PmConstCopyVariableTest, SimpleTest);
76
77 // Variable override.
78 virtual const T* GetValue(base::TimeDelta /* timeout */,
79 std::string* /* errmsg */) {
80 return new T(obj_);
81 }
82
83 private:
84 // Value to be copied by GetValue().
85 const T obj_;
86};
87
Alex Deymo81f30e82014-01-08 14:33:06 -080088} // namespace chromeos_policy_manager
89
Gilad Arnold2cbb3852014-03-07 12:40:50 -080090#endif // CHROMEOS_PLATFORM_UPDATE_ENGINE_POLICY_MANAGER_GENERIC_VARIABLES_H_