WPILibC++ 2023.4.3
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#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 DoubleArrayTopic;
25
26/**
27 * NetworkTables DoubleArray subscriber.
28 */
30 public:
32 using ValueType = std::vector<double>;
33 using ParamType = std::span<const double>;
35
36 using SmallRetType = std::span<double>;
37 using SmallElemType = double;
39
40
42
43 /**
44 * Construct from a subscriber handle; recommended to use
45 * DoubleArrayTopic::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 DoubleArray publisher.
156 */
158 public:
160 using ValueType = std::vector<double>;
161 using ParamType = std::span<const double>;
162
163 using SmallRetType = std::span<double>;
164 using SmallElemType = double;
165
167
169
170 /**
171 * Construct from a publisher handle; recommended to use
172 * DoubleArrayTopic::Publish() instead.
173 *
174 * @param handle Native handle
175 */
176 explicit DoubleArrayPublisher(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 DoubleArray entry.
205 *
206 * @note Unlike NetworkTableEntry, the entry goes away when this is destroyed.
207 */
209 public DoubleArrayPublisher {
210 public:
214 using ValueType = std::vector<double>;
215 using ParamType = std::span<const double>;
216
217 using SmallRetType = std::span<double>;
218 using SmallElemType = double;
219
221
222 DoubleArrayEntry() = default;
223
224 /**
225 * Construct from an entry handle; recommended to use
226 * DoubleArrayTopic::GetEntry() instead.
227 *
228 * @param handle Native handle
229 * @param defaultValue Default value
230 */
231 DoubleArrayEntry(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 DoubleArray topic.
262 */
263class DoubleArrayTopic final : public Topic {
264 public:
268 using ValueType = std::vector<double>;
269 using ParamType = std::span<const double>;
271 /** The default type string for this topic type. */
272 static constexpr std::string_view kTypeString = "double[]";
273
274 DoubleArrayTopic() = default;
275
276 /**
277 * Construct from a topic handle; recommended to use
278 * NetworkTableInstance::GetDoubleArrayTopic() instead.
279 *
280 * @param handle Native handle
281 */
282 explicit DoubleArrayTopic(NT_Topic handle) : Topic{handle} {}
283
284 /**
285 * Construct from a generic topic.
286 *
287 * @param topic Topic
288 */
289 explicit DoubleArrayTopic(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 DoubleArray entry.
Definition: DoubleArrayTopic.h:209
DoubleArrayEntry()=default
DoubleArrayTopic TopicType
Definition: DoubleArrayTopic.h:213
TopicType GetTopic() const
Get the corresponding topic.
Definition: DoubleArrayTopic.inc:82
void Unpublish()
Stops publishing the entry if it's published.
Definition: DoubleArrayTopic.inc:86
NT_Entry GetHandle() const
Gets the native handle for the entry.
Definition: DoubleArrayTopic.h:245
DoubleArrayEntry(NT_Entry handle, ParamType defaultValue)
Construct from an entry handle; recommended to use DoubleArrayTopic::GetEntry() instead.
NetworkTables DoubleArray publisher.
Definition: DoubleArrayTopic.h:157
TopicType GetTopic() const
Get the corresponding topic.
Definition: DoubleArrayTopic.inc:73
void Set(ParamType value, int64_t time=0)
Publish a new value.
Definition: DoubleArrayTopic.inc:64
std::span< double > SmallRetType
Definition: DoubleArrayTopic.h:163
double SmallElemType
Definition: DoubleArrayTopic.h:164
void SetDefault(ParamType value)
Publish a default value.
Definition: DoubleArrayTopic.inc:69
std::vector< double > ValueType
Definition: DoubleArrayTopic.h:160
std::span< const double > ParamType
Definition: DoubleArrayTopic.h:161
NetworkTables DoubleArray subscriber.
Definition: DoubleArrayTopic.h:29
std::vector< double > ValueType
Definition: DoubleArrayTopic.h:32
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: DoubleArrayTopic.inc:18
ValueType Get(ParamType defaultValue) const
Get the last published value.
std::vector< TimestampedValueType > ReadQueue()
Get an array of all value changes since the last call to ReadQueue.
Definition: DoubleArrayTopic.inc:53
TopicType GetTopic() const
Get the corresponding topic.
Definition: DoubleArrayTopic.inc:57
DoubleArraySubscriber(NT_Subscriber handle, ParamType defaultValue)
Construct from a subscriber handle; recommended to use DoubleArrayTopic::Subscribe() instead.
SmallRetType Get(wpi::SmallVectorImpl< SmallElemType > &buf, ParamType defaultValue) const
Get the last published value.
double SmallElemType
Definition: DoubleArrayTopic.h:37
std::span< double > SmallRetType
Definition: DoubleArrayTopic.h:36
std::span< const double > ParamType
Definition: DoubleArrayTopic.h:33
TimestampedValueType GetAtomic(ParamType defaultValue) const
Get the last published value along with its timestamp.
TimestampedValueType GetAtomic() const
Get the last published value along with its timestamp If no value has been published,...
Definition: DoubleArrayTopic.inc:35
NetworkTables DoubleArray topic.
Definition: DoubleArrayTopic.h:263
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.inc:125
EntryType GetEntry(ParamType defaultValue, const PubSubOptions &options=kDefaultPubSubOptions)
Create a new entry for the topic.
Definition: DoubleArrayTopic.inc:118
DoubleArrayEntry EntryType
Definition: DoubleArrayTopic.h:267
static constexpr std::string_view kTypeString
The default type string for this topic type.
Definition: DoubleArrayTopic.h:272
SubscriberType Subscribe(ParamType defaultValue, const PubSubOptions &options=kDefaultPubSubOptions)
Create a new subscriber to the topic.
Definition: DoubleArrayTopic.inc:90
std::span< const double > ParamType
Definition: DoubleArrayTopic.h:269
DoubleArrayTopic(Topic topic)
Construct from a generic topic.
Definition: DoubleArrayTopic.h:289
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.inc:97
DoubleArrayTopic()=default
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.inc:111
PublisherType Publish(const PubSubOptions &options=kDefaultPubSubOptions)
Create a new publisher to the topic.
Definition: DoubleArrayTopic.inc:105
DoubleArrayPublisher PublisherType
Definition: DoubleArrayTopic.h:266
std::vector< double > ValueType
Definition: DoubleArrayTopic.h:268
DoubleArraySubscriber SubscriberType
Definition: DoubleArrayTopic.h:265
DoubleArrayTopic(NT_Topic handle)
Construct from a topic handle; recommended to use NetworkTableInstance::GetDoubleArrayTopic() instead...
Definition: DoubleArrayTopic.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 DoubleArray.
Definition: ntcore_cpp_types.h:1070
Timestamped DoubleArray view (for SmallVector-taking functions).
Definition: ntcore_cpp_types.h:1095