Alex Deymo | 23949d4 | 2014-02-05 15:20:59 -0800 | [diff] [blame] | 1 | // 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 Arnold | 48415f1 | 2014-06-27 07:10:58 -0700 | [diff] [blame] | 5 | #ifndef UPDATE_ENGINE_UPDATE_MANAGER_EVALUATION_CONTEXT_INL_H_ |
| 6 | #define UPDATE_ENGINE_UPDATE_MANAGER_EVALUATION_CONTEXT_INL_H_ |
| 7 | |
| 8 | #include <string> |
Alex Deymo | 23949d4 | 2014-02-05 15:20:59 -0800 | [diff] [blame] | 9 | |
| 10 | #include <base/logging.h> |
| 11 | |
Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame] | 12 | namespace chromeos_update_manager { |
Alex Deymo | 23949d4 | 2014-02-05 15:20:59 -0800 | [diff] [blame] | 13 | |
| 14 | template<typename T> |
| 15 | const T* EvaluationContext::GetValue(Variable<T>* var) { |
Alex Deymo | cc0e5cf | 2014-04-23 20:20:11 -0700 | [diff] [blame] | 16 | if (var == nullptr) { |
Alex Deymo | 23949d4 | 2014-02-05 15:20:59 -0800 | [diff] [blame] | 17 | LOG(ERROR) << "GetValue received an uninitialized variable."; |
Alex Deymo | cc0e5cf | 2014-04-23 20:20:11 -0700 | [diff] [blame] | 18 | return nullptr; |
Alex Deymo | 23949d4 | 2014-02-05 15:20:59 -0800 | [diff] [blame] | 19 | } |
| 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 Arnold | f9f85d6 | 2014-06-19 18:07:01 -0700 | [diff] [blame] | 28 | const T* result = var->GetValue(RemainingTime(evaluation_monotonic_deadline_), |
| 29 | &errmsg); |
Alex Deymo | cc0e5cf | 2014-04-23 20:20:11 -0700 | [diff] [blame] | 30 | if (result == nullptr) { |
Alex Deymo | 23949d4 | 2014-02-05 15:20:59 -0800 | [diff] [blame] | 31 | LOG(WARNING) << "Error reading Variable " << var->GetName() << ": \"" |
| 32 | << errmsg << "\""; |
Alex Deymo | 23949d4 | 2014-02-05 15:20:59 -0800 | [diff] [blame] | 33 | } |
Alex Deymo | cc0e5cf | 2014-04-23 20:20:11 -0700 | [diff] [blame] | 34 | // 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( |
Yunlian Jiang | cef5cd6 | 2015-05-27 15:22:32 -0700 | [diff] [blame] | 37 | static_cast<BaseVariable*>(var), BoxedValue(result)); |
Alex Deymo | 23949d4 | 2014-02-05 15:20:59 -0800 | [diff] [blame] | 38 | return result; |
| 39 | } |
| 40 | |
Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame] | 41 | } // namespace chromeos_update_manager |
Alex Deymo | 23949d4 | 2014-02-05 15:20:59 -0800 | [diff] [blame] | 42 | |
Gilad Arnold | 48415f1 | 2014-06-27 07:10:58 -0700 | [diff] [blame] | 43 | #endif // UPDATE_ENGINE_UPDATE_MANAGER_EVALUATION_CONTEXT_INL_H_ |