blob: 32ea1b90b4bc040f695755ec950ddad48e2b5341 [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
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
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.
Gilad Arnoldb33e1982014-01-27 14:46:27 -080040 CopyVariable(const T& ref) : ref_(ref) {};
Alex Deymo81f30e82014-01-08 14:33:06 -080041
Alex Deymo81f30e82014-01-08 14:33:06 -080042 protected:
Gilad Arnoldb33e1982014-01-27 14:46:27 -080043 friend class PmCopyVariableTest;
44 FRIEND_TEST(PmCopyVariableTest, SimpleTest);
45 FRIEND_TEST(PmCopyVariableTest, UseCopyConstructorTest);
Alex Deymo81f30e82014-01-08 14:33:06 -080046
47 // Variable override.
Gilad Arnoldb33e1982014-01-27 14:46:27 -080048 virtual const T* GetValue(base::TimeDelta /* timeout */,
49 std::string* /* errmsg */) {
50 return new T(ref_);
51 }
Alex Deymo81f30e82014-01-08 14:33:06 -080052
53 private:
54 // Reference to the object to be copied by GetValue().
55 const T& ref_;
56};
57
58} // namespace chromeos_policy_manager
59
Alex Deymo81f30e82014-01-08 14:33:06 -080060#endif // CHROMEOS_PLATFORM_UPDATE_ENGINE_POLICY_MANAGER_GENERIC_VARIABLES_H