WPILibC++ 2023.4.3
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;
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 {
63 return m_mapping != nullptr;
64 }
65
66 void Flush();
67 void Unmap();
68
69 uint64_t size() const {
70 return m_size;
71 }
72 uint8_t* data() const {
73 return static_cast<uint8_t*>(m_mapping);
74 }
75 const uint8_t* const_data() const {
76 return static_cast<const uint8_t*>(m_mapping);
77 }
78
79 /**
80 * Returns required alignment.
81 */
82 static size_t GetAlignment();
83
84 private:
85 uint64_t m_size = 0;
86 void* m_mapping = nullptr;
87#ifdef _WIN32
88 fs::file_t m_fileHandle = nullptr;
89#endif
90};
91
92} // 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:72
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:75
MappedFileRegion()=default
uint64_t size() const
Definition: MappedFileRegion.h:69
::uint64_t uint64_t
Definition: Meta.h:58
::uint8_t uint8_t
Definition: Meta.h:52
Definition: fs.h:44
int file_t
Definition: fs.h:58
/file This file defines the SmallVector class.
Definition: AprilTagFieldLayout.h:18