openMSX
Filename.hh
Go to the documentation of this file.
1 #ifndef FILENAME_HH
2 #define FILENAME_HH
3 
4 #include "FileContext.hh"
5 #include "FileOperations.hh"
6 #include <string>
7 
8 namespace openmsx {
9 
17 class Filename
18 {
19 public:
20  // dummy constructor, to be able to serialize vector<Filename>
21  Filename() = default;
22  Filename(Filename&&) = default;
23  Filename& operator=(Filename&&) = default;
24  Filename& operator=(const Filename&) = default;
25 
26  //workaround msvc bug(?)
27  //Filename(const Filename&) = default;
28  Filename(const Filename& f)
29  : originalFilename(f.originalFilename)
30  , resolvedFilename(f.resolvedFilename) {}
31 
32  // needed because the template below hides this version
33  Filename(Filename& f) : Filename(const_cast<const Filename&>(f)) {}
34 
35  template<typename String>
36  explicit Filename(String&& filename)
37  : originalFilename(std::forward<String>(filename))
38  , resolvedFilename(originalFilename) {}
39 
40  template<typename String>
42  : originalFilename(std::forward<String>(filename))
43  , resolvedFilename(FileOperations::getAbsolutePath(
44  context.resolve(originalFilename))) {}
45 
46  [[nodiscard]] const std::string& getOriginal() const { return originalFilename; }
47  [[nodiscard]] const std::string& getResolved() const & { return resolvedFilename; }
48  [[nodiscard]] std::string getResolved() && { return std::move(resolvedFilename); }
49 
55  void updateAfterLoadState();
56 
61  [[nodiscard]] bool empty() const;
62 
67  void setResolved(std::string resolved) {
68  resolvedFilename = std::move(resolved);
69  }
70 
71  // Do both Filename objects point to the same file?
72  [[nodiscard]] bool operator==(const Filename& other) const {
73  return resolvedFilename == other.resolvedFilename;
74  }
75  [[nodiscard]] bool operator!=(const Filename& other) const {
76  return !(*this == other);
77  }
78 
79  template<typename Archive>
80  void serialize(Archive& ar, unsigned version);
81 
82 private:
83  // non-const because we want this class to be assignable
84  // (to be able to store them in std::vector)
85  std::string originalFilename;
86  std::string resolvedFilename;
87 };
88 
89 } // namespace openmsx
90 
91 #endif
This class represents a filename.
Definition: Filename.hh:18
void setResolved(std::string resolved)
Change the resolved part of this filename E.g.
Definition: Filename.hh:67
Filename()=default
Filename(Filename &f)
Definition: Filename.hh:33
bool empty() const
Convenience method to test for empty filename.
Definition: Filename.cc:21
Filename(String &&filename, const FileContext &context)
Definition: Filename.hh:41
Filename(const Filename &f)
Definition: Filename.hh:28
Filename & operator=(const Filename &)=default
void serialize(Archive &ar, unsigned version)
Definition: Filename.cc:28
const std::string & getResolved() const &
Definition: Filename.hh:47
bool operator!=(const Filename &other) const
Definition: Filename.hh:75
const std::string & getOriginal() const
Definition: Filename.hh:46
Filename(Filename &&)=default
bool operator==(const Filename &other) const
Definition: Filename.hh:72
void updateAfterLoadState()
After a loadstate we prefer to use the exact same file as before savestate.
Definition: Filename.cc:8
Filename(String &&filename)
Definition: Filename.hh:36
std::string getResolved() &&
Definition: Filename.hh:48
Filename & operator=(Filename &&)=default
std::unique_ptr< Context > context
Definition: GLContext.cc:9
string getAbsolutePath(string_view path)
Transform given path into an absolute path.
This file implemented 3 utility functions:
Definition: Autofire.cc:9
constexpr const char *const filename