WPILibC++ 2023.4.3
FloatArrayTopic.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#include <utility>
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 FloatArrayTopic;
25
26/**
27 * NetworkTables FloatArray subscriber.
28 */
30 public:
32 using ValueType = std::vector<float>;
33 using ParamType = std::span<const float>;
35
36 using SmallRetType = std::span<float>;
37 using SmallElemType = float;
39
40
42
43 /**
44 * Construct from a subscriber handle; recommended to use
45 * FloatArrayTopic::Subscribe() instead.
46 *
47 * @param handle Native handle
48 * @param defaultValue Default value
49 */
51
52 /**
53 * Get the last published value.
54 * If no value has been published, returns the stored default value.
55 *
56 * @return value
57 */
58 ValueType Get() const;
59
60 /**
61 * Get the last published value.
62 * If no value has been published, returns the passed defaultValue.
63 *
64 * @param defaultValue default value to return if no value has been published
65 * @return value
66 */
67 ValueType Get(ParamType defaultValue) const;
68
69 /**
70 * Get the last published value.
71 * If no value has been published, returns the stored default value.
72 *
73 * @param buf storage for returned value
74 * @return value
75 */
77
78 /**
79 * Get the last published value.
80 * If no value has been published, returns the passed defaultValue.
81 *
82 * @param buf storage for returned value
83 * @param defaultValue default value to return if no value has been published
84 * @return value
85 */
87
88 /**
89 * Get the last published value along with its timestamp
90 * If no value has been published, returns the stored default value and a
91 * timestamp of 0.
92 *
93 * @return timestamped value
94 */
96
97 /**
98 * Get the last published value along with its timestamp.
99 * If no value has been published, returns the passed defaultValue and a
100 * timestamp of 0.
101 *
102 * @param defaultValue default value to return if no value has been published
103 * @return timestamped value
104 */
106
107 /**
108 * Get the last published value along with its timestamp.
109 * If no value has been published, returns the stored default value and a
110 * timestamp of 0.
111 *
112 * @param buf storage for returned value
113 * @return timestamped value
114 */
117
118 /**
119 * Get the last published value along with its timestamp.
120 * If no value has been published, returns the passed defaultValue and a
121 * timestamp of 0.
122 *
123 * @param buf storage for returned value
124 * @param defaultValue default value to return if no value has been published
125 * @return timestamped value
126 */
129 ParamType defaultValue) const;
130
131 /**
132 * Get an array of all value changes since the last call to ReadQueue.
133 * Also provides a timestamp for each value.
134 *
135 * @note The "poll storage" subscribe option can be used to set the queue
136 * depth.
137 *
138 * @return Array of timestamped values; empty array if no new changes have
139 * been published since the previous call.
140 */
141 std::vector<TimestampedValueType> ReadQueue();
142
143 /**
144 * Get the corresponding topic.
145 *
146 * @return Topic
147 */
148 TopicType GetTopic() const;
149
150 private:
151 ValueType m_defaultValue;
152};
153
154/**
155 * NetworkTables FloatArray publisher.
156 */
158 public:
160 using ValueType = std::vector<float>;
161 using ParamType = std::span<const float>;
162
163 using SmallRetType = std::span<float>;
164 using SmallElemType = float;
165
167
169
170 /**
171 * Construct from a publisher handle; recommended to use
172 * FloatArrayTopic::Publish() instead.
173 *
174 * @param handle Native handle
175 */
176 explicit FloatArrayPublisher(NT_Publisher handle);
177
178 /**
179 * Publish a new value.
180 *
181 * @param value value to publish
182 * @param time timestamp; 0 indicates current NT time should be used
183 */
184 void Set(ParamType value, int64_t time = 0);
185
186 /**
187 * Publish a default value.
188 * On reconnect, a default value will never be used in preference to a
189 * published value.
190 *
191 * @param value value
192 */
194
195 /**
196 * Get the corresponding topic.
197 *
198 * @return Topic
199 */
200 TopicType GetTopic() const;
201};
202
203/**
204 * NetworkTables FloatArray entry.
205 *
206 * @note Unlike NetworkTableEntry, the entry goes away when this is destroyed.
207 */
209 public FloatArrayPublisher {
210 public:
214 using ValueType = std::vector<float>;
215 using ParamType = std::span<const float>;
216
217 using SmallRetType = std::span<float>;
218 using SmallElemType = float;
219
221
222 FloatArrayEntry() = default;
223
224 /**
225 * Construct from an entry handle; recommended to use
226 * FloatArrayTopic::GetEntry() instead.
227 *
228 * @param handle Native handle
229 * @param defaultValue Default value
230 */
231 FloatArrayEntry(NT_Entry handle, ParamType defaultValue);
232
233 /**
234 * Determines if the native handle is valid.
235 *
236 * @return True if the native handle is valid, false otherwise.
237 */
238 explicit operator bool() const { return m_subHandle != 0; }
239
240 /**
241 * Gets the native handle for the entry.
242 *
243 * @return Native handle
244 */
245 NT_Entry GetHandle() const { return m_subHandle; }
246
247 /**
248 * Get the corresponding topic.
249 *
250 * @return Topic
251 */
252 TopicType GetTopic() const;
253
254 /**
255 * Stops publishing the entry if it's published.
256 */
257 void Unpublish();
258};
259
260/**
261 * NetworkTables FloatArray topic.
262 */
263class FloatArrayTopic final : public Topic {
264 public:
268 using ValueType = std::vector<float>;
269 using ParamType = std::span<const float>;
271 /** The default type string for this topic type. */
272 static constexpr std::string_view kTypeString = "float[]";
273
274 FloatArrayTopic() = default;
275
276 /**
277 * Construct from a topic handle; recommended to use
278 * NetworkTableInstance::GetFloatArrayTopic() instead.
279 *
280 * @param handle Native handle
281 */
282 explicit FloatArrayTopic(NT_Topic handle) : Topic{handle} {}
283
284 /**
285 * Construct from a generic topic.
286 *
287 * @param topic Topic
288 */
289 explicit FloatArrayTopic(Topic topic) : Topic{topic} {}
290
291 /**
292 * Create a new subscriber to the topic.
293 *
294 * <p>The subscriber is only active as long as the returned object
295 * is not destroyed.
296 *
297 * @note Subscribers that do not match the published data type do not return
298 * any values. To determine if the data type matches, use the appropriate
299 * Topic functions.
300 *
301 * @param defaultValue default value used when a default is not provided to a
302 * getter function
303 * @param options subscribe options
304 * @return subscriber
305 */
306 [[nodiscard]]
308 ParamType defaultValue,
309 const PubSubOptions& options = kDefaultPubSubOptions);
310 /**
311 * Create a new subscriber to the topic, with specific type string.
312 *
313 * <p>The subscriber is only active as long as the returned object
314 * is not destroyed.
315 *
316 * @note Subscribers that do not match the published data type do not return
317 * any values. To determine if the data type matches, use the appropriate
318 * Topic functions.
319 *
320 * @param typeString type string
321 * @param defaultValue default value used when a default is not provided to a
322 * getter function
323 * @param options subscribe options
324 * @return subscriber
325 */
326 [[nodiscard]]
328 std::string_view typeString, ParamType defaultValue,
329 const PubSubOptions& options = kDefaultPubSubOptions);
330
331 /**
332 * Create a new publisher to the topic.
333 *
334 * The publisher is only active as long as the returned object
335 * is not destroyed.
336 *
337 * @note It is not possible to publish two different data types to the same
338 * topic. Conflicts between publishers are typically resolved by the
339 * server on a first-come, first-served basis. Any published values that
340 * do not match the topic's data type are dropped (ignored). To determine
341 * if the data type matches, use the appropriate Topic functions.
342 *
343 * @param options publish options
344 * @return publisher
345 */
346 [[nodiscard]]
348
349 /**
350 * Create a new publisher to the topic, with type string and initial
351 * properties.
352 *
353 * The publisher is only active as long as the returned object
354 * is not destroyed.
355 *
356 * @note It is not possible to publish two different data types to the same
357 * topic. Conflicts between publishers are typically resolved by the
358 * server on a first-come, first-served basis. Any published values that
359 * do not match the topic's data type are dropped (ignored). To determine
360 * if the data type matches, use the appropriate Topic functions.
361 *
362 * @param typeString type string
363 * @param properties JSON properties
364 * @param options publish options
365 * @return publisher
366 */
367 [[nodiscard]]
369 const wpi::json& properties, const PubSubOptions& options = kDefaultPubSubOptions);
370
371 /**
372 * Create a new entry for the topic.
373 *
374 * Entries act as a combination of a subscriber and a weak publisher. The
375 * subscriber is active as long as the entry is not destroyed. The publisher
376 * is created when the entry is first written to, and remains active until
377 * either Unpublish() is called or the entry is destroyed.
378 *
379 * @note It is not possible to use two different data types with the same
380 * topic. Conflicts between publishers are typically resolved by the
381 * server on a first-come, first-served basis. Any published values that
382 * do not match the topic's data type are dropped (ignored), and the entry
383 * will show no new values if the data type does not match. To determine
384 * if the data type matches, use the appropriate Topic functions.
385 *
386 * @param defaultValue default value used when a default is not provided to a
387 * getter function
388 * @param options publish and/or subscribe options
389 * @return entry
390 */
391 [[nodiscard]]
392 EntryType GetEntry(ParamType defaultValue,
393 const PubSubOptions& options = kDefaultPubSubOptions);
394 /**
395 * Create a new entry for the topic, with specific type string.
396 *
397 * Entries act as a combination of a subscriber and a weak publisher. The
398 * subscriber is active as long as the entry is not destroyed. The publisher
399 * is created when the entry is first written to, and remains active until
400 * either Unpublish() is called or the entry is destroyed.
401 *
402 * @note It is not possible to use two different data types with the same
403 * topic. Conflicts between publishers are typically resolved by the
404 * server on a first-come, first-served basis. Any published values that
405 * do not match the topic's data type are dropped (ignored), and the entry
406 * will show no new values if the data type does not match. To determine
407 * if the data type matches, use the appropriate Topic functions.
408 *
409 * @param typeString type string
410 * @param defaultValue default value used when a default is not provided to a
411 * getter function
412 * @param options publish and/or subscribe options
413 * @return entry
414 */
415 [[nodiscard]]
416 EntryType GetEntryEx(std::string_view typeString, ParamType defaultValue,
417 const PubSubOptions& options = kDefaultPubSubOptions);
418
419};
420
421} // namespace nt
422
NetworkTables FloatArray entry.
Definition: FloatArrayTopic.h:209
FloatArrayEntry(NT_Entry handle, ParamType defaultValue)
Construct from an entry handle; recommended to use FloatArrayTopic::GetEntry() instead.
FloatArrayTopic TopicType
Definition: FloatArrayTopic.h:213
void Unpublish()
Stops publishing the entry if it's published.
Definition: FloatArrayTopic.inc:86
FloatArrayEntry()=default
TopicType GetTopic() const
Get the corresponding topic.
Definition: FloatArrayTopic.inc:82
NT_Entry GetHandle() const
Gets the native handle for the entry.
Definition: FloatArrayTopic.h:245
NetworkTables FloatArray publisher.
Definition: FloatArrayTopic.h:157
void Set(ParamType value, int64_t time=0)
Publish a new value.
Definition: FloatArrayTopic.inc:64
void SetDefault(ParamType value)
Publish a default value.
Definition: FloatArrayTopic.inc:69
TopicType GetTopic() const
Get the corresponding topic.
Definition: FloatArrayTopic.inc:73
std::vector< float > ValueType
Definition: FloatArrayTopic.h:160
float SmallElemType
Definition: FloatArrayTopic.h:164
std::span< float > SmallRetType
Definition: FloatArrayTopic.h:163
std::span< const float > ParamType
Definition: FloatArrayTopic.h:161
NetworkTables FloatArray subscriber.
Definition: FloatArrayTopic.h:29
std::span< const float > ParamType
Definition: FloatArrayTopic.h:33
std::span< float > SmallRetType
Definition: FloatArrayTopic.h:36
std::vector< float > ValueType
Definition: FloatArrayTopic.h:32
FloatArraySubscriber(NT_Subscriber handle, ParamType defaultValue)
Construct from a subscriber handle; recommended to use FloatArrayTopic::Subscribe() instead.
TopicType GetTopic() const
Get the corresponding topic.
Definition: FloatArrayTopic.inc:57
ValueType Get(ParamType defaultValue) const
Get the last published value.
TimestampedValueType GetAtomic() const
Get the last published value along with its timestamp If no value has been published,...
Definition: FloatArrayTopic.inc:35
SmallRetType Get(wpi::SmallVectorImpl< SmallElemType > &buf, ParamType defaultValue) const
Get the last published value.
float SmallElemType
Definition: FloatArrayTopic.h:37
TimestampedValueViewType GetAtomic(wpi::SmallVectorImpl< SmallElemType > &buf, ParamType defaultValue) const
Get the last published value along with its timestamp.
ValueType Get() const
Get the last published value.
Definition: FloatArrayTopic.inc:18
std::vector< TimestampedValueType > ReadQueue()
Get an array of all value changes since the last call to ReadQueue.
Definition: FloatArrayTopic.inc:53
TimestampedValueType GetAtomic(ParamType defaultValue) const
Get the last published value along with its timestamp.
NetworkTables FloatArray topic.
Definition: FloatArrayTopic.h:263
FloatArrayTopic(Topic topic)
Construct from a generic topic.
Definition: FloatArrayTopic.h:289
PublisherType Publish(const PubSubOptions &options=kDefaultPubSubOptions)
Create a new publisher to the topic.
Definition: FloatArrayTopic.inc:105
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: FloatArrayTopic.inc:111
EntryType GetEntry(ParamType defaultValue, const PubSubOptions &options=kDefaultPubSubOptions)
Create a new entry for the topic.
Definition: FloatArrayTopic.inc:118
EntryType GetEntryEx(std::string_view typeString, ParamType defaultValue, const PubSubOptions &options=kDefaultPubSubOptions)
Create a new entry for the topic, with specific type string.
Definition: FloatArrayTopic.inc:125
FloatArrayEntry EntryType
Definition: FloatArrayTopic.h:267
FloatArrayPublisher PublisherType
Definition: FloatArrayTopic.h:266
FloatArraySubscriber SubscriberType
Definition: FloatArrayTopic.h:265
FloatArrayTopic()=default
std::span< const float > ParamType
Definition: FloatArrayTopic.h:269
static constexpr std::string_view kTypeString
The default type string for this topic type.
Definition: FloatArrayTopic.h:272
SubscriberType SubscribeEx(std::string_view typeString, ParamType defaultValue, const PubSubOptions &options=kDefaultPubSubOptions)
Create a new subscriber to the topic, with specific type string.
Definition: FloatArrayTopic.inc:97
SubscriberType Subscribe(ParamType defaultValue, const PubSubOptions &options=kDefaultPubSubOptions)
Create a new subscriber to the topic.
Definition: FloatArrayTopic.inc:90
std::vector< float > ValueType
Definition: FloatArrayTopic.h:268
FloatArrayTopic(NT_Topic handle)
Construct from a topic handle; recommended to use NetworkTableInstance::GetFloatArrayTopic() instead.
Definition: FloatArrayTopic.h:282
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
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
Definition: SmallVector.h:557
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 FloatArray.
Definition: ntcore_cpp_types.h:940
Timestamped FloatArray view (for SmallVector-taking functions).
Definition: ntcore_cpp_types.h:965