blob: e78bbbdecaaef5b5e2d479db96e57b83048dbf41 [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
42 virtual ~CopyVariable() {}
43
44 protected:
Gilad Arnoldb33e1982014-01-27 14:46:27 -080045 friend class PmCopyVariableTest;
46 FRIEND_TEST(PmCopyVariableTest, SimpleTest);
47 FRIEND_TEST(PmCopyVariableTest, UseCopyConstructorTest);
Alex Deymo81f30e82014-01-08 14:33:06 -080048
49 // Variable override.
Gilad Arnoldb33e1982014-01-27 14:46:27 -080050 virtual const T* GetValue(base::TimeDelta /* timeout */,
51 std::string* /* errmsg */) {
52 return new T(ref_);
53 }
Alex Deymo81f30e82014-01-08 14:33:06 -080054
55 private:
56 // Reference to the object to be copied by GetValue().
57 const T& ref_;
58};
59
60} // namespace chromeos_policy_manager
61
Alex Deymo81f30e82014-01-08 14:33:06 -080062#endif // CHROMEOS_PLATFORM_UPDATE_ENGINE_POLICY_MANAGER_GENERIC_VARIABLES_H