WPILibC++ 2023.4.3
FloatTopic.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#include <span>
11#include <string_view>
12#include <vector>
13
14#include "networktables/Topic.h"
15
16namespace wpi {
17template <typename T>
18class SmallVectorImpl;
19class json;
20} // namespace wpi
21
22namespace nt {
23
24class FloatTopic;
25
26/**
27 * NetworkTables Float subscriber.
28 */
30 public:
32 using ValueType = float;
33 using ParamType = float;
35
36
37 FloatSubscriber() = default;
38
39 /**
40 * Construct from a subscriber handle; recommended to use
41 * FloatTopic::Subscribe() instead.
42 *
43 * @param handle Native handle
44 * @param defaultValue Default value
45 */
46 FloatSubscriber(NT_Subscriber handle, ParamType defaultValue);
47
48 /**
49 * Get the last published value.
50 * If no value has been published, returns the stored default value.
51 *
52 * @return value
53 */
54 ValueType Get() const;
55
56 /**
57 * Get the last published value.
58 * If no value has been published, returns the passed defaultValue.
59 *
60 * @param defaultValue default value to return if no value has been published
61 * @return value
62 */
63 ValueType Get(ParamType defaultValue) const;
64
65 /**
66 * Get the last published value along with its timestamp
67 * If no value has been published, returns the stored default value and a
68 * timestamp of 0.
69 *
70 * @return timestamped value
71 */
73
74 /**
75 * Get the last published value along with its timestamp.
76 * If no value has been published, returns the passed defaultValue and a
77 * timestamp of 0.
78 *
79 * @param defaultValue default value to return if no value has been published
80 * @return timestamped value
81 */
82 TimestampedValueType GetAtomic(ParamType defaultValue) const;
83
84 /**
85 * Get an array of all value changes since the last call to ReadQueue.
86 * Also provides a timestamp for each value.
87 *
88 * @note The "poll storage" subscribe option can be used to set the queue
89 * depth.
90 *
91 * @return Array of timestamped values; empty array if no new changes have
92 * been published since the previous call.
93 */
94 std::vector<TimestampedValueType> ReadQueue();
95
96 /**
97 * Get the corresponding topic.
98 *
99 * @return Topic
100 */
101 TopicType GetTopic() const;
102
103 private:
104 ValueType m_defaultValue;
105};
106
107/**
108 * NetworkTables Float publisher.
109 */
110class FloatPublisher : public Publisher {
111 public:
113 using ValueType = float;
114 using ParamType = float;
115
117
118 FloatPublisher() = default;
119
120 /**
121 * Construct from a publisher handle; recommended to use
122 * FloatTopic::Publish() instead.
123 *
124 * @param handle Native handle
125 */
126 explicit FloatPublisher(NT_Publisher handle);
127
128 /**
129 * Publish a new value.
130 *
131 * @param value value to publish
132 * @param time timestamp; 0 indicates current NT time should be used
133 */
134 void Set(ParamType value, int64_t time = 0);
135
136 /**
137 * Publish a default value.
138 * On reconnect, a default value will never be used in preference to a
139 * published value.
140 *
141 * @param value value
142 */
144
145 /**
146 * Get the corresponding topic.
147 *
148 * @return Topic
149 */
150 TopicType GetTopic() const;
151};
152
153/**
154 * NetworkTables Float entry.
155 *
156 * @note Unlike NetworkTableEntry, the entry goes away when this is destroyed.
157 */
158class FloatEntry final : public FloatSubscriber,
159 public FloatPublisher {
160 public:
164 using ValueType = float;
165 using ParamType = float;
166
168
169 FloatEntry() = default;
170
171 /**
172 * Construct from an entry handle; recommended to use
173 * FloatTopic::GetEntry() instead.
174 *
175 * @param handle Native handle
176 * @param defaultValue Default value
177 */
178 FloatEntry(NT_Entry handle, ParamType defaultValue);
179
180 /**
181 * Determines if the native handle is valid.
182 *
183 * @return True if the native handle is valid, false otherwise.
184 */
185 explicit operator bool() const { return m_subHandle != 0; }
186
187 /**
188 * Gets the native handle for the entry.
189 *
190 * @return Native handle
191 */
192 NT_Entry GetHandle() const { return m_subHandle; }
193
194 /**
195 * Get the corresponding topic.
196 *
197 * @return Topic
198 */
199 TopicType GetTopic() const;
200
201 /**
202 * Stops publishing the entry if it's published.
203 */
204 void Unpublish();
205};
206
207/**
208 * NetworkTables Float topic.
209 */
210class FloatTopic final : public Topic {
211 public:
215 using ValueType = float;
216 using ParamType = float;
218 /** The default type string for this topic type. */
219 static constexpr std::string_view kTypeString = "float";
220
221 FloatTopic() = default;
222
223 /**
224 * Construct from a topic handle; recommended to use
225 * NetworkTableInstance::GetFloatTopic() instead.
226 *
227 * @param handle Native handle
228 */
229 explicit FloatTopic(NT_Topic handle) : Topic{handle} {}
230
231 /**
232 * Construct from a generic topic.
233 *
234 * @param topic Topic
235 */
236 explicit FloatTopic(Topic topic) : Topic{topic} {}
237
238 /**
239 * Create a new subscriber to the topic.
240 *
241 * <p>The subscriber is only active as long as the returned object
242 * is not destroyed.
243 *
244 * @note Subscribers that do not match the published data type do not return
245 * any values. To determine if the data type matches, use the appropriate
246 * Topic functions.
247 *
248 * @param defaultValue default value used when a default is not provided to a
249 * getter function
250 * @param options subscribe options
251 * @return subscriber
252 */
253 [[nodiscard]]
255 ParamType defaultValue,
256 const PubSubOptions& options = kDefaultPubSubOptions);
257 /**
258 * Create a new subscriber to the topic, with specific type string.
259 *
260 * <p>The subscriber is only active as long as the returned object
261 * is not destroyed.
262 *
263 * @note Subscribers that do not match the published data type do not return
264 * any values. To determine if the data type matches, use the appropriate
265 * Topic functions.
266 *
267 * @param typeString type string
268 * @param defaultValue default value used when a default is not provided to a
269 * getter function
270 * @param options subscribe options
271 * @return subscriber
272 */
273 [[nodiscard]]
275 std::string_view typeString, ParamType defaultValue,
276 const PubSubOptions& options = kDefaultPubSubOptions);
277
278 /**
279 * Create a new publisher to the topic.
280 *
281 * The publisher is only active as long as the returned object
282 * is not destroyed.
283 *
284 * @note It is not possible to publish two different data types to the same
285 * topic. Conflicts between publishers are typically resolved by the
286 * server on a first-come, first-served basis. Any published values that
287 * do not match the topic's data type are dropped (ignored). To determine
288 * if the data type matches, use the appropriate Topic functions.
289 *
290 * @param options publish options
291 * @return publisher
292 */
293 [[nodiscard]]
295
296 /**
297 * Create a new publisher to the topic, with type string and initial
298 * properties.
299 *
300 * The publisher is only active as long as the returned object
301 * is not destroyed.
302 *
303 * @note It is not possible to publish two different data types to the same
304 * topic. Conflicts between publishers are typically resolved by the
305 * server on a first-come, first-served basis. Any published values that
306 * do not match the topic's data type are dropped (ignored). To determine
307 * if the data type matches, use the appropriate Topic functions.
308 *
309 * @param typeString type string
310 * @param properties JSON properties
311 * @param options publish options
312 * @return publisher
313 */
314 [[nodiscard]]
316 const wpi::json& properties, const PubSubOptions& options = kDefaultPubSubOptions);
317
318 /**
319 * Create a new entry for the topic.
320 *
321 * Entries act as a combination of a subscriber and a weak publisher. The
322 * subscriber is active as long as the entry is not destroyed. The publisher
323 * is created when the entry is first written to, and remains active until
324 * either Unpublish() is called or the entry is destroyed.
325 *
326 * @note It is not possible to use two different data types with the same
327 * topic. Conflicts between publishers are typically resolved by the
328 * server on a first-come, first-served basis. Any published values that
329 * do not match the topic's data type are dropped (ignored), and the entry
330 * will show no new values if the data type does not match. To determine
331 * if the data type matches, use the appropriate Topic functions.
332 *
333 * @param defaultValue default value used when a default is not provided to a
334 * getter function
335 * @param options publish and/or subscribe options
336 * @return entry
337 */
338 [[nodiscard]]
339 EntryType GetEntry(ParamType defaultValue,
340 const PubSubOptions& options = kDefaultPubSubOptions);
341 /**
342 * Create a new entry for the topic, with specific type string.
343 *
344 * Entries act as a combination of a subscriber and a weak publisher. The
345 * subscriber is active as long as the entry is not destroyed. The publisher
346 * is created when the entry is first written to, and remains active until
347 * either Unpublish() is called or the entry is destroyed.
348 *
349 * @note It is not possible to use two different data types with the same
350 * topic. Conflicts between publishers are typically resolved by the
351 * server on a first-come, first-served basis. Any published values that
352 * do not match the topic's data type are dropped (ignored), and the entry
353 * will show no new values if the data type does not match. To determine
354 * if the data type matches, use the appropriate Topic functions.
355 *
356 * @param typeString type string
357 * @param defaultValue default value used when a default is not provided to a
358 * getter function
359 * @param options publish and/or subscribe options
360 * @return entry
361 */
362 [[nodiscard]]
363 EntryType GetEntryEx(std::string_view typeString, ParamType defaultValue,
364 const PubSubOptions& options = kDefaultPubSubOptions);
365
366};
367
368} // namespace nt
369
NetworkTables Float entry.
Definition: FloatTopic.h:159
NT_Entry GetHandle() const
Gets the native handle for the entry.
Definition: FloatTopic.h:192
void Unpublish()
Stops publishing the entry if it's published.
Definition: FloatTopic.inc:70
TopicType GetTopic() const
Get the corresponding topic.
Definition: FloatTopic.inc:66
FloatEntry()=default
FloatTopic TopicType
Definition: FloatTopic.h:163
NetworkTables Float publisher.
Definition: FloatTopic.h:110
float ParamType
Definition: FloatTopic.h:114
FloatPublisher()=default
TopicType GetTopic() const
Get the corresponding topic.
Definition: FloatTopic.inc:57
void Set(ParamType value, int64_t time=0)
Publish a new value.
Definition: FloatTopic.inc:48
void SetDefault(ParamType value)
Publish a default value.
Definition: FloatTopic.inc:53
float ValueType
Definition: FloatTopic.h:113
NetworkTables Float subscriber.
Definition: FloatTopic.h:29
std::vector< TimestampedValueType > ReadQueue()
Get an array of all value changes since the last call to ReadQueue.
Definition: FloatTopic.inc:37
float ValueType
Definition: FloatTopic.h:32
ValueType Get() const
Get the last published value.
Definition: FloatTopic.inc:18
TimestampedValueType GetAtomic() const
Get the last published value along with its timestamp If no value has been published,...
Definition: FloatTopic.inc:27
FloatSubscriber()=default
TopicType GetTopic() const
Get the corresponding topic.
Definition: FloatTopic.inc:41
float ParamType
Definition: FloatTopic.h:33
NetworkTables Float topic.
Definition: FloatTopic.h:210
FloatSubscriber SubscriberType
Definition: FloatTopic.h:212
float ParamType
Definition: FloatTopic.h:216
static constexpr std::string_view kTypeString
The default type string for this topic type.
Definition: FloatTopic.h:219
EntryType GetEntryEx(std::string_view typeString, ParamType defaultValue, const PubSubOptions &options=kDefaultPubSubOptions)
Create a new entry for the topic, with specific type string.
Definition: FloatTopic.inc:109
FloatEntry EntryType
Definition: FloatTopic.h:214
SubscriberType SubscribeEx(std::string_view typeString, ParamType defaultValue, const PubSubOptions &options=kDefaultPubSubOptions)
Create a new subscriber to the topic, with specific type string.
Definition: FloatTopic.inc:81
EntryType GetEntry(ParamType defaultValue, const PubSubOptions &options=kDefaultPubSubOptions)
Create a new entry for the topic.
Definition: FloatTopic.inc:102
SubscriberType Subscribe(ParamType defaultValue, const PubSubOptions &options=kDefaultPubSubOptions)
Create a new subscriber to the topic.
Definition: FloatTopic.inc:74
PublisherType PublishEx(std::string_view typeString, const wpi::json &properties, const PubSubOptions &options=kDefaultPubSubOptions)
Create a new publisher to the topic, with type string and initial properties.
Definition: FloatTopic.inc:95
PublisherType Publish(const PubSubOptions &options=kDefaultPubSubOptions)
Create a new publisher to the topic.
Definition: FloatTopic.inc:89
FloatTopic(Topic topic)
Construct from a generic topic.
Definition: FloatTopic.h:236
FloatPublisher PublisherType
Definition: FloatTopic.h:213
FloatTopic()=default
FloatTopic(NT_Topic handle)
Construct from a topic handle; recommended to use NetworkTableInstance::GetFloatTopic() instead.
Definition: FloatTopic.h:229
float ValueType
Definition: FloatTopic.h:215
NetworkTables publisher.
Definition: Topic.h:345
NetworkTables subscriber.
Definition: Topic.h:290
NT_Subscriber m_subHandle
Definition: Topic.h:341
NetworkTables Topic.
Definition: Topic.h:30
Definition: core.h:1240
a class to store JSON values
Definition: json.h:2655
basic_string_view< char > string_view
Definition: core.h:520
NT_Handle NT_Topic
Definition: ntcore_c.h:38
NT_Handle NT_Subscriber
Definition: ntcore_c.h:39
NT_Handle NT_Publisher
Definition: ntcore_c.h:40
NT_Handle NT_Entry
Definition: ntcore_c.h:33
constexpr PubSubOptions kDefaultPubSubOptions
Default publish/subscribe options.
Definition: ntcore_cpp.h:381
::int64_t int64_t
Definition: Meta.h:59
NetworkTables (ntcore) namespace.
Definition: ntcore_cpp.h:35
/file This file defines the SmallVector class.
Definition: AprilTagFieldLayout.h:18
NetworkTables publish/subscribe options.
Definition: ntcore_cpp.h:304
Timestamped Float.
Definition: ntcore_cpp_types.h:224