WPILibC++ 2024.3.2
MappedFileRegion.h
Go to the documentation of this file.
1// Copyright (c) FIRST and other WPILib contributors.
2// Open Source Software; you can modify and/or share it under the terms of
3// the WPILib BSD license file in the root directory of this project.
4
5#pragma once
6
7#include <stdint.h>
8
9#include <system_error>
10
11// Duplicated from fs.h to avoid a dependency
12namespace fs {
13#if defined(_WIN32)
14// A Win32 HANDLE is a typedef of void*
15using file_t = void*;
16#else
17using file_t = int;
18#endif
19} // namespace fs
20
21namespace wpi {
22
24 public:
25 enum MapMode {
26 kReadOnly, ///< May only access map via const_data as read only.
27 kReadWrite, ///< May access map via data and modify it. Written to path.
28 kPriv ///< May modify via data, but changes are lost on destruction.
29 };
30
31 MappedFileRegion() = default;
32 MappedFileRegion(fs::file_t f, uint64_t length, uint64_t offset,
33 MapMode mapMode, std::error_code& ec);
35
38
40 : m_size(rhs.m_size), m_mapping(rhs.m_mapping) {
41 rhs.m_mapping = nullptr;
42#ifdef _WIN32
43 m_fileHandle = rhs.m_fileHandle;
44 rhs.m_fileHandle = nullptr;
45#endif
46 }
47
49 if (m_mapping) {
50 Unmap();
51 }
52 m_size = rhs.m_size;
53 m_mapping = rhs.m_mapping;
54 rhs.m_mapping = nullptr;
55#ifdef _WIN32
56 m_fileHandle = rhs.m_fileHandle;
57 rhs.m_fileHandle = nullptr;
58#endif
59 return *this;
60 }
61
62 explicit operator bool() const { return m_mapping != nullptr; }
63
64 void Flush();
65 void Unmap();
66
67 uint64_t size() const { return m_size; }
68 uint8_t* data() const { return static_cast<uint8_t*>(m_mapping); }
69 const uint8_t* const_data() const {
70 return static_cast<const uint8_t*>(m_mapping);
71 }
72
73 /**
74 * Returns required alignment.
75 */
76 static size_t GetAlignment();
77
78 private:
79 uint64_t m_size = 0;
80 void* m_mapping = nullptr;
81#ifdef _WIN32
82 fs::file_t m_fileHandle = nullptr;
83#endif
84};
85
86} // namespace wpi
Definition: MappedFileRegion.h:23
MappedFileRegion(fs::file_t f, uint64_t length, uint64_t offset, MapMode mapMode, std::error_code &ec)
~MappedFileRegion()
Definition: MappedFileRegion.h:34
MappedFileRegion(MappedFileRegion &&rhs)
Definition: MappedFileRegion.h:39
MappedFileRegion & operator=(const MappedFileRegion &)=delete
MappedFileRegion & operator=(MappedFileRegion &&rhs)
Definition: MappedFileRegion.h:48
static size_t GetAlignment()
Returns required alignment.
uint8_t * data() const
Definition: MappedFileRegion.h:68
MappedFileRegion(const MappedFileRegion &)=delete
MapMode
Definition: MappedFileRegion.h:25
@ kPriv
May modify via data, but changes are lost on destruction.
Definition: MappedFileRegion.h:28
@ kReadOnly
May only access map via const_data as read only.
Definition: MappedFileRegion.h:26
@ kReadWrite
May access map via data and modify it. Written to path.
Definition: MappedFileRegion.h:27
const uint8_t * const_data() const
Definition: MappedFileRegion.h:69
MappedFileRegion()=default
uint64_t size() const
Definition: MappedFileRegion.h:67
Definition: fs.h:21
int file_t
Definition: fs.h:32
Definition: ntcore_cpp.h:26