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