WPILibC++ 2025.2.1
Loading...
Searching...
No Matches
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// THIS FILE WAS AUTO-GENERATED BY ./ntcore/generate_topics.py. DO NOT MODIFY
6
7#pragma once
8
9#include <stdint.h>
10
11#include <utility>
12#include <span>
13#include <string_view>
14#include <vector>
15
16#include <wpi/json_fwd.h>
17
19#include "networktables/Topic.h"
20#include "ntcore_cpp.h"
21
22namespace wpi {
23template <typename T>
24class SmallVectorImpl;
25} // namespace wpi
26
27namespace nt {
28
29class FloatArrayTopic;
30
31/**
32 * NetworkTables FloatArray subscriber.
33 */
35 public:
37 using ValueType = std::vector<float>;
38 using ParamType = std::span<const float>;
40
41 using SmallRetType = std::span<float>;
42 using SmallElemType = float;
44
45
47
48 /**
49 * Construct from a subscriber handle; recommended to use
50 * FloatArrayTopic::Subscribe() instead.
51 *
52 * @param handle Native handle
53 * @param defaultValue Default value
54 */
56 : Subscriber{handle},
57 m_defaultValue{defaultValue.begin(), defaultValue.end()} {}
58
59 /**
60 * Get the last published value.
61 * If no value has been published, returns the stored default value.
62 *
63 * @return value
64 */
65 ValueType Get() const {
66 return Get(m_defaultValue);
67 }
68
69 /**
70 * Get the last published value.
71 * If no value has been published, returns the passed defaultValue.
72 *
73 * @param defaultValue default value to return if no value has been published
74 * @return value
75 */
76 ValueType Get(ParamType defaultValue) const {
77 return ::nt::GetFloatArray(m_subHandle, defaultValue);
78 }
79
80 /**
81 * Get the last published value.
82 * If no value has been published, returns the stored default value.
83 *
84 * @param buf storage for returned value
85 * @return value
86 */
88 return Get(buf, m_defaultValue);
89 }
90
91 /**
92 * Get the last published value.
93 * If no value has been published, returns the passed defaultValue.
94 *
95 * @param buf storage for returned value
96 * @param defaultValue default value to return if no value has been published
97 * @return value
98 */
100 return nt::GetFloatArray(m_subHandle, buf, defaultValue);
101 }
102
103 /**
104 * Get the last published value along with its timestamp
105 * If no value has been published, returns the stored default value and a
106 * timestamp of 0.
107 *
108 * @return timestamped value
109 */
111 return GetAtomic(m_defaultValue);
112 }
113
114 /**
115 * Get the last published value along with its timestamp.
116 * If no value has been published, returns the passed defaultValue and a
117 * timestamp of 0.
118 *
119 * @param defaultValue default value to return if no value has been published
120 * @return timestamped value
121 */
123 return ::nt::GetAtomicFloatArray(m_subHandle, defaultValue);
124 }
125
126 /**
127 * Get the last published value along with its timestamp.
128 * If no value has been published, returns the stored default value and a
129 * timestamp of 0.
130 *
131 * @param buf storage for returned value
132 * @return timestamped value
133 */
136 return GetAtomic(buf, m_defaultValue);
137 }
138
139 /**
140 * Get the last published value along with its timestamp.
141 * If no value has been published, returns the passed defaultValue and a
142 * timestamp of 0.
143 *
144 * @param buf storage for returned value
145 * @param defaultValue default value to return if no value has been published
146 * @return timestamped value
147 */
150 ParamType defaultValue) const {
151 return nt::GetAtomicFloatArray(m_subHandle, buf, defaultValue);
152 }
153
154 /**
155 * Get an array of all value changes since the last call to ReadQueue.
156 * Also provides a timestamp for each value.
157 *
158 * @note The "poll storage" subscribe option can be used to set the queue
159 * depth.
160 *
161 * @return Array of timestamped values; empty array if no new changes have
162 * been published since the previous call.
163 */
164 std::vector<TimestampedValueType> ReadQueue() {
165 return ::nt::ReadQueueFloatArray(m_subHandle);
166 }
167
168 /**
169 * Get the corresponding topic.
170 *
171 * @return Topic
172 */
173 TopicType GetTopic() const;
174
175 private:
176 ValueType m_defaultValue;
177};
178
179/**
180 * NetworkTables FloatArray publisher.
181 */
183 public:
185 using ValueType = std::vector<float>;
186 using ParamType = std::span<const float>;
187
188 using SmallRetType = std::span<float>;
189 using SmallElemType = float;
190
192
194
195 /**
196 * Construct from a publisher handle; recommended to use
197 * FloatArrayTopic::Publish() instead.
198 *
199 * @param handle Native handle
200 */
201 explicit FloatArrayPublisher(NT_Publisher handle) : Publisher{handle} {}
202
203 /**
204 * Publish a new value.
205 *
206 * @param value value to publish
207 * @param time timestamp; 0 indicates current NT time should be used
208 */
209 void Set(ParamType value, int64_t time = 0) {
210 ::nt::SetFloatArray(m_pubHandle, value, time);
211 }
212
213 /**
214 * Publish a default value.
215 * On reconnect, a default value will never be used in preference to a
216 * published value.
217 *
218 * @param value value
219 */
223
224 /**
225 * Get the corresponding topic.
226 *
227 * @return Topic
228 */
229 TopicType GetTopic() const;
230};
231
232/**
233 * NetworkTables FloatArray entry.
234 *
235 * @note Unlike NetworkTableEntry, the entry goes away when this is destroyed.
236 */
238 public FloatArrayPublisher {
239 public:
243 using ValueType = std::vector<float>;
244 using ParamType = std::span<const float>;
245
246 using SmallRetType = std::span<float>;
247 using SmallElemType = float;
248
250
251 FloatArrayEntry() = default;
252
253 /**
254 * Construct from an entry handle; recommended to use
255 * FloatArrayTopic::GetEntry() instead.
256 *
257 * @param handle Native handle
258 * @param defaultValue Default value
259 */
260 FloatArrayEntry(NT_Entry handle, ParamType defaultValue)
261 : FloatArraySubscriber{handle, defaultValue},
262 FloatArrayPublisher{handle} {}
263
264 /**
265 * Determines if the native handle is valid.
266 *
267 * @return True if the native handle is valid, false otherwise.
268 */
269 explicit operator bool() const { return m_subHandle != 0; }
270
271 /**
272 * Gets the native handle for the entry.
273 *
274 * @return Native handle
275 */
276 NT_Entry GetHandle() const { return m_subHandle; }
277
278 /**
279 * Get the corresponding topic.
280 *
281 * @return Topic
282 */
283 TopicType GetTopic() const;
284
285 /**
286 * Stops publishing the entry if it's published.
287 */
291};
292
293/**
294 * NetworkTables FloatArray topic.
295 */
296class FloatArrayTopic final : public Topic {
297 public:
301 using ValueType = std::vector<float>;
302 using ParamType = std::span<const float>;
304 /** The default type string for this topic type. */
305 static constexpr std::string_view kTypeString = "float[]";
306
307 FloatArrayTopic() = default;
308
309 /**
310 * Construct from a topic handle; recommended to use
311 * NetworkTableInstance::GetFloatArrayTopic() instead.
312 *
313 * @param handle Native handle
314 */
315 explicit FloatArrayTopic(NT_Topic handle) : Topic{handle} {}
316
317 /**
318 * Construct from a generic topic.
319 *
320 * @param topic Topic
321 */
322 explicit FloatArrayTopic(Topic topic) : Topic{topic} {}
323
324 /**
325 * Create a new subscriber to the topic.
326 *
327 * <p>The subscriber is only active as long as the returned object
328 * is not destroyed.
329 *
330 * @note Subscribers that do not match the published data type do not return
331 * any values. To determine if the data type matches, use the appropriate
332 * Topic functions.
333 *
334 * @param defaultValue default value used when a default is not provided to a
335 * getter function
336 * @param options subscribe options
337 * @return subscriber
338 */
339 [[nodiscard]]
341 ParamType defaultValue,
342 const PubSubOptions& options = kDefaultPubSubOptions) {
344 ::nt::Subscribe(m_handle, NT_FLOAT_ARRAY, "float[]", options),
345 defaultValue};
346 }
347 /**
348 * Create a new subscriber to the topic, with specific type string.
349 *
350 * <p>The subscriber is only active as long as the returned object
351 * is not destroyed.
352 *
353 * @note Subscribers that do not match the published data type do not return
354 * any values. To determine if the data type matches, use the appropriate
355 * Topic functions.
356 *
357 * @param typeString type string
358 * @param defaultValue default value used when a default is not provided to a
359 * getter function
360 * @param options subscribe options
361 * @return subscriber
362 */
363 [[nodiscard]]
365 std::string_view typeString, ParamType defaultValue,
366 const PubSubOptions& options = kDefaultPubSubOptions) {
368 ::nt::Subscribe(m_handle, NT_FLOAT_ARRAY, typeString, options),
369 defaultValue};
370 }
371
372 /**
373 * Create a new publisher to the topic.
374 *
375 * The publisher is only active as long as the returned object
376 * is not destroyed.
377 *
378 * @note It is not possible to publish two different data types to the same
379 * topic. Conflicts between publishers are typically resolved by the
380 * server on a first-come, first-served basis. Any published values that
381 * do not match the topic's data type are dropped (ignored). To determine
382 * if the data type matches, use the appropriate Topic functions.
383 *
384 * @param options publish options
385 * @return publisher
386 */
387 [[nodiscard]]
389 return FloatArrayPublisher{
390 ::nt::Publish(m_handle, NT_FLOAT_ARRAY, "float[]", options)};
391 }
392
393 /**
394 * Create a new publisher to the topic, with type string and initial
395 * properties.
396 *
397 * The publisher is only active as long as the returned object
398 * is not destroyed.
399 *
400 * @note It is not possible to publish two different data types to the same
401 * topic. Conflicts between publishers are typically resolved by the
402 * server on a first-come, first-served basis. Any published values that
403 * do not match the topic's data type are dropped (ignored). To determine
404 * if the data type matches, use the appropriate Topic functions.
405 *
406 * @param typeString type string
407 * @param properties JSON properties
408 * @param options publish options
409 * @return publisher
410 */
411 [[nodiscard]]
412 PublisherType PublishEx(std::string_view typeString,
413 const wpi::json& properties, const PubSubOptions& options = kDefaultPubSubOptions) {
414 return FloatArrayPublisher{
415 ::nt::PublishEx(m_handle, NT_FLOAT_ARRAY, typeString, properties, options)};
416 }
417
418 /**
419 * Create a new entry for the topic.
420 *
421 * Entries act as a combination of a subscriber and a weak publisher. The
422 * subscriber is active as long as the entry is not destroyed. The publisher
423 * is created when the entry is first written to, and remains active until
424 * either Unpublish() is called or the entry is destroyed.
425 *
426 * @note It is not possible to use two different data types with the same
427 * topic. Conflicts between publishers are typically resolved by the
428 * server on a first-come, first-served basis. Any published values that
429 * do not match the topic's data type are dropped (ignored), and the entry
430 * will show no new values if the data type does not match. To determine
431 * if the data type matches, use the appropriate Topic functions.
432 *
433 * @param defaultValue default value used when a default is not provided to a
434 * getter function
435 * @param options publish and/or subscribe options
436 * @return entry
437 */
438 [[nodiscard]]
440 const PubSubOptions& options = kDefaultPubSubOptions) {
441 return FloatArrayEntry{
442 ::nt::GetEntry(m_handle, NT_FLOAT_ARRAY, "float[]", options),
443 defaultValue};
444 }
445 /**
446 * Create a new entry for the topic, with specific type string.
447 *
448 * Entries act as a combination of a subscriber and a weak publisher. The
449 * subscriber is active as long as the entry is not destroyed. The publisher
450 * is created when the entry is first written to, and remains active until
451 * either Unpublish() is called or the entry is destroyed.
452 *
453 * @note It is not possible to use two different data types with the same
454 * topic. Conflicts between publishers are typically resolved by the
455 * server on a first-come, first-served basis. Any published values that
456 * do not match the topic's data type are dropped (ignored), and the entry
457 * will show no new values if the data type does not match. To determine
458 * if the data type matches, use the appropriate Topic functions.
459 *
460 * @param typeString type string
461 * @param defaultValue default value used when a default is not provided to a
462 * getter function
463 * @param options publish and/or subscribe options
464 * @return entry
465 */
466 [[nodiscard]]
467 EntryType GetEntryEx(std::string_view typeString, ParamType defaultValue,
468 const PubSubOptions& options = kDefaultPubSubOptions) {
469 return FloatArrayEntry{
470 ::nt::GetEntry(m_handle, NT_FLOAT_ARRAY, typeString, options),
471 defaultValue};
472 }
473
474};
475
479
483
487
488} // namespace nt
NetworkTables FloatArray entry.
Definition FloatArrayTopic.h:238
float SmallElemType
Definition FloatArrayTopic.h:247
FloatArrayEntry(NT_Entry handle, ParamType defaultValue)
Construct from an entry handle; recommended to use FloatArrayTopic::GetEntry() instead.
Definition FloatArrayTopic.h:260
std::span< const float > ParamType
Definition FloatArrayTopic.h:244
FloatArrayTopic TopicType
Definition FloatArrayTopic.h:242
std::vector< float > ValueType
Definition FloatArrayTopic.h:243
void Unpublish()
Stops publishing the entry if it's published.
Definition FloatArrayTopic.h:288
FloatArrayEntry()=default
std::span< float > SmallRetType
Definition FloatArrayTopic.h:246
TopicType GetTopic() const
Get the corresponding topic.
Definition FloatArrayTopic.h:484
NT_Entry GetHandle() const
Gets the native handle for the entry.
Definition FloatArrayTopic.h:276
NetworkTables FloatArray publisher.
Definition FloatArrayTopic.h:182
FloatArrayTopic TopicType
Definition FloatArrayTopic.h:184
void Set(ParamType value, int64_t time=0)
Publish a new value.
Definition FloatArrayTopic.h:209
FloatArrayPublisher(NT_Publisher handle)
Construct from a publisher handle; recommended to use FloatArrayTopic::Publish() instead.
Definition FloatArrayTopic.h:201
void SetDefault(ParamType value)
Publish a default value.
Definition FloatArrayTopic.h:220
TopicType GetTopic() const
Get the corresponding topic.
Definition FloatArrayTopic.h:480
std::vector< float > ValueType
Definition FloatArrayTopic.h:185
float SmallElemType
Definition FloatArrayTopic.h:189
std::span< float > SmallRetType
Definition FloatArrayTopic.h:188
std::span< const float > ParamType
Definition FloatArrayTopic.h:186
NetworkTables FloatArray subscriber.
Definition FloatArrayTopic.h:34
std::span< const float > ParamType
Definition FloatArrayTopic.h:38
std::span< float > SmallRetType
Definition FloatArrayTopic.h:41
std::vector< float > ValueType
Definition FloatArrayTopic.h:37
FloatArraySubscriber(NT_Subscriber handle, ParamType defaultValue)
Construct from a subscriber handle; recommended to use FloatArrayTopic::Subscribe() instead.
Definition FloatArrayTopic.h:55
ValueType Get() const
Get the last published value.
Definition FloatArrayTopic.h:65
TopicType GetTopic() const
Get the corresponding topic.
Definition FloatArrayTopic.h:476
SmallRetType Get(wpi::SmallVectorImpl< SmallElemType > &buf) const
Get the last published value.
Definition FloatArrayTopic.h:87
ValueType Get(ParamType defaultValue) const
Get the last published value.
Definition FloatArrayTopic.h:76
std::vector< TimestampedValueType > ReadQueue()
Get an array of all value changes since the last call to ReadQueue.
Definition FloatArrayTopic.h:164
SmallRetType Get(wpi::SmallVectorImpl< SmallElemType > &buf, ParamType defaultValue) const
Get the last published value.
Definition FloatArrayTopic.h:99
TimestampedValueViewType GetAtomic(wpi::SmallVectorImpl< SmallElemType > &buf) const
Get the last published value along with its timestamp.
Definition FloatArrayTopic.h:134
float SmallElemType
Definition FloatArrayTopic.h:42
TimestampedValueViewType GetAtomic(wpi::SmallVectorImpl< SmallElemType > &buf, ParamType defaultValue) const
Get the last published value along with its timestamp.
Definition FloatArrayTopic.h:148
TimestampedValueType GetAtomic() const
Get the last published value along with its timestamp If no value has been published,...
Definition FloatArrayTopic.h:110
FloatArrayTopic TopicType
Definition FloatArrayTopic.h:36
TimestampedValueType GetAtomic(ParamType defaultValue) const
Get the last published value along with its timestamp.
Definition FloatArrayTopic.h:122
NetworkTables FloatArray topic.
Definition FloatArrayTopic.h:296
FloatArrayTopic(Topic topic)
Construct from a generic topic.
Definition FloatArrayTopic.h:322
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.h:412
FloatArrayTopic()=default
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.h:364
std::span< const float > ParamType
Definition FloatArrayTopic.h:302
EntryType GetEntry(ParamType defaultValue, const PubSubOptions &options=kDefaultPubSubOptions)
Create a new entry for the topic.
Definition FloatArrayTopic.h:439
static constexpr std::string_view kTypeString
The default type string for this topic type.
Definition FloatArrayTopic.h:305
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.h:467
SubscriberType Subscribe(ParamType defaultValue, const PubSubOptions &options=kDefaultPubSubOptions)
Create a new subscriber to the topic.
Definition FloatArrayTopic.h:340
std::vector< float > ValueType
Definition FloatArrayTopic.h:301
PublisherType Publish(const PubSubOptions &options=kDefaultPubSubOptions)
Create a new publisher to the topic.
Definition FloatArrayTopic.h:388
FloatArrayTopic(NT_Topic handle)
Construct from a topic handle; recommended to use NetworkTableInstance::GetFloatArrayTopic() instead.
Definition FloatArrayTopic.h:315
NetworkTables publisher.
Definition Topic.h:393
NT_Publisher m_pubHandle
NetworkTables handle.
Definition Topic.h:441
NetworkTables subscriber.
Definition Topic.h:321
NT_Subscriber m_subHandle
Definition Topic.h:386
NetworkTables Topic.
Definition Topic.h:28
NT_Topic m_handle
Definition Topic.h:317
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
Definition sha1.h:30
std::vector< float > GetFloatArray(NT_Handle subentry, std::span< const float > defaultValue)
Get the last published value.
bool SetDefaultFloatArray(NT_Handle pubentry, std::span< const float > defaultValue)
Publish a default value.
bool SetFloatArray(NT_Handle pubentry, std::span< const float > value, int64_t time=0)
Publish a new value.
TimestampedFloatArray GetAtomicFloatArray(NT_Handle subentry, std::span< const float > defaultValue)
Get the last published value along with its timestamp.
NT_Handle NT_Topic
Definition ntcore_c.h:42
NT_Handle NT_Subscriber
Definition ntcore_c.h:43
NT_Handle NT_Publisher
Definition ntcore_c.h:44
NT_Handle NT_Entry
Definition ntcore_c.h:37
@ NT_FLOAT_ARRAY
Definition ntcore_c.h:66
Timestamped< std::span< float > > TimestampedFloatArrayView
Timestamped FloatArray view (for SmallVector-taking functions).
Definition ntcore_cpp_types.h:745
Timestamped< std::vector< float > > TimestampedFloatArray
Timestamped FloatArray.
Definition ntcore_cpp_types.h:739
constexpr PubSubOptions kDefaultPubSubOptions
Default publish/subscribe options.
Definition ntcore_cpp.h:390
NT_Entry GetEntry(NT_Inst inst, std::string_view name)
Get Entry Handle.
NT_Topic GetTopicFromHandle(NT_Handle pubsubentry)
Gets the topic handle from an entry/subscriber/publisher handle.
NT_Publisher PublishEx(NT_Topic topic, NT_Type type, std::string_view typeStr, const wpi::json &properties, const PubSubOptions &options=kDefaultPubSubOptions)
Creates a new publisher to a topic.
NT_Publisher Publish(NT_Topic topic, NT_Type type, std::string_view typeStr, const PubSubOptions &options=kDefaultPubSubOptions)
Creates a new publisher to a topic.
void Unpublish(NT_Handle pubentry)
Stops publisher.
NT_Subscriber Subscribe(NT_Topic topic, NT_Type type, std::string_view typeStr, const PubSubOptions &options=kDefaultPubSubOptions)
Creates a new subscriber to value changes on a topic.
NetworkTables (ntcore) namespace.
Definition ntcore_cpp.h:36
Foonathan namespace.
Definition ntcore_cpp.h:26
NetworkTables publish/subscribe options.
Definition ntcore_cpp.h:305
Timestamped value.
Definition ntcore_cpp_types.h:30