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