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