WPILibC++ 2024.1.1-beta-4
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
25 public:
27 ~SendableBuilderImpl() override = default;
28
31
32 /**
33 * Set the network table. Must be called prior to any Add* functions being
34 * called.
35 * @param table Network table
36 */
37 void SetTable(std::shared_ptr<nt::NetworkTable> table);
38
39 /**
40 * Get the network table.
41 * @return The network table
42 */
43 std::shared_ptr<nt::NetworkTable> GetTable() override;
44
45 /**
46 * Return whether this sendable has an associated table.
47 * @return True if it has a table, false if not.
48 */
49 bool IsPublished() const override;
50
51 /**
52 * Return whether this sendable should be treated as an actuator.
53 * @return True if actuator, false if not.
54 */
55 bool IsActuator() const;
56
57 /**
58 * Synchronize with network table values by calling the getters for all
59 * properties and setters when the network table value has changed.
60 */
61 void Update() override;
62
63 /**
64 * Hook setters for all properties.
65 */
67
68 /**
69 * Unhook setters for all properties.
70 */
72
73 /**
74 * Start LiveWindow mode by hooking the setters for all properties. Also
75 * calls the SafeState function if one was provided.
76 */
78
79 /**
80 * Stop LiveWindow mode by unhooking the setters for all properties. Also
81 * calls the SafeState function if one was provided.
82 */
84
85 /**
86 * Clear properties.
87 */
88 void ClearProperties() override;
89
91 void SetActuator(bool value) override;
92 void SetSafeState(std::function<void()> func) override;
93 void SetUpdateTable(wpi::unique_function<void()> func) override;
95
96 void AddBooleanProperty(std::string_view key, std::function<bool()> getter,
97 std::function<void(bool)> setter) override;
98
99 void PublishConstBoolean(std::string_view key, bool value) override;
100
101 void AddIntegerProperty(std::string_view key, std::function<int64_t()> getter,
102 std::function<void(int64_t)> setter) override;
103
104 void PublishConstInteger(std::string_view key, int64_t value) override;
105
106 void AddFloatProperty(std::string_view key, std::function<float()> getter,
107 std::function<void(float)> setter) override;
108
109 void PublishConstFloat(std::string_view key, float value) override;
110
111 void AddDoubleProperty(std::string_view key, std::function<double()> getter,
112 std::function<void(double)> setter) override;
113
114 void PublishConstDouble(std::string_view key, double value) override;
115
117 std::function<std::string()> getter,
118 std::function<void(std::string_view)> setter) override;
119
121 std::string_view value) override;
122
124 std::string_view key, std::function<std::vector<int>()> getter,
125 std::function<void(std::span<const int>)> setter) override;
126
128 std::span<const int> value) override;
129
131 std::string_view key, std::function<std::vector<int64_t>()> getter,
132 std::function<void(std::span<const int64_t>)> setter) override;
133
135 std::span<const int64_t> value) override;
136
138 std::string_view key, std::function<std::vector<float>()> getter,
139 std::function<void(std::span<const float>)> setter) override;
140
142 std::span<const float> value) override;
143
145 std::string_view key, std::function<std::vector<double>()> getter,
146 std::function<void(std::span<const double>)> setter) override;
147
149 std::span<const double> value) override;
150
152 std::string_view key, std::function<std::vector<std::string>()> getter,
153 std::function<void(std::span<const std::string>)> setter) override;
154
156 std::span<const std::string> value) override;
157
159 std::string_view key, std::string_view typeString,
160 std::function<std::vector<uint8_t>()> getter,
161 std::function<void(std::span<const uint8_t>)> setter) override;
162
164 std::span<const uint8_t> value) override;
165
168 std::function<std::string_view(wpi::SmallVectorImpl<char>& buf)> getter,
169 std::function<void(std::string_view)> setter) override;
170
173 std::function<std::span<const int>(wpi::SmallVectorImpl<int>& buf)>
174 getter,
175 std::function<void(std::span<const int>)> setter) override;
176
179 std::function<
180 std::span<const int64_t>(wpi::SmallVectorImpl<int64_t>& buf)>
181 getter,
182 std::function<void(std::span<const int64_t>)> setter) override;
183
186 std::function<std::span<const float>(wpi::SmallVectorImpl<float>& buf)>
187 getter,
188 std::function<void(std::span<const float>)> setter) override;
189
192 std::function<std::span<const double>(wpi::SmallVectorImpl<double>& buf)>
193 getter,
194 std::function<void(std::span<const double>)> setter) override;
195
198 std::function<
199 std::span<const std::string>(wpi::SmallVectorImpl<std::string>& buf)>
200 getter,
201 std::function<void(std::span<const std::string>)> setter) override;
202
204 std::string_view key, std::string_view typeString,
205 std::function<std::span<uint8_t>(wpi::SmallVectorImpl<uint8_t>& buf)>
206 getter,
207 std::function<void(std::span<const uint8_t>)> setter) override;
208
209 private:
210 struct Property {
211 virtual ~Property() = default;
212 virtual void Update(bool controllable, int64_t time) = 0;
213 };
214
215 template <typename Topic>
216 struct PropertyImpl : public Property {
217 void Update(bool controllable, int64_t time) override;
218
219 using Publisher = typename Topic::PublisherType;
220 using Subscriber = typename Topic::SubscriberType;
221 Publisher pub;
222 Subscriber sub;
223 std::function<void(Publisher& pub, int64_t time)> updateNetwork;
224 std::function<void(Subscriber& sub)> updateLocal;
225 };
226
227 template <typename Topic, typename Getter, typename Setter>
228 void AddPropertyImpl(Topic topic, Getter getter, Setter setter);
229
230 template <typename Topic, typename Value>
231 void PublishConstImpl(Topic topic, Value value);
232
233 template <typename T, size_t Size, typename Topic, typename Getter,
234 typename Setter>
235 void AddSmallPropertyImpl(Topic topic, Getter getter, Setter setter);
236
237 std::vector<std::unique_ptr<Property>> m_properties;
238 std::function<void()> m_safeState;
239 std::vector<wpi::unique_function<void()>> m_updateTables;
240 std::shared_ptr<nt::NetworkTable> m_table;
241 bool m_controllable = false;
242 bool m_actuator = false;
243
244 nt::BooleanPublisher m_controllablePublisher;
245 nt::StringPublisher m_typePublisher;
246 nt::BooleanPublisher m_actuatorPublisher;
247};
248
249} // namespace frc
This file provides a collection of function (or more generally, callable) type erasure utilities supp...
This file defines the SmallVector class.
Definition: SendableBuilderImpl.h:24
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
Definition: NTSendableBuilder.h:18
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