/* * Copyright (c) 2024 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #ifndef WINDOW_WINDOW_MANAGER_SCREEN_CACHE_H #define WINDOW_WINDOW_MANAGER_SCREEN_CACHE_H #include #include #include #include namespace OHOS::Rosen { template class ScreenCache { public: ScreenCache(size_t capacity, ValueType errorCode); void Set(const KeyType& key, const ValueType& value); ValueType Get(const KeyType& key); private: std::unordered_map Map_; std::list accessOrder_; const size_t capacity_; const ValueType errorCode_; std::mutex mtx_; }; template ScreenCache::ScreenCache(size_t capacity, ValueType errorCode) : capacity_(capacity), errorCode_(errorCode) { } template void ScreenCache::Set(const KeyType& key, const ValueType& value) { std::lock_guard guard(mtx_); auto it = Map_.find(key); if (it != Map_.end()) { accessOrder_.erase(std::find(accessOrder_.begin(), accessOrder_.end(), key)); } else { if (Map_.size() >= capacity_) { KeyType lastKey = accessOrder_.back(); accessOrder_.pop_back(); Map_.erase(lastKey); } } Map_[key] = value; accessOrder_.push_front(key); } template ValueType ScreenCache::Get(const KeyType& key) { std::lock_guard guard(mtx_); auto it = Map_.find(key); if (it != Map_.end()) { accessOrder_.erase(std::find(accessOrder_.begin(), accessOrder_.end(), key)); accessOrder_.push_front(key); return it->second; } return errorCode_; } } // namespace OHOS::Rosen #endif // WINDOW_WINDOW_MANAGER_SCREEN_CACHE_H