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