WPILibC++ 2023.4.3
SimDeviceSim.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 <functional>
8#include <string>
9#include <vector>
10
11#include <hal/SimDevice.h>
13
14namespace frc::sim {
15
16/**
17 * Class to control the simulation side of a SimDevice.
18 */
20 public:
21 /**
22 * Constructs a SimDeviceSim.
23 *
24 * @param name name of the SimDevice
25 */
26 explicit SimDeviceSim(const char* name);
27
28 /**
29 * Constructs a SimDeviceSim.
30 *
31 * @param name name of the SimDevice
32 * @param index device index number to append to name
33 */
34 SimDeviceSim(const char* name, int index);
35
36 /**
37 * Constructs a SimDeviceSim.
38 *
39 * @param name name of the SimDevice
40 * @param index device index number to append to name
41 * @param channel device channel number to append to name
42 */
43 SimDeviceSim(const char* name, int index, int channel);
44
45 /**
46 * Constructs a SimDeviceSim.
47 *
48 * @param handle the low level handle for the corresponding SimDevice.
49 */
51
52 /**
53 * Get the name of this object.
54 *
55 * @return name
56 */
57 std::string GetName() const;
58
59 /**
60 * Get the property object with the given name.
61 *
62 * @param name the property name
63 * @return the property object
64 */
65 hal::SimValue GetValue(const char* name) const;
66
67 /**
68 * Get the property object with the given name.
69 *
70 * @param name the property name
71 * @return the property object
72 */
73 hal::SimInt GetInt(const char* name) const;
74
75 /**
76 * Get the property object with the given name.
77 *
78 * @param name the property name
79 * @return the property object
80 */
81 hal::SimLong GetLong(const char* name) const;
82
83 /**
84 * Get the property object with the given name.
85 *
86 * @param name the property name
87 * @return the property object
88 */
89 hal::SimDouble GetDouble(const char* name) const;
90
91 /**
92 * Get the property object with the given name.
93 *
94 * @param name the property name
95 * @return the property object
96 */
97 hal::SimEnum GetEnum(const char* name) const;
98
99 /**
100 * Get the property object with the given name.
101 *
102 * @param name the property name
103 * @return the property object
104 */
105 hal::SimBoolean GetBoolean(const char* name) const;
106
107 /**
108 * Get all options for the given enum.
109 *
110 * @param val the enum
111 * @return names of the different values for that enum
112 */
113 static std::vector<std::string> GetEnumOptions(hal::SimEnum val);
114
115 /**
116 * Get all properties.
117 *
118 * @param callback callback called for each property (SimValue). Signature
119 * of the callback must be const char*, HAL_SimValueHandle,
120 * int, const HAL_Value*
121 */
122 template <typename F>
123 void EnumerateValues(F callback) const {
125 m_handle, &callback,
126 [](const char* name, void* param, HAL_SimValueHandle handle,
127 int direction, const struct HAL_Value* value) {
128 std::invoke(*static_cast<F*>(param), name, handle, direction, value);
129 });
130 }
131
132 /**
133 * Get the raw handle of this object.
134 *
135 * @return the handle used to refer to this object
136 */
137 operator HAL_SimDeviceHandle() const { return m_handle; } // NOLINT
138
139 /**
140 * Get all sim devices with the given prefix.
141 *
142 * @param prefix the prefix to filter sim devices
143 * @param callback callback function to call for each sim device
144 */
145 template <typename F>
146 static void EnumerateDevices(const char* prefix, F callback) {
148 prefix, &callback,
149 [](const char* name, void* param, HAL_SimDeviceHandle handle) {
150 std::invoke(*static_cast<F*>(param), name, handle);
151 });
152 }
153
154 /**
155 * Reset all SimDevice data.
156 */
157 static void ResetData();
158
159 private:
160 HAL_SimDeviceHandle m_handle;
161};
162} // namespace frc::sim
void HALSIM_EnumerateSimValues(HAL_SimDeviceHandle device, void *param, HALSIM_SimValueCallback callback)
void HALSIM_EnumerateSimDevices(const char *prefix, void *param, HALSIM_SimDeviceCallback callback)
Class to control the simulation side of a SimDevice.
Definition: SimDeviceSim.h:19
hal::SimInt GetInt(const char *name) const
Get the property object with the given name.
static std::vector< std::string > GetEnumOptions(hal::SimEnum val)
Get all options for the given enum.
hal::SimEnum GetEnum(const char *name) const
Get the property object with the given name.
void EnumerateValues(F callback) const
Get all properties.
Definition: SimDeviceSim.h:123
SimDeviceSim(const char *name, int index)
Constructs a SimDeviceSim.
SimDeviceSim(const char *name)
Constructs a SimDeviceSim.
hal::SimBoolean GetBoolean(const char *name) const
Get the property object with the given name.
hal::SimDouble GetDouble(const char *name) const
Get the property object with the given name.
hal::SimLong GetLong(const char *name) const
Get the property object with the given name.
static void ResetData()
Reset all SimDevice data.
SimDeviceSim(const char *name, int index, int channel)
Constructs a SimDeviceSim.
SimDeviceSim(HAL_SimDeviceHandle handle)
Constructs a SimDeviceSim.
std::string GetName() const
Get the name of this object.
static void EnumerateDevices(const char *prefix, F callback)
Get all sim devices with the given prefix.
Definition: SimDeviceSim.h:146
hal::SimValue GetValue(const char *name) const
Get the property object with the given name.
Definition: core.h:1240
HAL_Handle HAL_SimDeviceHandle
Definition: Types.h:53
HAL_Handle HAL_SimValueHandle
Definition: Types.h:55
Definition: AnalogOutputSim.h:15
static constexpr const unit_t< compound_unit< charge::coulomb, inverse< substance::mol > > > F(N_A *e)
Faraday constant.
HAL Entry Value.
Definition: Value.h:20