WPILibC++ 2027.0.0-alpha-2
Loading...
Searching...
No Matches
Types.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/**
10 * @defgroup hal_types Type Definitions
11 * @ingroup hal_capi
12 * @{
13 */
14
15#define HAL_kInvalidHandle 0
16
17typedef int32_t HAL_Handle;
18
20
22
24
26
28
30
32
34
36
38
40
42
44
46
48
50
52
54
56
58
60
62
64
66
68
70
72
73typedef int32_t HAL_Bool;
74
75#ifdef __cplusplus
76#define HAL_ENUM_WITH_UNDERLYING_TYPE(name, type) enum name : type
77#elif defined(__clang__)
78#define HAL_ENUM_WITH_UNDERLYING_TYPE(name, type) \
79 enum name : type; \
80 typedef enum name name; \
81 enum name : type
82#else
83#define HAL_ENUM_WITH_UNDERLYING_TYPE(name, type) \
84 typedef type name; \
85 enum name
86#endif
87
88#define HAL_ENUM(name) HAL_ENUM_WITH_UNDERLYING_TYPE(name, int32_t)
89
90#ifdef __cplusplus
91namespace hal {
92/**
93 * A move-only C++ wrapper around a HAL handle.
94 * Will free the handle if FreeFunction is provided
95 */
96template <typename CType, void (*FreeFunction)(CType) = nullptr,
97 int32_t CInvalid = HAL_kInvalidHandle>
98class Handle {
99 public:
100 Handle() = default;
101 /*implicit*/ Handle(CType val) : m_handle(val) {} // NOLINT
102
103 Handle(const Handle&) = delete;
104 Handle& operator=(const Handle&) = delete;
105
106 Handle(Handle&& rhs) : m_handle(rhs.m_handle) { rhs.m_handle = CInvalid; }
107
109 m_handle = rhs.m_handle;
110 rhs.m_handle = CInvalid;
111 return *this;
112 }
113
115// FIXME: GCC gives the false positive "the address of <GetDefault> will never
116// be NULL" because it doesn't realize the default template parameter can make
117// GetDefault nullptr. Fixed in GCC 13.
118// https://gcc.gnu.org/bugzilla/show_bug.cgi?id=94554
119// https://gcc.gnu.org/bugzilla/show_bug.cgi?id=105885
120#if __GNUC__
121#pragma GCC diagnostic push
122#pragma GCC diagnostic ignored "-Waddress"
123#endif // __GNUC__
124 if constexpr (FreeFunction != nullptr) {
125#if __GNUC__
126#pragma GCC diagnostic pop
127#endif // __GNUC__
128 if (m_handle != CInvalid) {
129 FreeFunction(m_handle);
130 }
131 }
132 }
133
134 operator CType() const { return m_handle; } // NOLINT
135
136 private:
137 CType m_handle = CInvalid;
138};
139
140} // namespace hal
141#endif
142/** @} */
A move-only C++ wrapper around a HAL handle.
Definition Types.h:98
Handle & operator=(Handle &&rhs)
Definition Types.h:108
Handle()=default
Handle(Handle &&rhs)
Definition Types.h:106
Handle(const Handle &)=delete
Handle(CType val)
Definition Types.h:101
~Handle()
Definition Types.h:114
Handle & operator=(const Handle &)=delete
HAL_Handle HAL_CompressorHandle
Definition Types.h:25
HAL_Handle HAL_CANHandle
Definition Types.h:49
HAL_Handle HAL_CTREPCMHandle
Definition Types.h:65
HAL_CANHandle HAL_PDPHandle
Definition Types.h:61
HAL_Handle HAL_SerialPortHandle
Definition Types.h:47
int32_t HAL_Bool
Definition Types.h:73
HAL_Handle HAL_SolenoidHandle
Definition Types.h:45
int32_t HAL_Handle
Definition Types.h:17
HAL_Handle HAL_DMAHandle
Definition Types.h:55
HAL_Handle HAL_AnalogTriggerHandle
Definition Types.h:23
HAL_Handle HAL_SimDeviceHandle
Definition Types.h:51
HAL_Handle HAL_AnalogOutputHandle
Definition Types.h:21
HAL_Handle HAL_CANStreamHandle
Definition Types.h:71
HAL_Handle HAL_FPGAEncoderHandle
Definition Types.h:35
HAL_Handle HAL_DigitalPWMHandle
Definition Types.h:31
HAL_Handle HAL_GyroHandle
Definition Types.h:37
HAL_Handle HAL_EncoderHandle
Definition Types.h:33
HAL_Handle HAL_DutyCycleHandle
Definition Types.h:57
HAL_Handle HAL_PowerDistributionHandle
Definition Types.h:63
HAL_Handle HAL_InterruptHandle
Definition Types.h:39
HAL_Handle HAL_DigitalHandle
Definition Types.h:29
HAL_Handle HAL_CounterHandle
Definition Types.h:27
HAL_Handle HAL_REVPHHandle
Definition Types.h:69
HAL_Handle HAL_RelayHandle
Definition Types.h:43
HAL_Handle HAL_AnalogInputHandle
Definition Types.h:19
HAL_Handle HAL_NotifierHandle
Definition Types.h:41
HAL_Handle HAL_REVPDHHandle
Definition Types.h:67
HAL_Handle HAL_AddressableLEDHandle
Definition Types.h:59
HAL_Handle HAL_SimValueHandle
Definition Types.h:53
#define HAL_kInvalidHandle
Definition Types.h:15
WPILib Hardware Abstraction Layer (HAL) namespace.
Definition SimCallbackRegistry.h:16