blob: c8e38088bd71bbe608302c9c426421e7a5d68233 [file] [log] [blame]
Alex Deymo23949d42014-02-05 15:20:59 -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 Arnold2cbb3852014-03-07 12:40:50 -08005#ifndef CHROMEOS_PLATFORM_UPDATE_ENGINE_POLICY_MANAGER_EVALUATION_CONTEXT_INL_H_
6#define CHROMEOS_PLATFORM_UPDATE_ENGINE_POLICY_MANAGER_EVALUATION_CONTEXT_INL_H_
Alex Deymo23949d42014-02-05 15:20:59 -08007
8#include <base/logging.h>
9
10namespace chromeos_policy_manager {
11
12template<typename T>
13const T* EvaluationContext::GetValue(Variable<T>* var) {
Alex Deymocc0e5cf2014-04-23 20:20:11 -070014 if (var == nullptr) {
Alex Deymo23949d42014-02-05 15:20:59 -080015 LOG(ERROR) << "GetValue received an uninitialized variable.";
Alex Deymocc0e5cf2014-04-23 20:20:11 -070016 return nullptr;
Alex Deymo23949d42014-02-05 15:20:59 -080017 }
18
19 // Search for the value on the cache first.
20 ValueCacheMap::iterator it = value_cache_.find(var);
21 if (it != value_cache_.end())
22 return reinterpret_cast<const T*>(it->second.value());
23
24 // Get the value from the variable if not found on the cache.
25 std::string errmsg;
26 const T* result = var->GetValue(RemainingTime(), &errmsg);
Alex Deymocc0e5cf2014-04-23 20:20:11 -070027 if (result == nullptr) {
Alex Deymo23949d42014-02-05 15:20:59 -080028 LOG(WARNING) << "Error reading Variable " << var->GetName() << ": \""
29 << errmsg << "\"";
Alex Deymo23949d42014-02-05 15:20:59 -080030 }
Alex Deymocc0e5cf2014-04-23 20:20:11 -070031 // Cache the value for the next time. The map of CachedValues keeps the
32 // ownership of the pointer until the map is destroyed.
33 value_cache_.emplace(
34 static_cast<BaseVariable*>(var),
35 std::move(BoxedValue(result)));
Alex Deymo23949d42014-02-05 15:20:59 -080036 return result;
37}
38
39} // namespace chromeos_policy_manager
40
Gilad Arnold2cbb3852014-03-07 12:40:50 -080041#endif // CHROMEOS_PLATFORM_UPDATE_ENGINE_POLICY_MANAGER_EVALUATION_CONTEXT_INL_H_