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