StormByte-System 3.0.0
C++26 process and environment module of the StormByte suite
Loading...
Searching...
No Matches
device.hxx
Go to the documentation of this file.
1/*
2 * Copyright (C) 2024-2026 David C. Manuelda (StormBytePP)
3 *
4 * This file is part of StormByte-System.
5 *
6 * StormByte-System original source is dual-licensed:
7 *
8 * 1. GNU Lesser General Public License v3.0 (or later)
9 * You may redistribute and/or modify this file under the terms of the
10 * GNU Lesser General Public License as published by the Free Software
11 * Foundation, either version 3 of the License, or (at your option)
12 * any later version.
13 *
14 * 2. Commercial license
15 * Alternatively, this file may be used under the terms of a commercial
16 * license agreement with the copyright holder
17 * (David C. Manuelda <StormByte@gmail.com>).
18 *
19 * Both licenses apply only to original StormByte-System source in this
20 * repository. They do not cover other StormByte modules or any third-party
21 * material shipped with this repository (including everything under
22 * thirdparty/, and in particular the bundled StormByte Base tree), which
23 * remains under its own license.
24 *
25 * Neither license grants any patent rights. Any patent licenses required
26 * to use this software or third-party components must be obtained separately
27 * from the patent holders.
28 *
29 * StormByte-System is distributed in the hope that it will be useful,
30 * but WITHOUT ANY WARRANTY; without even the implied warranty of
31 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
32 * GNU Lesser General Public License for more details.
33 *
34 * You should have received a copy of the GNU Lesser General Public License
35 * version 3 along with StormByte-System. If not, see
36 * <https://www.gnu.org/licenses/lgpl-3.0.html>.
37 *
38 * SPDX-License-Identifier: LGPL-3.0-or-later OR LicenseRef-StormByte-Commercial
39 */
40
41#pragma once
42
43#include <StormByte/bitmask.hxx>
44#include <StormByte/error.hxx>
45#include <StormByte/byte_size.hxx>
46#include <StormByte/safe/string.hxx>
48#include <StormByte/type_traits/safe.hxx>
49
50#include <cstdint>
51#include <filesystem>
52#include <string>
53#include <string_view>
54#include <system_error>
55
90 public:
97 enum class Error {
98 Success = 0,
99 BrokenSymlink,
100 DeviceNotFound,
101 NotADevice,
102 Permission,
103 ProbeFailed
104 };
105
112 enum class Kind {
113 HDD,
114 SSD,
115 NVMeGen3,
116 NVMeGen4,
117 NVMeGen5,
118 USBHDD,
119 USBStick,
120 Network
121 };
122
129 enum class AccessFlag: std::uint8_t {
130 Readable = 0x01,
131 Writable = 0x02
132 };
133
138 class STORMBYTE_SYSTEM_PUBLIC Access: public StormByte::Bitmask<Access, AccessFlag> {
139 public:
140 using Bitmask::Bitmask;
141 };
142
154
165
170
175 explicit Device(const StormByte::Safe::String& path) noexcept;
176
181 explicit Device(std::string_view path) noexcept;
182
187 explicit Device(std::wstring_view path) noexcept;
188
193 explicit Device(const std::filesystem::path& path) noexcept:
194#ifdef WINDOWS
195 Device(std::wstring_view(path.native())) {}
196#else
197 Device(std::string_view(path.native())) {}
198#endif
199
203 Device(const Device&) = default;
204
208 Device(Device&&) noexcept = default;
209
213 Device& operator=(const Device&) = default;
214
218 Device& operator=(Device&&) noexcept = default;
219
223 virtual ~Device() = default;
224
226
231
236 explicit operator bool() const noexcept;
237
242 StormByte::Error::Fault Fault() const noexcept;
243
248 const StormByte::Safe::String& Path() const noexcept;
249
251
256
261 enum Kind Kind() const noexcept;
262
267 class Access Access() const noexcept;
268
275 virtual struct Throughput Throughput() const noexcept;
276
283 virtual struct Window Window() const noexcept;
284
286
287 private:
288 StormByte::Safe::String m_path;
289 bool m_initialization_failed{false};
290 };
291}
292
296
300template<>
302 static constexpr const char* Name = "StormByte.System.Device";
303
310 switch (e) {
312 return "Success";
314 return "Broken symbolic link";
316 return "Device not found";
318 return "Path is not a classifiable device";
320 return "Device permission denied";
322 return "Device probe failed";
323 }
324 return "Unknown Device error";
325 }
326};
327
328namespace StormByte::System {
334
342 return std::error_code(static_cast<int>(e), device_category());
343 }
344}
345
346namespace std {
350 template<>
351 struct is_error_code_enum<StormByte::System::Device::Error>: true_type {};
352}
constexpr Bitmask() noexcept
Bitmask of AccessFlag.
Definition device.hxx:138
Query object for the medium behind a filesystem accessor.
Definition device.hxx:89
Error
Device classification and probe enumerators.
Definition device.hxx:97
@ NotADevice
The path exists but is not a classifiable file, directory or device node.
Definition device.hxx:101
@ ProbeFailed
Classification I/O failed.
Definition device.hxx:103
@ Success
No error.
Definition device.hxx:98
@ BrokenSymlink
The path is a symlink whose target does not exist.
Definition device.hxx:99
@ DeviceNotFound
No volume or node could be resolved.
Definition device.hxx:100
@ Permission
The process may not stat or access this path.
Definition device.hxx:102
const StormByte::Safe::String & Path() const noexcept
Accessor stored at construction.
Device(std::string_view path) noexcept
Store a UTF-8 accessor view.
Device(std::wstring_view path) noexcept
Store a wide accessor as UTF-8 text.
Device(const std::filesystem::path &path) noexcept
Store a filesystem path using its native path view.
Definition device.hxx:193
AccessFlag
Transfer permission of this path for this process.
Definition device.hxx:129
Device(Device &&) noexcept=default
Move constructor.
StormByte::Error::Fault Fault() const noexcept
Result of the current probe.
Kind
Classified storage or network medium.
Definition device.hxx:112
Device(const Device &)=default
Copy constructor.
Device(const StormByte::Safe::String &path) noexcept
Store an owned UTF-8 accessor.
STORMBYTE_DECLARE_MAYBE_SAFE(class StormByte::System::Device::Access)
System module of the StormByte suite.
Definition device.hxx:59
STORMBYTE_FORCE_INLINE std::error_code make_error_code(Device::Error e) noexcept
Builds an std::error_code from Device::Error.
Definition device.hxx:341
const StormByte::Error::Category< Device::Error > & device_category() noexcept
Category singleton for Device::Error.
STL namespace.
#define STORMBYTE_FORCE_INLINE
static STORMBYTE_FORCE_INLINE std::string Message(StormByte::System::Device::Error e)
Text for one Device enumerator.
Definition device.hxx:309
static constexpr const char * Name
Stable category tag.
Definition device.hxx:302
Nominal sequential rates of the resolved medium.
Definition device.hxx:150
StormByte::ByteSize write_bps
Nominal sequential write.
Definition device.hxx:152
StormByte::ByteSize read_bps
Nominal sequential read.
Definition device.hxx:151
Suggested transfer windows derived from Throughput.
Definition device.hxx:161
StormByte::ByteSize read
Suggested read window.
Definition device.hxx:162
StormByte::ByteSize write
Suggested write window.
Definition device.hxx:163
#define STORMBYTE_SYSTEM_PUBLIC
Definition visibility.h:53