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 MakefileCacheManager()25MakefileCacheManager::MakefileCacheManager() {} 26 ~MakefileCacheManager()27MakefileCacheManager::~MakefileCacheManager() {} 28 Get()29MakefileCacheManager* MakefileCacheManager::Get() { 30 return g_instance; 31 } 32 33 class MakefileCacheManagerImpl : public MakefileCacheManager { 34 public: MakefileCacheManagerImpl()35 MakefileCacheManagerImpl() { 36 g_instance = this; 37 } 38 ~MakefileCacheManagerImpl()39 virtual ~MakefileCacheManagerImpl() { 40 for (auto p : cache_) { 41 delete p.second; 42 } 43 } 44 ReadMakefile(const string & filename)45 virtual Makefile* ReadMakefile(const string& filename) override { 46 Makefile* result = NULL; 47 auto p = cache_.emplace(filename, result); 48 if (p.second) { 49 p.first->second = result = new Makefile(filename); 50 } else { 51 result = p.first->second; 52 } 53 return result; 54 } 55 GetAllFilenames(unordered_set<string> * out)56 virtual void GetAllFilenames(unordered_set<string>* out) override { 57 for (const auto& p : cache_) 58 out->insert(p.first); 59 } 60 61 private: 62 unordered_map<string, Makefile*> cache_; 63 }; 64 NewMakefileCacheManager()65MakefileCacheManager* NewMakefileCacheManager() { 66 return new MakefileCacheManagerImpl(); 67 } 68