blob: 6a05550e7ce2a035e0d586bfa18bc645d3325774 [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:
Shinichiro Hamaji6e6de8d2015-06-18 11:12:58 +090035 MakefileCacheManagerImpl() {
36 g_instance = this;
37 }
38
Shinichiro Hamaji776ca302015-06-06 03:52:48 +090039 virtual ~MakefileCacheManagerImpl() {
40 for (auto p : cache_) {
41 delete p.second;
42 }
43 }
44
45 virtual Makefile* ReadMakefile(const string& filename) override {
46 Makefile* result = NULL;
47 auto p = cache_.insert(make_pair(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
Shinichiro Hamaji998ccf72015-07-18 12:37:02 +090056 virtual void GetAllFilenames(unordered_set<string>* out) override {
57 for (const auto& p : cache_)
58 out->insert(p.first);
59 }
60
Shinichiro Hamaji776ca302015-06-06 03:52:48 +090061private:
62 unordered_map<string, Makefile*> cache_;
63};
64
65MakefileCacheManager* NewMakefileCacheManager() {
66 return new MakefileCacheManagerImpl();
67}