blob: fe16c3b58c6d212759ce6a1c8a057a9f699b4795 [file] [log] [blame]
Shinichiro Hamaji1d545aa2015-06-23 15:29:13 +09001// 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
Fumitoshi Ukai744bb2b2015-06-25 00:10:52 +090015// +build ignore
16
Shinichiro Hamaji776ca302015-06-06 03:52:48 +090017#include "file_cache.h"
18
19#include <unordered_map>
20
21#include "file.h"
22
Shinichiro Hamaji6e6de8d2015-06-18 11:12:58 +090023static MakefileCacheManager* g_instance;
24
Shinichiro Hamaji776ca302015-06-06 03:52:48 +090025MakefileCacheManager::MakefileCacheManager() {}
26
27MakefileCacheManager::~MakefileCacheManager() {}
28
Shinichiro Hamaji6e6de8d2015-06-18 11:12:58 +090029MakefileCacheManager* MakefileCacheManager::Get() {
30 return g_instance;
31}
32
Shinichiro Hamaji776ca302015-06-06 03:52:48 +090033class MakefileCacheManagerImpl : public MakefileCacheManager {
34 public:
Dan Willemsen3ce083f2017-10-11 22:17:48 -070035 MakefileCacheManagerImpl() { g_instance = this; }
Shinichiro Hamaji6e6de8d2015-06-18 11:12:58 +090036
Shinichiro Hamaji776ca302015-06-06 03:52:48 +090037 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;
Shinichiro Hamaji3deff5b2016-02-17 17:50:06 +090045 auto p = cache_.emplace(filename, result);
Shinichiro Hamaji776ca302015-06-06 03:52:48 +090046 if (p.second) {
47 p.first->second = result = new Makefile(filename);
48 } else {
49 result = p.first->second;
50 }
51 return result;
52 }
53
Shinichiro Hamaji998ccf72015-07-18 12:37:02 +090054 virtual void GetAllFilenames(unordered_set<string>* out) override {
55 for (const auto& p : cache_)
56 out->insert(p.first);
57 }
58
Dan Willemsen3ce083f2017-10-11 22:17:48 -070059 private:
Shinichiro Hamaji776ca302015-06-06 03:52:48 +090060 unordered_map<string, Makefile*> cache_;
61};
62
63MakefileCacheManager* NewMakefileCacheManager() {
64 return new MakefileCacheManagerImpl();
65}