1 // Copyright 2015 Google Inc. All rights reserved 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 // +build ignore 16 17 #include "file_cache.h" 18 19 #include <unordered_map> 20 21 #include "file.h" 22 23 static MakefileCacheManager* g_instance; 24 25 MakefileCacheManager::MakefileCacheManager() {} 26 27 MakefileCacheManager::~MakefileCacheManager() {} 28 29 MakefileCacheManager* MakefileCacheManager::Get() { 30 return g_instance; 31 } 32 33 class MakefileCacheManagerImpl : public MakefileCacheManager { 34 public: 35 MakefileCacheManagerImpl() { g_instance = this; } 36 37 virtual ~MakefileCacheManagerImpl() { 38 for (auto p : cache_) { 39 delete p.second; 40 } 41 } 42 43 virtual Makefile* ReadMakefile(const string& filename) override { 44 Makefile* result = NULL; 45 auto p = cache_.emplace(filename, result); 46 if (p.second) { 47 p.first->second = result = new Makefile(filename); 48 } else { 49 result = p.first->second; 50 } 51 return result; 52 } 53 54 virtual void GetAllFilenames(unordered_set<string>* out) override { 55 for (const auto& p : cache_) 56 out->insert(p.first); 57 } 58 59 private: 60 unordered_map<string, Makefile*> cache_; 61 }; 62 63 MakefileCacheManager* NewMakefileCacheManager() { 64 return new MakefileCacheManagerImpl(); 65 } 66