winapi_common
file.hpp
1 // Copyright (c) 2020 Egor Tensin <Egor.Tensin@gmail.com>
2 // This file is part of the "winapi-common" project.
3 // For details, see https://github.com/egor-tensin/winapi-common.
4 // Distributed under the MIT License.
5 
6 #pragma once
7 
8 #include "handle.hpp"
9 #include "path.hpp"
10 
11 #include <boost/functional/hash.hpp>
12 
13 #include <windows.h>
14 
15 #include <cstddef>
16 #include <functional>
17 #include <string>
18 #include <utility>
19 
20 namespace winapi {
21 
22 bool operator==(const FILE_ID_128& a, const FILE_ID_128& b);
23 
29 class File : public Handle {
30 public:
31  struct ID {
32  const FILE_ID_INFO impl;
33 
34  bool operator==(const ID& other) const {
35  return impl.VolumeSerialNumber == other.impl.VolumeSerialNumber &&
36  impl.FileId == other.impl.FileId;
37  }
38  };
39 
41  static File open_r(const std::string&);
43  static File open_r(const CanonicalPath&);
45  static File open_read_attributes(const std::string&);
49  static File open_w(const std::string&);
51  static File open_w(const CanonicalPath&);
52 
54  static void remove(const std::string&);
56  static void remove(const CanonicalPath&);
57 
59  explicit File(Handle&& handle) : Handle{std::move(handle)} {}
60 
65  std::size_t get_size() const;
66 
71  ID query_id() const;
72 };
73 
74 } // namespace winapi
75 
76 namespace std {
77 
78 template <>
79 struct hash<winapi::File::ID> {
80  std::size_t operator()(const winapi::File::ID& id) const {
81  std::size_t seed = 0;
82  boost::hash_combine(seed, id.impl.VolumeSerialNumber);
83  boost::hash_combine(seed, id.impl.FileId.Identifier);
84  return seed;
85  }
86 };
87 
88 } // namespace std
Absolute, canonical path.
Definition: path.hpp:13
File I/O.
Definition: file.hpp:29
static File open_w(const std::string &)
Definition: file.cpp:108
static void remove(const std::string &)
Definition: file.cpp:116
File(Handle &&handle)
Definition: file.hpp:59
static File open_read_attributes(const std::string &)
Definition: file.cpp:100
static File open_r(const std::string &)
Definition: file.cpp:92
std::size_t get_size() const
Definition: file.cpp:124
ID query_id() const
Definition: file.cpp:139
HANDLE wrapper.
Definition: handle.hpp:25