blob: 7f76981a0b76e9e2db4b64a719a91a904e2a29e6 [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
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 Deymo81f30e82014-01-08 14:33:06 -080042
Alex Deymo81f30e82014-01-08 14:33:06 -080043 protected:
Gilad Arnoldb33e1982014-01-27 14:46:27 -080044 friend class PmCopyVariableTest;
45 FRIEND_TEST(PmCopyVariableTest, SimpleTest);
46 FRIEND_TEST(PmCopyVariableTest, UseCopyConstructorTest);
Alex Deymo81f30e82014-01-08 14:33:06 -080047
48 // Variable override.
Gilad Arnoldb33e1982014-01-27 14:46:27 -080049 virtual const T* GetValue(base::TimeDelta /* timeout */,
50 std::string* /* errmsg */) {
51 return new T(ref_);
52 }
Alex Deymo81f30e82014-01-08 14:33:06 -080053
54 private:
55 // Reference to the object to be copied by GetValue().
56 const T& ref_;
57};
58
59} // namespace chromeos_policy_manager
60
Alex Deymo81f30e82014-01-08 14:33:06 -080061#endif // CHROMEOS_PLATFORM_UPDATE_ENGINE_POLICY_MANAGER_GENERIC_VARIABLES_H