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 | |
Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame^] | 5 | #ifndef CHROMEOS_PLATFORM_UPDATE_ENGINE_UPDATE_MANAGER_EVALUATION_CONTEXT_INL_H_ |
| 6 | #define CHROMEOS_PLATFORM_UPDATE_ENGINE_UPDATE_MANAGER_EVALUATION_CONTEXT_INL_H_ |
Alex Deymo | 23949d4 | 2014-02-05 15:20:59 -0800 | [diff] [blame] | 7 | |
| 8 | #include <base/logging.h> |
| 9 | |
Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame^] | 10 | namespace chromeos_update_manager { |
Alex Deymo | 23949d4 | 2014-02-05 15:20:59 -0800 | [diff] [blame] | 11 | |
| 12 | template<typename T> |
| 13 | const T* EvaluationContext::GetValue(Variable<T>* var) { |
Alex Deymo | cc0e5cf | 2014-04-23 20:20:11 -0700 | [diff] [blame] | 14 | if (var == nullptr) { |
Alex Deymo | 23949d4 | 2014-02-05 15:20:59 -0800 | [diff] [blame] | 15 | LOG(ERROR) << "GetValue received an uninitialized variable."; |
Alex Deymo | cc0e5cf | 2014-04-23 20:20:11 -0700 | [diff] [blame] | 16 | return nullptr; |
Alex Deymo | 23949d4 | 2014-02-05 15:20:59 -0800 | [diff] [blame] | 17 | } |
| 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 Deymo | cc0e5cf | 2014-04-23 20:20:11 -0700 | [diff] [blame] | 27 | if (result == nullptr) { |
Alex Deymo | 23949d4 | 2014-02-05 15:20:59 -0800 | [diff] [blame] | 28 | LOG(WARNING) << "Error reading Variable " << var->GetName() << ": \"" |
| 29 | << errmsg << "\""; |
Alex Deymo | 23949d4 | 2014-02-05 15:20:59 -0800 | [diff] [blame] | 30 | } |
Alex Deymo | cc0e5cf | 2014-04-23 20:20:11 -0700 | [diff] [blame] | 31 | // 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 Deymo | 23949d4 | 2014-02-05 15:20:59 -0800 | [diff] [blame] | 36 | return result; |
| 37 | } |
| 38 | |
Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame^] | 39 | } // namespace chromeos_update_manager |
Alex Deymo | 23949d4 | 2014-02-05 15:20:59 -0800 | [diff] [blame] | 40 | |
Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame^] | 41 | #endif // CHROMEOS_PLATFORM_UPDATE_ENGINE_UPDATE_MANAGER_EVALUATION_CONTEXT_INL_H_ |