1 // Copyright 2019 The SwiftShader Authors. 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 #ifndef VK_DEBUG_FILE_HPP_
16 #define VK_DEBUG_FILE_HPP_
17
18 #include "ID.hpp"
19
20 #include <memory>
21 #include <string>
22 #include <unordered_set>
23
24 namespace vk {
25 namespace dbg {
26
27 class File
28 {
29 public:
30 using ID = dbg::ID<File>;
31
32 // createVirtual() returns a new file that is not backed by the filesystem.
33 // name is the name of the file.
34 // source is the content of the file.
35 static std::shared_ptr<File> createVirtual(ID id, std::string name, std::string source);
36
37 // createPhysical() returns a new file that is backed by the file at path.
38 static std::shared_ptr<File> createPhysical(ID id, std::string path);
39
40 // clearBreakpoints() removes all the breakpoints set on the file.
41 // This function and addBreakpoint() is safe to call concurrently on
42 // multiple threads.
43 virtual void clearBreakpoints() = 0;
44
45 // addBreakpoint() adds a new line breakpoint at the line with the given
46 // index.
47 // This function and clearBreakpoints() is safe to call concurrently on
48 // multiple threads.
49 virtual void addBreakpoint(int line) = 0;
50
51 // hasBreakpoint() returns true iff the file has a breakpoint set at the
52 // line with the given index.
53 virtual bool hasBreakpoint(int line) const = 0;
54
55 // getBreakpoints() returns all the breakpoints set in the file.
56 virtual std::unordered_set<int> getBreakpoints() const = 0;
57
58 // isVirtual() returns true iff the file is not backed by the filesystem.
59 virtual bool isVirtual() const = 0;
60
61 // path() returns the path to the file, if backed by the filesystem,
62 // otherwise and empty string.
63 inline std::string path() const;
64
65 // The unique identifier of the file.
66 const ID id;
67
68 // The directory of file if backed by the filesystem, otherwise an empty string.
69 const std::string dir;
70
71 // The name of the file.
72 const std::string name;
73
74 // The source of the file if not backed by the filesystem, otherwise an empty string.
75 const std::string source;
76
77 virtual ~File() = default;
78
79 protected:
80 inline File(ID id, std::string dir, std::string name, std::string source);
81 };
82
File(ID id,std::string dir,std::string name,std::string source)83 File::File(ID id, std::string dir, std::string name, std::string source)
84 : id(std::move(id))
85 , dir(std::move(dir))
86 , name(std::move(name))
87 , source(source)
88 {}
89
path() const90 std::string File::path() const
91 {
92 return (dir.size() > 0) ? (dir + "/" + name) : name;
93 }
94
95 } // namespace dbg
96 } // namespace vk
97
98 #endif // VK_DEBUG_FILE_HPP_
99