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