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