WPILibC++ 2025.2.1
Loading...
Searching...
No Matches
DoubleArrayTopic.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 DoubleArrayTopic;
30
31/**
32 * NetworkTables DoubleArray subscriber.
33 */
35 public:
37 using ValueType = std::vector<double>;
38 using ParamType = std::span<const double>;
40
41 using SmallRetType = std::span<double>;
42 using SmallElemType = double;
44
45
47
48 /**
49 * Construct from a subscriber handle; recommended to use
50 * DoubleArrayTopic::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::GetDoubleArray(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::GetDoubleArray(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::GetAtomicDoubleArray(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::GetAtomicDoubleArray(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::ReadQueueDoubleArray(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 DoubleArray publisher.
181 */
183 public:
185 using ValueType = std::vector<double>;
186 using ParamType = std::span<const double>;
187
188 using SmallRetType = std::span<double>;
189 using SmallElemType = double;
190
192
194
195 /**
196 * Construct from a publisher handle; recommended to use
197 * DoubleArrayTopic::Publish() instead.
198 *
199 * @param handle Native handle
200 */
201 explicit DoubleArrayPublisher(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::SetDoubleArray(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 DoubleArray entry.
234 *
235 * @note Unlike NetworkTableEntry, the entry goes away when this is destroyed.
236 */
238 public DoubleArrayPublisher {
239 public:
243 using ValueType = std::vector<double>;
244 using ParamType = std::span<const double>;
245
246 using SmallRetType = std::span<double>;
247 using SmallElemType = double;
248
250
251 DoubleArrayEntry() = default;
252
253 /**
254 * Construct from an entry handle; recommended to use
255 * DoubleArrayTopic::GetEntry() instead.
256 *
257 * @param handle Native handle
258 * @param defaultValue Default value
259 */
260 DoubleArrayEntry(NT_Entry handle, ParamType defaultValue)
261 : DoubleArraySubscriber{handle, defaultValue},
262 DoubleArrayPublisher{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 DoubleArray topic.
295 */
296class DoubleArrayTopic final : public Topic {
297 public:
301 using ValueType = std::vector<double>;
302 using ParamType = std::span<const double>;
304 /** The default type string for this topic type. */
305 static constexpr std::string_view kTypeString = "double[]";
306
307 DoubleArrayTopic() = default;
308
309 /**
310 * Construct from a topic handle; recommended to use
311 * NetworkTableInstance::GetDoubleArrayTopic() instead.
312 *
313 * @param handle Native handle
314 */
315 explicit DoubleArrayTopic(NT_Topic handle) : Topic{handle} {}
316
317 /**
318 * Construct from a generic topic.
319 *
320 * @param topic Topic
321 */
322 explicit DoubleArrayTopic(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_DOUBLE_ARRAY, "double[]", 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_DOUBLE_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]]
390 ::nt::Publish(m_handle, NT_DOUBLE_ARRAY, "double[]", 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) {
415 ::nt::PublishEx(m_handle, NT_DOUBLE_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 DoubleArrayEntry{
442 ::nt::GetEntry(m_handle, NT_DOUBLE_ARRAY, "double[]", 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 DoubleArrayEntry{
470 ::nt::GetEntry(m_handle, NT_DOUBLE_ARRAY, typeString, options),
471 defaultValue};
472 }
473
474};
475
479
483
487
488} // namespace nt
NetworkTables DoubleArray entry.
Definition DoubleArrayTopic.h:238
DoubleArrayEntry()=default
std::span< const double > ParamType
Definition DoubleArrayTopic.h:244
DoubleArrayTopic TopicType
Definition DoubleArrayTopic.h:242
TopicType GetTopic() const
Get the corresponding topic.
Definition DoubleArrayTopic.h:484
std::vector< double > ValueType
Definition DoubleArrayTopic.h:243
void Unpublish()
Stops publishing the entry if it's published.
Definition DoubleArrayTopic.h:288
double SmallElemType
Definition DoubleArrayTopic.h:247
NT_Entry GetHandle() const
Gets the native handle for the entry.
Definition DoubleArrayTopic.h:276
DoubleArrayEntry(NT_Entry handle, ParamType defaultValue)
Construct from an entry handle; recommended to use DoubleArrayTopic::GetEntry() instead.
Definition DoubleArrayTopic.h:260
std::span< double > SmallRetType
Definition DoubleArrayTopic.h:246
NetworkTables DoubleArray publisher.
Definition DoubleArrayTopic.h:182
TopicType GetTopic() const
Get the corresponding topic.
Definition DoubleArrayTopic.h:480
void Set(ParamType value, int64_t time=0)
Publish a new value.
Definition DoubleArrayTopic.h:209
std::span< double > SmallRetType
Definition DoubleArrayTopic.h:188
DoubleArrayPublisher(NT_Publisher handle)
Construct from a publisher handle; recommended to use DoubleArrayTopic::Publish() instead.
Definition DoubleArrayTopic.h:201
double SmallElemType
Definition DoubleArrayTopic.h:189
void SetDefault(ParamType value)
Publish a default value.
Definition DoubleArrayTopic.h:220
std::vector< double > ValueType
Definition DoubleArrayTopic.h:185
DoubleArrayTopic TopicType
Definition DoubleArrayTopic.h:184
std::span< const double > ParamType
Definition DoubleArrayTopic.h:186
NetworkTables DoubleArray subscriber.
Definition DoubleArrayTopic.h:34
SmallRetType Get(wpi::SmallVectorImpl< SmallElemType > &buf) const
Get the last published value.
Definition DoubleArrayTopic.h:87
std::vector< double > ValueType
Definition DoubleArrayTopic.h:37
TimestampedValueViewType GetAtomic(wpi::SmallVectorImpl< SmallElemType > &buf, ParamType defaultValue) const
Get the last published value along with its timestamp.
Definition DoubleArrayTopic.h:148
ValueType Get() const
Get the last published value.
Definition DoubleArrayTopic.h:65
ValueType Get(ParamType defaultValue) const
Get the last published value.
Definition DoubleArrayTopic.h:76
std::vector< TimestampedValueType > ReadQueue()
Get an array of all value changes since the last call to ReadQueue.
Definition DoubleArrayTopic.h:164
TopicType GetTopic() const
Get the corresponding topic.
Definition DoubleArrayTopic.h:476
TimestampedValueType GetAtomic() const
Get the last published value along with its timestamp If no value has been published,...
Definition DoubleArrayTopic.h:110
TimestampedValueViewType GetAtomic(wpi::SmallVectorImpl< SmallElemType > &buf) const
Get the last published value along with its timestamp.
Definition DoubleArrayTopic.h:134
DoubleArraySubscriber(NT_Subscriber handle, ParamType defaultValue)
Construct from a subscriber handle; recommended to use DoubleArrayTopic::Subscribe() instead.
Definition DoubleArrayTopic.h:55
SmallRetType Get(wpi::SmallVectorImpl< SmallElemType > &buf, ParamType defaultValue) const
Get the last published value.
Definition DoubleArrayTopic.h:99
double SmallElemType
Definition DoubleArrayTopic.h:42
std::span< double > SmallRetType
Definition DoubleArrayTopic.h:41
std::span< const double > ParamType
Definition DoubleArrayTopic.h:38
DoubleArrayTopic TopicType
Definition DoubleArrayTopic.h:36
TimestampedValueType GetAtomic(ParamType defaultValue) const
Get the last published value along with its timestamp.
Definition DoubleArrayTopic.h:122
NetworkTables DoubleArray topic.
Definition DoubleArrayTopic.h:296
PublisherType Publish(const PubSubOptions &options=kDefaultPubSubOptions)
Create a new publisher to the topic.
Definition DoubleArrayTopic.h:388
SubscriberType Subscribe(ParamType defaultValue, const PubSubOptions &options=kDefaultPubSubOptions)
Create a new subscriber to the topic.
Definition DoubleArrayTopic.h:340
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 DoubleArrayTopic.h:412
static constexpr std::string_view kTypeString
The default type string for this topic type.
Definition DoubleArrayTopic.h:305
SubscriberType SubscribeEx(std::string_view typeString, ParamType defaultValue, const PubSubOptions &options=kDefaultPubSubOptions)
Create a new subscriber to the topic, with specific type string.
Definition DoubleArrayTopic.h:364
std::span< const double > ParamType
Definition DoubleArrayTopic.h:302
DoubleArrayTopic(Topic topic)
Construct from a generic topic.
Definition DoubleArrayTopic.h:322
DoubleArrayTopic()=default
EntryType GetEntry(ParamType defaultValue, const PubSubOptions &options=kDefaultPubSubOptions)
Create a new entry for the topic.
Definition DoubleArrayTopic.h:439
std::vector< double > ValueType
Definition DoubleArrayTopic.h:301
EntryType GetEntryEx(std::string_view typeString, ParamType defaultValue, const PubSubOptions &options=kDefaultPubSubOptions)
Create a new entry for the topic, with specific type string.
Definition DoubleArrayTopic.h:467
DoubleArrayTopic(NT_Topic handle)
Construct from a topic handle; recommended to use NetworkTableInstance::GetDoubleArrayTopic() instead...
Definition DoubleArrayTopic.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
bool SetDoubleArray(NT_Handle pubentry, std::span< const double > value, int64_t time=0)
Publish a new value.
bool SetDefaultDoubleArray(NT_Handle pubentry, std::span< const double > defaultValue)
Publish a default value.
std::vector< double > GetDoubleArray(NT_Handle subentry, std::span< const double > defaultValue)
Get the last published value.
TimestampedDoubleArray GetAtomicDoubleArray(NT_Handle subentry, std::span< const double > 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_DOUBLE_ARRAY
Definition ntcore_c.h:60
Timestamped< std::span< double > > TimestampedDoubleArrayView
Timestamped DoubleArray view (for SmallVector-taking functions).
Definition ntcore_cpp_types.h:837
Timestamped< std::vector< double > > TimestampedDoubleArray
Timestamped DoubleArray.
Definition ntcore_cpp_types.h:831
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