blob: 325513844bd604edbf0a5dfb69c9d9092477a24a [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 Arnold48415f12014-06-27 07:10:58 -07005#ifndef UPDATE_ENGINE_UPDATE_MANAGER_EVALUATION_CONTEXT_INL_H_
6#define UPDATE_ENGINE_UPDATE_MANAGER_EVALUATION_CONTEXT_INL_H_
7
8#include <string>
Alex Deymo23949d42014-02-05 15:20:59 -08009
10#include <base/logging.h>
11
Alex Deymo63784a52014-05-28 10:46:14 -070012namespace chromeos_update_manager {
Alex Deymo23949d42014-02-05 15:20:59 -080013
14template<typename T>
15const T* EvaluationContext::GetValue(Variable<T>* var) {
Alex Deymocc0e5cf2014-04-23 20:20:11 -070016 if (var == nullptr) {
Alex Deymo23949d42014-02-05 15:20:59 -080017 LOG(ERROR) << "GetValue received an uninitialized variable.";
Alex Deymocc0e5cf2014-04-23 20:20:11 -070018 return nullptr;
Alex Deymo23949d42014-02-05 15:20:59 -080019 }
20
21 // Search for the value on the cache first.
22 ValueCacheMap::iterator it = value_cache_.find(var);
23 if (it != value_cache_.end())
24 return reinterpret_cast<const T*>(it->second.value());
25
26 // Get the value from the variable if not found on the cache.
27 std::string errmsg;
Gilad Arnoldf9f85d62014-06-19 18:07:01 -070028 const T* result = var->GetValue(RemainingTime(evaluation_monotonic_deadline_),
29 &errmsg);
Alex Deymocc0e5cf2014-04-23 20:20:11 -070030 if (result == nullptr) {
Alex Deymo23949d42014-02-05 15:20:59 -080031 LOG(WARNING) << "Error reading Variable " << var->GetName() << ": \""
32 << errmsg << "\"";
Alex Deymo23949d42014-02-05 15:20:59 -080033 }
Alex Deymocc0e5cf2014-04-23 20:20:11 -070034 // Cache the value for the next time. The map of CachedValues keeps the
35 // ownership of the pointer until the map is destroyed.
36 value_cache_.emplace(
37 static_cast<BaseVariable*>(var),
38 std::move(BoxedValue(result)));
Alex Deymo23949d42014-02-05 15:20:59 -080039 return result;
40}
41
Alex Deymo63784a52014-05-28 10:46:14 -070042} // namespace chromeos_update_manager
Alex Deymo23949d42014-02-05 15:20:59 -080043
Gilad Arnold48415f12014-06-27 07:10:58 -070044#endif // UPDATE_ENGINE_UPDATE_MANAGER_EVALUATION_CONTEXT_INL_H_