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