Sane C++ Libraries
C++ Platform Abstraction Libraries
Loading...
Searching...
No Matches
SerialPortError.h
1// Copyright (c) Stefano Cristiano
2// SPDX-License-Identifier: MIT
3#pragma once
4#include "../Common/Result.h"
5
6namespace SC
7{
10
12enum class SerialPortError : uint32_t
13{
14 InvalidBaudRate = 1,
15 InvalidDataBits,
16 InvalidParity,
17 InvalidStopBits,
18 InvalidFlowControl,
19 InvalidHandle,
20 UnsupportedPathEncoding,
21 InvalidPath,
22 PathMustBeAbsolute,
23 OpenFailed,
24 ReadSettingsFailed,
25 SetSettingsFailed,
26 UnsupportedHardwareFlowControl,
27 UnsupportedBaudRate,
28 UnsupportedDataBits,
29 UnsupportedParity,
30 UnsupportedStopBits,
31 PathTooLong,
32};
33
37enum class SerialPortErrorDetail : uint32_t
38{
39 None = 0,
40 PosixOpen,
41 PosixReadDescriptorFlags,
42 PosixSetDescriptorFlags,
43 PosixReadSettings,
44 PosixSetInputBaudRate,
45 PosixSetOutputBaudRate,
46 PosixSetSettings,
47 WindowsCreateFile,
48 WindowsSetTimeouts,
49 WindowsReadSettings,
50 WindowsSetSettings,
51};
52
54static constexpr ResultCategory SerialPortResultCategory = ResultCategory(3);
55
59struct [[nodiscard]] ResultSerialPort
60{
61 Result result;
62 SerialPortErrorDetail detail = SerialPortErrorDetail::None;
63 uint32_t nativeError = 0;
64
65 explicit constexpr ResultSerialPort(bool valid = true) : result(valid) {}
66 constexpr ResultSerialPort(SerialPortError error, SerialPortErrorDetail detail = SerialPortErrorDetail::None,
67 uint32_t nativeError = 0)
68 : result(Result::Error(SerialPortResultCategory, error)), detail(detail), nativeError(nativeError)
69 {}
70 constexpr ResultSerialPort(Result result) : result(result) {}
71
72 template <typename ResultLike>
73 constexpr ResultSerialPort(const ResultLike& other) : result(other.toResult())
74 {}
75
76 explicit constexpr operator bool() const { return static_cast<bool>(result); }
77
78 constexpr operator Result() const { return result; }
79
80 constexpr Result toResult() const { return result; }
81 constexpr bool isError(SerialPortError error) const { return result.isError(SerialPortResultCategory, error); }
82};
83
85} // namespace SC
SerialPortErrorDetail
Stable lower-level and backend-specific context for a SerialPort error.
Definition SerialPortError.h:38
SerialPortError
Stable error codes returned by the SerialPort library.
Definition SerialPortError.h:13
SerialPort result retaining optional lower-level and native error details.
Definition SerialPortError.h:60