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