WPILibC++  2020.3.2
SimDeviceSim.h
1 /*----------------------------------------------------------------------------*/
2 /* Copyright (c) 2019 FIRST. All Rights Reserved. */
3 /* Open Source Software - may be modified and shared by FRC teams. The code */
4 /* must be accompanied by the FIRST BSD license file in the root directory of */
5 /* the project. */
6 /*----------------------------------------------------------------------------*/
7 
8 #pragma once
9 
10 #include <functional>
11 #include <memory>
12 #include <string>
13 #include <utility>
14 #include <vector>
15 
16 #include "CallbackStore.h"
17 #include "hal/SimDevice.h"
18 #include "mockdata/SimDeviceData.h"
19 
20 namespace frc {
21 namespace sim {
22 class SimDeviceSim {
23  public:
24  explicit SimDeviceSim(const char* name)
25  : m_handle{HALSIM_GetSimDeviceHandle(name)} {}
26 
27  hal::SimValue GetValue(const char* name) const {
28  return HALSIM_GetSimValueHandle(m_handle, name);
29  }
30 
31  hal::SimDouble GetDouble(const char* name) const {
32  return HALSIM_GetSimValueHandle(m_handle, name);
33  }
34 
35  hal::SimEnum GetEnum(const char* name) const {
36  return HALSIM_GetSimValueHandle(m_handle, name);
37  }
38 
39  hal::SimBoolean GetBoolean(const char* name) const {
40  return HALSIM_GetSimValueHandle(m_handle, name);
41  }
42 
43  static std::vector<std::string> GetEnumOptions(hal::SimEnum val) {
44  int32_t numOptions;
45  const char** options = HALSIM_GetSimValueEnumOptions(val, &numOptions);
46  std::vector<std::string> rv;
47  rv.reserve(numOptions);
48  for (int32_t i = 0; i < numOptions; ++i) rv.emplace_back(options[i]);
49  return rv;
50  }
51 
52  template <typename F>
53  void EnumerateValues(F callback) const {
54  return HALSIM_EnumerateSimValues(
55  m_handle, &callback,
56  [](const char* name, void* param, HAL_SimValueHandle handle,
57  HAL_Bool readonly, const struct HAL_Value* value) {
58  std::invoke(*static_cast<F*>(param), name, handle, readonly, value);
59  });
60  }
61 
62  operator HAL_SimDeviceHandle() const { return m_handle; }
63 
64  template <typename F>
65  static void EnumerateDevices(const char* prefix, F callback) {
66  return HALSIM_EnumerateSimDevices(
67  prefix, &callback,
68  [](const char* name, void* param, HAL_SimDeviceHandle handle) {
69  std::invoke(*static_cast<F*>(param), name, handle);
70  });
71  }
72 
73  static void ResetData() { HALSIM_ResetSimDeviceData(); }
74 
75  private:
76  HAL_SimDeviceHandle m_handle;
77 };
78 } // namespace sim
79 } // namespace frc
frc::sim::SimDeviceSim
Definition: SimDeviceSim.h:22
frc
A class that enforces constraints on the differential drive kinematics.
Definition: SPIAccelerometerSim.h:16
HAL_Value
HAL Entry Value.
Definition: Value.h:23