WPILibC++ 2025.2.1
Loading...
Searching...
No Matches
StringTopic.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#include <string>
12#include <string_view>
13#include <utility>
14#include <span>
15#include <string_view>
16#include <vector>
17
18#include <wpi/json_fwd.h>
19
21#include "networktables/Topic.h"
22#include "ntcore_cpp.h"
23
24namespace wpi {
25template <typename T>
26class SmallVectorImpl;
27} // namespace wpi
28
29namespace nt {
30
31class StringTopic;
32
33/**
34 * NetworkTables String subscriber.
35 */
37 public:
39 using ValueType = std::string;
40 using ParamType = std::string_view;
42
43 using SmallRetType = std::string_view;
44 using SmallElemType = char;
46
47
48 StringSubscriber() = default;
49
50 /**
51 * Construct from a subscriber handle; recommended to use
52 * StringTopic::Subscribe() instead.
53 *
54 * @param handle Native handle
55 * @param defaultValue Default value
56 */
58 : Subscriber{handle},
59 m_defaultValue{defaultValue} {}
60
61 /**
62 * Get the last published value.
63 * If no value has been published, returns the stored default value.
64 *
65 * @return value
66 */
67 ValueType Get() const {
68 return Get(m_defaultValue);
69 }
70
71 /**
72 * Get the last published value.
73 * If no value has been published, returns the passed defaultValue.
74 *
75 * @param defaultValue default value to return if no value has been published
76 * @return value
77 */
78 ValueType Get(ParamType defaultValue) const {
79 return ::nt::GetString(m_subHandle, defaultValue);
80 }
81
82 /**
83 * Get the last published value.
84 * If no value has been published, returns the stored default value.
85 *
86 * @param buf storage for returned value
87 * @return value
88 */
90 return Get(buf, m_defaultValue);
91 }
92
93 /**
94 * Get the last published value.
95 * If no value has been published, returns the passed defaultValue.
96 *
97 * @param buf storage for returned value
98 * @param defaultValue default value to return if no value has been published
99 * @return value
100 */
102 return nt::GetString(m_subHandle, buf, defaultValue);
103 }
104
105 /**
106 * Get the last published value along with its timestamp
107 * If no value has been published, returns the stored default value and a
108 * timestamp of 0.
109 *
110 * @return timestamped value
111 */
113 return GetAtomic(m_defaultValue);
114 }
115
116 /**
117 * Get the last published value along with its timestamp.
118 * If no value has been published, returns the passed defaultValue and a
119 * timestamp of 0.
120 *
121 * @param defaultValue default value to return if no value has been published
122 * @return timestamped value
123 */
125 return ::nt::GetAtomicString(m_subHandle, defaultValue);
126 }
127
128 /**
129 * Get the last published value along with its timestamp.
130 * If no value has been published, returns the stored default value and a
131 * timestamp of 0.
132 *
133 * @param buf storage for returned value
134 * @return timestamped value
135 */
138 return GetAtomic(buf, m_defaultValue);
139 }
140
141 /**
142 * Get the last published value along with its timestamp.
143 * If no value has been published, returns the passed defaultValue and a
144 * timestamp of 0.
145 *
146 * @param buf storage for returned value
147 * @param defaultValue default value to return if no value has been published
148 * @return timestamped value
149 */
152 ParamType defaultValue) const {
153 return nt::GetAtomicString(m_subHandle, buf, defaultValue);
154 }
155
156 /**
157 * Get an array of all value changes since the last call to ReadQueue.
158 * Also provides a timestamp for each value.
159 *
160 * @note The "poll storage" subscribe option can be used to set the queue
161 * depth.
162 *
163 * @return Array of timestamped values; empty array if no new changes have
164 * been published since the previous call.
165 */
166 std::vector<TimestampedValueType> ReadQueue() {
167 return ::nt::ReadQueueString(m_subHandle);
168 }
169
170 /**
171 * Get the corresponding topic.
172 *
173 * @return Topic
174 */
175 TopicType GetTopic() const;
176
177 private:
178 ValueType m_defaultValue;
179};
180
181/**
182 * NetworkTables String publisher.
183 */
185 public:
187 using ValueType = std::string;
188 using ParamType = std::string_view;
189
190 using SmallRetType = std::string_view;
191 using SmallElemType = char;
192
194
195 StringPublisher() = default;
196
197 /**
198 * Construct from a publisher handle; recommended to use
199 * StringTopic::Publish() instead.
200 *
201 * @param handle Native handle
202 */
203 explicit StringPublisher(NT_Publisher handle) : Publisher{handle} {}
204
205 /**
206 * Publish a new value.
207 *
208 * @param value value to publish
209 * @param time timestamp; 0 indicates current NT time should be used
210 */
211 void Set(ParamType value, int64_t time = 0) {
212 ::nt::SetString(m_pubHandle, value, time);
213 }
214
215 /**
216 * Publish a default value.
217 * On reconnect, a default value will never be used in preference to a
218 * published value.
219 *
220 * @param value value
221 */
222 void SetDefault(ParamType value) {
224 }
225
226 /**
227 * Get the corresponding topic.
228 *
229 * @return Topic
230 */
231 TopicType GetTopic() const;
232};
233
234/**
235 * NetworkTables String entry.
236 *
237 * @note Unlike NetworkTableEntry, the entry goes away when this is destroyed.
238 */
239class StringEntry final : public StringSubscriber,
240 public StringPublisher {
241 public:
245 using ValueType = std::string;
246 using ParamType = std::string_view;
247
248 using SmallRetType = std::string_view;
249 using SmallElemType = char;
250
252
253 StringEntry() = default;
254
255 /**
256 * Construct from an entry handle; recommended to use
257 * StringTopic::GetEntry() instead.
258 *
259 * @param handle Native handle
260 * @param defaultValue Default value
261 */
262 StringEntry(NT_Entry handle, ParamType defaultValue)
263 : StringSubscriber{handle, defaultValue},
264 StringPublisher{handle} {}
265
266 /**
267 * Determines if the native handle is valid.
268 *
269 * @return True if the native handle is valid, false otherwise.
270 */
271 explicit operator bool() const { return m_subHandle != 0; }
272
273 /**
274 * Gets the native handle for the entry.
275 *
276 * @return Native handle
277 */
278 NT_Entry GetHandle() const { return m_subHandle; }
279
280 /**
281 * Get the corresponding topic.
282 *
283 * @return Topic
284 */
285 TopicType GetTopic() const;
286
287 /**
288 * Stops publishing the entry if it's published.
289 */
293};
294
295/**
296 * NetworkTables String topic.
297 */
298class StringTopic final : public Topic {
299 public:
303 using ValueType = std::string;
304 using ParamType = std::string_view;
306 /** The default type string for this topic type. */
307 static constexpr std::string_view kTypeString = "string";
308
309 StringTopic() = default;
310
311 /**
312 * Construct from a topic handle; recommended to use
313 * NetworkTableInstance::GetStringTopic() instead.
314 *
315 * @param handle Native handle
316 */
317 explicit StringTopic(NT_Topic handle) : Topic{handle} {}
318
319 /**
320 * Construct from a generic topic.
321 *
322 * @param topic Topic
323 */
324 explicit StringTopic(Topic topic) : Topic{topic} {}
325
326 /**
327 * Create a new subscriber to the topic.
328 *
329 * <p>The subscriber is only active as long as the returned object
330 * is not destroyed.
331 *
332 * @note Subscribers that do not match the published data type do not return
333 * any values. To determine if the data type matches, use the appropriate
334 * Topic functions.
335 *
336 * @param defaultValue default value used when a default is not provided to a
337 * getter function
338 * @param options subscribe options
339 * @return subscriber
340 */
341 [[nodiscard]]
343 ParamType defaultValue,
344 const PubSubOptions& options = kDefaultPubSubOptions) {
345 return StringSubscriber{
346 ::nt::Subscribe(m_handle, NT_STRING, "string", options),
347 defaultValue};
348 }
349 /**
350 * Create a new subscriber to the topic, with specific type string.
351 *
352 * <p>The subscriber is only active as long as the returned object
353 * is not destroyed.
354 *
355 * @note Subscribers that do not match the published data type do not return
356 * any values. To determine if the data type matches, use the appropriate
357 * 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 subscribe options
363 * @return subscriber
364 */
365 [[nodiscard]]
367 std::string_view typeString, ParamType defaultValue,
368 const PubSubOptions& options = kDefaultPubSubOptions) {
369 return StringSubscriber{
370 ::nt::Subscribe(m_handle, NT_STRING, typeString, options),
371 defaultValue};
372 }
373
374 /**
375 * Create a new publisher to the topic.
376 *
377 * The publisher is only active as long as the returned object
378 * is not destroyed.
379 *
380 * @note It is not possible to publish two different data types to the same
381 * topic. Conflicts between publishers are typically resolved by the
382 * server on a first-come, first-served basis. Any published values that
383 * do not match the topic's data type are dropped (ignored). To determine
384 * if the data type matches, use the appropriate Topic functions.
385 *
386 * @param options publish options
387 * @return publisher
388 */
389 [[nodiscard]]
391 return StringPublisher{
392 ::nt::Publish(m_handle, NT_STRING, "string", options)};
393 }
394
395 /**
396 * Create a new publisher to the topic, with type string and initial
397 * properties.
398 *
399 * The publisher is only active as long as the returned object
400 * is not destroyed.
401 *
402 * @note It is not possible to publish two different data types to 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). To determine
406 * if the data type matches, use the appropriate Topic functions.
407 *
408 * @param typeString type string
409 * @param properties JSON properties
410 * @param options publish options
411 * @return publisher
412 */
413 [[nodiscard]]
414 PublisherType PublishEx(std::string_view typeString,
415 const wpi::json& properties, const PubSubOptions& options = kDefaultPubSubOptions) {
416 return StringPublisher{
417 ::nt::PublishEx(m_handle, NT_STRING, typeString, properties, options)};
418 }
419
420 /**
421 * Create a new entry for the topic.
422 *
423 * Entries act as a combination of a subscriber and a weak publisher. The
424 * subscriber is active as long as the entry is not destroyed. The publisher
425 * is created when the entry is first written to, and remains active until
426 * either Unpublish() is called or the entry is destroyed.
427 *
428 * @note It is not possible to use two different data types with the same
429 * topic. Conflicts between publishers are typically resolved by the
430 * server on a first-come, first-served basis. Any published values that
431 * do not match the topic's data type are dropped (ignored), and the entry
432 * will show no new values if the data type does not match. To determine
433 * if the data type matches, use the appropriate Topic functions.
434 *
435 * @param defaultValue default value used when a default is not provided to a
436 * getter function
437 * @param options publish and/or subscribe options
438 * @return entry
439 */
440 [[nodiscard]]
442 const PubSubOptions& options = kDefaultPubSubOptions) {
443 return StringEntry{
444 ::nt::GetEntry(m_handle, NT_STRING, "string", options),
445 defaultValue};
446 }
447 /**
448 * Create a new entry for the topic, with specific type string.
449 *
450 * Entries act as a combination of a subscriber and a weak publisher. The
451 * subscriber is active as long as the entry is not destroyed. The publisher
452 * is created when the entry is first written to, and remains active until
453 * either Unpublish() is called or the entry is destroyed.
454 *
455 * @note It is not possible to use two different data types with the same
456 * topic. Conflicts between publishers are typically resolved by the
457 * server on a first-come, first-served basis. Any published values that
458 * do not match the topic's data type are dropped (ignored), and the entry
459 * will show no new values if the data type does not match. To determine
460 * if the data type matches, use the appropriate Topic functions.
461 *
462 * @param typeString type string
463 * @param defaultValue default value used when a default is not provided to a
464 * getter function
465 * @param options publish and/or subscribe options
466 * @return entry
467 */
468 [[nodiscard]]
469 EntryType GetEntryEx(std::string_view typeString, ParamType defaultValue,
470 const PubSubOptions& options = kDefaultPubSubOptions) {
471 return StringEntry{
472 ::nt::GetEntry(m_handle, NT_STRING, typeString, options),
473 defaultValue};
474 }
475
476};
477
481
485
489
490} // namespace nt
NetworkTables publisher.
Definition Topic.h:393
NT_Publisher m_pubHandle
NetworkTables handle.
Definition Topic.h:441
NetworkTables String entry.
Definition StringTopic.h:240
void Unpublish()
Stops publishing the entry if it's published.
Definition StringTopic.h:290
TopicType GetTopic() const
Get the corresponding topic.
Definition StringTopic.h:486
StringEntry(NT_Entry handle, ParamType defaultValue)
Construct from an entry handle; recommended to use StringTopic::GetEntry() instead.
Definition StringTopic.h:262
std::string ValueType
Definition StringTopic.h:245
char SmallElemType
Definition StringTopic.h:249
StringTopic TopicType
Definition StringTopic.h:244
NT_Entry GetHandle() const
Gets the native handle for the entry.
Definition StringTopic.h:278
std::string_view ParamType
Definition StringTopic.h:246
std::string_view SmallRetType
Definition StringTopic.h:248
StringEntry()=default
NetworkTables String publisher.
Definition StringTopic.h:184
void SetDefault(ParamType value)
Publish a default value.
Definition StringTopic.h:222
std::string_view SmallRetType
Definition StringTopic.h:190
StringPublisher()=default
StringTopic TopicType
Definition StringTopic.h:186
std::string_view ParamType
Definition StringTopic.h:188
StringPublisher(NT_Publisher handle)
Construct from a publisher handle; recommended to use StringTopic::Publish() instead.
Definition StringTopic.h:203
TopicType GetTopic() const
Get the corresponding topic.
Definition StringTopic.h:482
std::string ValueType
Definition StringTopic.h:187
void Set(ParamType value, int64_t time=0)
Publish a new value.
Definition StringTopic.h:211
char SmallElemType
Definition StringTopic.h:191
NetworkTables String subscriber.
Definition StringTopic.h:36
ValueType Get(ParamType defaultValue) const
Get the last published value.
Definition StringTopic.h:78
StringSubscriber()=default
TimestampedValueViewType GetAtomic(wpi::SmallVectorImpl< SmallElemType > &buf) const
Get the last published value along with its timestamp.
Definition StringTopic.h:136
SmallRetType Get(wpi::SmallVectorImpl< SmallElemType > &buf) const
Get the last published value.
Definition StringTopic.h:89
StringTopic TopicType
Definition StringTopic.h:38
TimestampedValueType GetAtomic(ParamType defaultValue) const
Get the last published value along with its timestamp.
Definition StringTopic.h:124
std::vector< TimestampedValueType > ReadQueue()
Get an array of all value changes since the last call to ReadQueue.
Definition StringTopic.h:166
StringSubscriber(NT_Subscriber handle, ParamType defaultValue)
Construct from a subscriber handle; recommended to use StringTopic::Subscribe() instead.
Definition StringTopic.h:57
char SmallElemType
Definition StringTopic.h:44
TopicType GetTopic() const
Get the corresponding topic.
Definition StringTopic.h:478
std::string_view ParamType
Definition StringTopic.h:40
std::string ValueType
Definition StringTopic.h:39
SmallRetType Get(wpi::SmallVectorImpl< SmallElemType > &buf, ParamType defaultValue) const
Get the last published value.
Definition StringTopic.h:101
TimestampedValueType GetAtomic() const
Get the last published value along with its timestamp If no value has been published,...
Definition StringTopic.h:112
TimestampedValueViewType GetAtomic(wpi::SmallVectorImpl< SmallElemType > &buf, ParamType defaultValue) const
Get the last published value along with its timestamp.
Definition StringTopic.h:150
ValueType Get() const
Get the last published value.
Definition StringTopic.h:67
std::string_view SmallRetType
Definition StringTopic.h:43
NetworkTables String topic.
Definition StringTopic.h:298
StringTopic(Topic topic)
Construct from a generic topic.
Definition StringTopic.h:324
PublisherType Publish(const PubSubOptions &options=kDefaultPubSubOptions)
Create a new publisher to the topic.
Definition StringTopic.h:390
StringTopic(NT_Topic handle)
Construct from a topic handle; recommended to use NetworkTableInstance::GetStringTopic() instead.
Definition StringTopic.h:317
StringTopic()=default
static constexpr std::string_view kTypeString
The default type string for this topic type.
Definition StringTopic.h:307
SubscriberType SubscribeEx(std::string_view typeString, ParamType defaultValue, const PubSubOptions &options=kDefaultPubSubOptions)
Create a new subscriber to the topic, with specific type string.
Definition StringTopic.h:366
SubscriberType Subscribe(ParamType defaultValue, const PubSubOptions &options=kDefaultPubSubOptions)
Create a new subscriber to the topic.
Definition StringTopic.h:342
std::string_view ParamType
Definition StringTopic.h:304
EntryType GetEntryEx(std::string_view typeString, ParamType defaultValue, const PubSubOptions &options=kDefaultPubSubOptions)
Create a new entry for the topic, with specific type string.
Definition StringTopic.h:469
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 StringTopic.h:414
std::string ValueType
Definition StringTopic.h:303
EntryType GetEntry(ParamType defaultValue, const PubSubOptions &options=kDefaultPubSubOptions)
Create a new entry for the topic.
Definition StringTopic.h:441
NetworkTables subscriber.
Definition Topic.h:321
NT_Subscriber m_subHandle
Definition Topic.h:386
NetworkTables Topic.
Definition Topic.h:28
NT_Topic m_handle
Definition Topic.h:317
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
Definition sha1.h:30
std::string GetString(NT_Handle subentry, std::string_view defaultValue)
Get the last published value.
bool SetDefaultString(NT_Handle pubentry, std::string_view defaultValue)
Publish a default value.
bool SetString(NT_Handle pubentry, std::string_view value, int64_t time=0)
Publish a new value.
TimestampedString GetAtomicString(NT_Handle subentry, std::string_view defaultValue)
Get the last published value along with its timestamp.
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_STRING
Definition ntcore_c.h:57
Timestamped< std::string_view > TimestampedStringView
Timestamped String view (for SmallVector-taking functions).
Definition ntcore_cpp_types.h:377
Timestamped< std::string > TimestampedString
Timestamped String.
Definition ntcore_cpp_types.h:371
constexpr PubSubOptions kDefaultPubSubOptions
Default publish/subscribe options.
Definition ntcore_cpp.h:390
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_Publisher PublishEx(NT_Topic topic, NT_Type type, std::string_view typeStr, const wpi::json &properties, const PubSubOptions &options=kDefaultPubSubOptions)
Creates a new publisher to a topic.
NT_Publisher Publish(NT_Topic topic, NT_Type type, std::string_view typeStr, const PubSubOptions &options=kDefaultPubSubOptions)
Creates a new publisher to a topic.
void Unpublish(NT_Handle pubentry)
Stops publisher.
NT_Subscriber Subscribe(NT_Topic topic, NT_Type type, std::string_view typeStr, const PubSubOptions &options=kDefaultPubSubOptions)
Creates a new subscriber to value changes on a topic.
NetworkTables (ntcore) namespace.
Definition ntcore_cpp.h:36
Foonathan namespace.
Definition ntcore_cpp.h:26
NetworkTables publish/subscribe options.
Definition ntcore_cpp.h:305
Timestamped value.
Definition ntcore_cpp_types.h:30