WPILibC++ 2024.3.2
SendableBuilderImpl.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 <memory>
9#include <span>
10#include <string>
11#include <string_view>
12#include <utility>
13#include <vector>
14
19#include <wpi/FunctionExtras.h>
20#include <wpi/SmallVector.h>
21
22namespace frc {
23
24/**
25 * Implementation detail for SendableBuilder.
26 */
28 public:
30 ~SendableBuilderImpl() override = default;
31
34
35 /**
36 * Set the network table. Must be called prior to any Add* functions being
37 * called.
38 * @param table Network table
39 */
40 void SetTable(std::shared_ptr<nt::NetworkTable> table);
41
42 /**
43 * Get the network table.
44 * @return The network table
45 */
46 std::shared_ptr<nt::NetworkTable> GetTable() override;
47
48 /**
49 * Return whether this sendable has an associated table.
50 * @return True if it has a table, false if not.
51 */
52 bool IsPublished() const override;
53
54 /**
55 * Return whether this sendable should be treated as an actuator.
56 * @return True if actuator, false if not.
57 */
58 bool IsActuator() const;
59
60 /**
61 * Synchronize with network table values by calling the getters for all
62 * properties and setters when the network table value has changed.
63 */
64 void Update() override;
65
66 /**
67 * Hook setters for all properties.
68 */
70
71 /**
72 * Unhook setters for all properties.
73 */
75
76 /**
77 * Start LiveWindow mode by hooking the setters for all properties. Also
78 * calls the SafeState function if one was provided.
79 */
81
82 /**
83 * Stop LiveWindow mode by unhooking the setters for all properties. Also
84 * calls the SafeState function if one was provided.
85 */
87
88 /**
89 * Clear properties.
90 */
91 void ClearProperties() override;
92
94 void SetActuator(bool value) override;
95 void SetSafeState(std::function<void()> func) override;
96 void SetUpdateTable(wpi::unique_function<void()> func) override;
98
99 void AddBooleanProperty(std::string_view key, std::function<bool()> getter,
100 std::function<void(bool)> setter) override;
101
102 void PublishConstBoolean(std::string_view key, bool value) override;
103
104 void AddIntegerProperty(std::string_view key, std::function<int64_t()> getter,
105 std::function<void(int64_t)> setter) override;
106
107 void PublishConstInteger(std::string_view key, int64_t value) override;
108
109 void AddFloatProperty(std::string_view key, std::function<float()> getter,
110 std::function<void(float)> setter) override;
111
112 void PublishConstFloat(std::string_view key, float value) override;
113
114 void AddDoubleProperty(std::string_view key, std::function<double()> getter,
115 std::function<void(double)> setter) override;
116
117 void PublishConstDouble(std::string_view key, double value) override;
118
120 std::function<std::string()> getter,
121 std::function<void(std::string_view)> setter) override;
122
124 std::string_view value) override;
125
127 std::string_view key, std::function<std::vector<int>()> getter,
128 std::function<void(std::span<const int>)> setter) override;
129
131 std::span<const int> value) override;
132
134 std::string_view key, std::function<std::vector<int64_t>()> getter,
135 std::function<void(std::span<const int64_t>)> setter) override;
136
138 std::span<const int64_t> value) override;
139
141 std::string_view key, std::function<std::vector<float>()> getter,
142 std::function<void(std::span<const float>)> setter) override;
143
145 std::span<const float> value) override;
146
148 std::string_view key, std::function<std::vector<double>()> getter,
149 std::function<void(std::span<const double>)> setter) override;
150
152 std::span<const double> value) override;
153
155 std::string_view key, std::function<std::vector<std::string>()> getter,
156 std::function<void(std::span<const std::string>)> setter) override;
157
159 std::span<const std::string> value) override;
160
162 std::string_view key, std::string_view typeString,
163 std::function<std::vector<uint8_t>()> getter,
164 std::function<void(std::span<const uint8_t>)> setter) override;
165
167 std::span<const uint8_t> value) override;
168
171 std::function<std::string_view(wpi::SmallVectorImpl<char>& buf)> getter,
172 std::function<void(std::string_view)> setter) override;
173
176 std::function<std::span<const int>(wpi::SmallVectorImpl<int>& buf)>
177 getter,
178 std::function<void(std::span<const int>)> setter) override;
179
182 std::function<
183 std::span<const int64_t>(wpi::SmallVectorImpl<int64_t>& buf)>
184 getter,
185 std::function<void(std::span<const int64_t>)> setter) override;
186
189 std::function<std::span<const float>(wpi::SmallVectorImpl<float>& buf)>
190 getter,
191 std::function<void(std::span<const float>)> setter) override;
192
195 std::function<std::span<const double>(wpi::SmallVectorImpl<double>& buf)>
196 getter,
197 std::function<void(std::span<const double>)> setter) override;
198
201 std::function<
202 std::span<const std::string>(wpi::SmallVectorImpl<std::string>& buf)>
203 getter,
204 std::function<void(std::span<const std::string>)> setter) override;
205
207 std::string_view key, std::string_view typeString,
208 std::function<std::span<uint8_t>(wpi::SmallVectorImpl<uint8_t>& buf)>
209 getter,
210 std::function<void(std::span<const uint8_t>)> setter) override;
211
212 private:
213 struct Property {
214 virtual ~Property() = default;
215 virtual void Update(bool controllable, int64_t time) = 0;
216 };
217
218 template <typename Topic>
219 struct PropertyImpl : public Property {
220 void Update(bool controllable, int64_t time) override;
221
222 using Publisher = typename Topic::PublisherType;
223 using Subscriber = typename Topic::SubscriberType;
224 Publisher pub;
225 Subscriber sub;
226 std::function<void(Publisher& pub, int64_t time)> updateNetwork;
227 std::function<void(Subscriber& sub)> updateLocal;
228 };
229
230 template <typename Topic, typename Getter, typename Setter>
231 void AddPropertyImpl(Topic topic, Getter getter, Setter setter);
232
233 template <typename Topic, typename Value>
234 void PublishConstImpl(Topic topic, Value value);
235
236 template <typename T, size_t Size, typename Topic, typename Getter,
237 typename Setter>
238 void AddSmallPropertyImpl(Topic topic, Getter getter, Setter setter);
239
240 std::vector<std::unique_ptr<Property>> m_properties;
241 std::function<void()> m_safeState;
242 std::vector<wpi::unique_function<void()>> m_updateTables;
243 std::shared_ptr<nt::NetworkTable> m_table;
244 bool m_controllable = false;
245 bool m_actuator = false;
246
247 nt::BooleanPublisher m_controllablePublisher;
248 nt::StringPublisher m_typePublisher;
249 nt::BooleanPublisher m_actuatorPublisher;
250};
251
252} // namespace frc
This file provides a collection of function (or more generally, callable) type erasure utilities supp...
This file defines the SmallVector class.
Implementation detail for SendableBuilder.
Definition: SendableBuilderImpl.h:27
void AddSmallRawProperty(std::string_view key, std::string_view typeString, std::function< std::span< uint8_t >(wpi::SmallVectorImpl< uint8_t > &buf)> getter, std::function< void(std::span< const uint8_t >)> setter) override
void AddDoubleProperty(std::string_view key, std::function< double()> getter, std::function< void(double)> setter) override
Add a double property.
void PublishConstInteger(std::string_view key, int64_t value) override
Add a constant integer property.
void AddIntegerProperty(std::string_view key, std::function< int64_t()> getter, std::function< void(int64_t)> setter) override
Add an integer property.
void SetSmartDashboardType(std::string_view type) override
Set the string representation of the named data type that will be used by the smart dashboard for thi...
void SetSafeState(std::function< void()> func) override
Set the function that should be called to set the Sendable into a safe state.
void AddDoubleArrayProperty(std::string_view key, std::function< std::vector< double >()> getter, std::function< void(std::span< const double >)> setter) override
Add a double array property.
void AddSmallFloatArrayProperty(std::string_view key, std::function< std::span< const float >(wpi::SmallVectorImpl< float > &buf)> getter, std::function< void(std::span< const float >)> setter) override
bool IsActuator() const
Return whether this sendable should be treated as an actuator.
void StartListeners()
Hook setters for all properties.
void AddSmallDoubleArrayProperty(std::string_view key, std::function< std::span< const double >(wpi::SmallVectorImpl< double > &buf)> getter, std::function< void(std::span< const double >)> setter) override
void AddBooleanProperty(std::string_view key, std::function< bool()> getter, std::function< void(bool)> setter) override
Add a boolean property.
void AddRawProperty(std::string_view key, std::string_view typeString, std::function< std::vector< uint8_t >()> getter, std::function< void(std::span< const uint8_t >)> setter) override
Add a raw property.
void AddStringProperty(std::string_view key, std::function< std::string()> getter, std::function< void(std::string_view)> setter) override
Add a string property.
void PublishConstDouble(std::string_view key, double value) override
Add a constant double property.
void StopLiveWindowMode()
Stop LiveWindow mode by unhooking the setters for all properties.
void AddSmallStringArrayProperty(std::string_view key, std::function< std::span< const std::string >(wpi::SmallVectorImpl< std::string > &buf)> getter, std::function< void(std::span< const std::string >)> setter) override
void PublishConstString(std::string_view key, std::string_view value) override
Add a constant string property.
void AddSmallStringProperty(std::string_view key, std::function< std::string_view(wpi::SmallVectorImpl< char > &buf)> getter, std::function< void(std::string_view)> setter) override
nt::Topic GetTopic(std::string_view key) override
Add a property without getters or setters.
void SetTable(std::shared_ptr< nt::NetworkTable > table)
Set the network table.
std::shared_ptr< nt::NetworkTable > GetTable() override
Get the network table.
SendableBuilderImpl & operator=(SendableBuilderImpl &&)=default
void AddSmallBooleanArrayProperty(std::string_view key, std::function< std::span< const int >(wpi::SmallVectorImpl< int > &buf)> getter, std::function< void(std::span< const int >)> setter) override
void PublishConstDoubleArray(std::string_view key, std::span< const double > value) override
Add a constant double array property.
void SetActuator(bool value) override
Set a flag indicating if this sendable should be treated as an actuator.
void AddSmallIntegerArrayProperty(std::string_view key, std::function< std::span< const int64_t >(wpi::SmallVectorImpl< int64_t > &buf)> getter, std::function< void(std::span< const int64_t >)> setter) override
void PublishConstBooleanArray(std::string_view key, std::span< const int > value) override
Add a constant boolean array property.
~SendableBuilderImpl() override=default
void AddBooleanArrayProperty(std::string_view key, std::function< std::vector< int >()> getter, std::function< void(std::span< const int >)> setter) override
Add a boolean array property.
void Update() override
Synchronize with network table values by calling the getters for all properties and setters when the ...
void PublishConstStringArray(std::string_view key, std::span< const std::string > value) override
Add a constant string array property.
void AddStringArrayProperty(std::string_view key, std::function< std::vector< std::string >()> getter, std::function< void(std::span< const std::string >)> setter) override
Add a string array property.
void PublishConstFloat(std::string_view key, float value) override
Add a constant float property.
void StartLiveWindowMode()
Start LiveWindow mode by hooking the setters for all properties.
void AddFloatProperty(std::string_view key, std::function< float()> getter, std::function< void(float)> setter) override
Add a float property.
SendableBuilderImpl(SendableBuilderImpl &&)=default
void AddIntegerArrayProperty(std::string_view key, std::function< std::vector< int64_t >()> getter, std::function< void(std::span< const int64_t >)> setter) override
Add an integer array property.
bool IsPublished() const override
Return whether this sendable has an associated table.
void PublishConstBoolean(std::string_view key, bool value) override
Add a constant boolean property.
void PublishConstFloatArray(std::string_view key, std::span< const float > value) override
Add a constant float array property.
void PublishConstRaw(std::string_view key, std::string_view typeString, std::span< const uint8_t > value) override
Add a constant raw property.
void SetUpdateTable(wpi::unique_function< void()> func) override
Set the function that should be called to update the network table for things other than properties.
void AddFloatArrayProperty(std::string_view key, std::function< std::vector< float >()> getter, std::function< void(std::span< const float >)> setter) override
Add a float array property.
void StopListeners()
Unhook setters for all properties.
void ClearProperties() override
Clear properties.
void PublishConstIntegerArray(std::string_view key, std::span< const int64_t > value) override
Add a constant integer array property.
NetworkTables Boolean publisher.
Definition: BooleanTopic.h:113
Helper class for building Sendable dashboard representations for NetworkTables.
Definition: NTSendableBuilder.h:22
NetworkTables String publisher.
Definition: StringTopic.h:162
NetworkTables Topic.
Definition: Topic.h:28
unique_function is a type-erasing functor similar to std::function.
Definition: FunctionExtras.h:57
basic_string_view< char > string_view
Definition: core.h:501
type
Definition: core.h:556
Definition: AprilTagPoseEstimator.h:15