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