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