1
2
3
4
5
6 #include <cmajor/symbols/SourceFileModuleMap.hpp>
7 #include <cmajor/symbols/ModuleCache.hpp>
8 #include <boost/uuid/nil_generator.hpp>
9 #include <map>
10
11 namespace cmajor { namespace symbols {
12
13 struct SourceFileModuleMapKey
14 {
15 SourceFileModuleMapKey(sngcm::ast::BackEnd backend_, sngcm::ast::Config config_, const std::string& sourceFilePath_) : backend(backend_), config(config_), sourceFilePath(sourceFilePath_) {}
16 sngcm::ast::BackEnd backend;
17 sngcm::ast::Config config;
18 std::string sourceFilePath;
19 };
20
21 bool operator==(const SourceFileModuleMapKey& left, const SourceFileModuleMapKey& right)
22 {
23 return left.backend == right.backend && left.config == right.config && left.sourceFilePath == right.sourceFilePath;
24 }
25
26 bool operator<(const SourceFileModuleMapKey& left, const SourceFileModuleMapKey& right)
27 {
28 if (left.backend < right.backend) return true;
29 if (left.backend > right.backend) return false;
30 if (left.config < right.config) return true;
31 if (left.config > right.config) return false;
32 return left.sourceFilePath < right.sourceFilePath;
33 }
34
35 class SourceFileModuleMap
36 {
37 public:
38 static void Init();
39 static void Done();
40 static SourceFileModuleMap& Instance() { return *instance; }
41 void SetModuleId(sngcm::ast::BackEnd backend, sngcm::ast::Config config, const std::string& sourceFilePath, const boost::uuids::uuid& moduleId);
42 boost::uuids::uuid GetModuleId(sngcm::ast::BackEnd backend, sngcm::ast::Config config, const std::string& sourceFilePath) const;
43 private:
44 static std::unique_ptr<SourceFileModuleMap> instance;
45 std::map<SourceFileModuleMapKey, boost::uuids::uuid> sourceFileModuleMap;
46 SourceFileModuleMap();
47 };
48
49 std::unique_ptr<SourceFileModuleMap> SourceFileModuleMap::instance;
50
51 SourceFileModuleMap::SourceFileModuleMap()
52 {
53 }
54
55 void SourceFileModuleMap::Init()
56 {
57 instance.reset(new SourceFileModuleMap());
58 }
59
60 void SourceFileModuleMap::Done()
61 {
62 instance.reset();
63 }
64
65 void SourceFileModuleMap::SetModuleId(sngcm::ast::BackEnd backend, sngcm::ast::Config config, const std::string& sourceFilePath, const boost::uuids::uuid& moduleId)
66 {
67 SourceFileModuleMapKey key(backend, config, sourceFilePath);
68 sourceFileModuleMap[key] = moduleId;
69 }
70
71 boost::uuids::uuid SourceFileModuleMap::GetModuleId(sngcm::ast::BackEnd backend, sngcm::ast::Config config, const std::string& sourceFilePath) const
72 {
73 SourceFileModuleMapKey key(backend, config, sourceFilePath);
74 auto it = sourceFileModuleMap.find(key);
75 if (it != sourceFileModuleMap.cend())
76 {
77 return it->second;
78 }
79 else
80 {
81 return boost::uuids::nil_uuid();
82 }
83 }
84
85 void MapSourceFileToModuleId(sngcm::ast::BackEnd backend, sngcm::ast::Config config, const std::string& sourceFilePath, const boost::uuids::uuid& moduleId)
86 {
87 SourceFileModuleMap::Instance().SetModuleId(backend, config, sourceFilePath, moduleId);
88 }
89
90 Module* GetModuleBySourceFile(sngcm::ast::BackEnd backend, sngcm::ast::Config config, const std::string& sourceFilePath)
91 {
92 boost::uuids::uuid moduleId = SourceFileModuleMap::Instance().GetModuleId(backend, config, sourceFilePath);
93 if (moduleId.is_nil())
94 {
95 return nullptr;
96 }
97 else
98 {
99 return GetModuleById(moduleId);
100 }
101 }
102
103 void InitSourceFileModuleMap()
104 {
105 SourceFileModuleMap::Init();
106 }
107
108 void DoneSourceFileModuleMap()
109 {
110 SourceFileModuleMap::Done();
111 }
112
113 } }