WPILibC++ 2025.2.1
Loading...
Searching...
No Matches
DoubleTopic.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
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 DoubleTopic;
30
31/**
32 * NetworkTables Double subscriber.
33 */
35 public:
37 using ValueType = double;
38 using ParamType = double;
40
41
42 DoubleSubscriber() = default;
43
44 /**
45 * Construct from a subscriber handle; recommended to use
46 * DoubleTopic::Subscribe() instead.
47 *
48 * @param handle Native handle
49 * @param defaultValue Default value
50 */
52 : Subscriber{handle},
53 m_defaultValue{defaultValue} {}
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::GetDouble(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::GetAtomicDouble(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::ReadQueueDouble(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 Double publisher.
126 */
128 public:
130 using ValueType = double;
131 using ParamType = double;
132
134
135 DoublePublisher() = default;
136
137 /**
138 * Construct from a publisher handle; recommended to use
139 * DoubleTopic::Publish() instead.
140 *
141 * @param handle Native handle
142 */
143 explicit DoublePublisher(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::SetDouble(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 */
162 void SetDefault(ParamType value) {
164 }
165
166 /**
167 * Get the corresponding topic.
168 *
169 * @return Topic
170 */
171 TopicType GetTopic() const;
172};
173
174/**
175 * NetworkTables Double entry.
176 *
177 * @note Unlike NetworkTableEntry, the entry goes away when this is destroyed.
178 */
179class DoubleEntry final : public DoubleSubscriber,
180 public DoublePublisher {
181 public:
185 using ValueType = double;
186 using ParamType = double;
187
189
190 DoubleEntry() = default;
191
192 /**
193 * Construct from an entry handle; recommended to use
194 * DoubleTopic::GetEntry() instead.
195 *
196 * @param handle Native handle
197 * @param defaultValue Default value
198 */
199 DoubleEntry(NT_Entry handle, ParamType defaultValue)
200 : DoubleSubscriber{handle, defaultValue},
201 DoublePublisher{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 Double topic.
234 */
235class DoubleTopic final : public Topic {
236 public:
240 using ValueType = double;
241 using ParamType = double;
243 /** The default type string for this topic type. */
244 static constexpr std::string_view kTypeString = "double";
245
246 DoubleTopic() = default;
247
248 /**
249 * Construct from a topic handle; recommended to use
250 * NetworkTableInstance::GetDoubleTopic() instead.
251 *
252 * @param handle Native handle
253 */
254 explicit DoubleTopic(NT_Topic handle) : Topic{handle} {}
255
256 /**
257 * Construct from a generic topic.
258 *
259 * @param topic Topic
260 */
261 explicit DoubleTopic(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) {
282 return DoubleSubscriber{
283 ::nt::Subscribe(m_handle, NT_DOUBLE, "double", 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) {
306 return DoubleSubscriber{
307 ::nt::Subscribe(m_handle, NT_DOUBLE, 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]]
328 return DoublePublisher{
329 ::nt::Publish(m_handle, NT_DOUBLE, "double", 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) {
353 return DoublePublisher{
354 ::nt::PublishEx(m_handle, NT_DOUBLE, 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 DoubleEntry{
381 ::nt::GetEntry(m_handle, NT_DOUBLE, "double", 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 DoubleEntry{
409 ::nt::GetEntry(m_handle, NT_DOUBLE, typeString, options),
410 defaultValue};
411 }
412
413};
414
418
422
426
427} // namespace nt
NetworkTables Double entry.
Definition DoubleTopic.h:180
double ValueType
Definition DoubleTopic.h:185
DoubleTopic TopicType
Definition DoubleTopic.h:184
DoubleEntry(NT_Entry handle, ParamType defaultValue)
Construct from an entry handle; recommended to use DoubleTopic::GetEntry() instead.
Definition DoubleTopic.h:199
TopicType GetTopic() const
Get the corresponding topic.
Definition DoubleTopic.h:423
double ParamType
Definition DoubleTopic.h:186
void Unpublish()
Stops publishing the entry if it's published.
Definition DoubleTopic.h:227
NT_Entry GetHandle() const
Gets the native handle for the entry.
Definition DoubleTopic.h:215
DoubleEntry()=default
NetworkTables Double publisher.
Definition DoubleTopic.h:127
DoublePublisher()=default
TopicType GetTopic() const
Get the corresponding topic.
Definition DoubleTopic.h:419
DoublePublisher(NT_Publisher handle)
Construct from a publisher handle; recommended to use DoubleTopic::Publish() instead.
Definition DoubleTopic.h:143
double ParamType
Definition DoubleTopic.h:131
double ValueType
Definition DoubleTopic.h:130
void SetDefault(ParamType value)
Publish a default value.
Definition DoubleTopic.h:162
DoubleTopic TopicType
Definition DoubleTopic.h:129
void Set(ParamType value, int64_t time=0)
Publish a new value.
Definition DoubleTopic.h:151
NetworkTables Double subscriber.
Definition DoubleTopic.h:34
ValueType Get(ParamType defaultValue) const
Get the last published value.
Definition DoubleTopic.h:72
DoubleSubscriber()=default
DoubleTopic TopicType
Definition DoubleTopic.h:36
TopicType GetTopic() const
Get the corresponding topic.
Definition DoubleTopic.h:415
std::vector< TimestampedValueType > ReadQueue()
Get an array of all value changes since the last call to ReadQueue.
Definition DoubleTopic.h:109
double ParamType
Definition DoubleTopic.h:38
DoubleSubscriber(NT_Subscriber handle, ParamType defaultValue)
Construct from a subscriber handle; recommended to use DoubleTopic::Subscribe() instead.
Definition DoubleTopic.h:51
TimestampedValueType GetAtomic() const
Get the last published value along with its timestamp If no value has been published,...
Definition DoubleTopic.h:83
double ValueType
Definition DoubleTopic.h:37
ValueType Get() const
Get the last published value.
Definition DoubleTopic.h:61
TimestampedValueType GetAtomic(ParamType defaultValue) const
Get the last published value along with its timestamp.
Definition DoubleTopic.h:95
NetworkTables Double topic.
Definition DoubleTopic.h:235
double ValueType
Definition DoubleTopic.h:240
EntryType GetEntry(ParamType defaultValue, const PubSubOptions &options=kDefaultPubSubOptions)
Create a new entry for the topic.
Definition DoubleTopic.h:378
EntryType GetEntryEx(std::string_view typeString, ParamType defaultValue, const PubSubOptions &options=kDefaultPubSubOptions)
Create a new entry for the topic, with specific type string.
Definition DoubleTopic.h:406
SubscriberType SubscribeEx(std::string_view typeString, ParamType defaultValue, const PubSubOptions &options=kDefaultPubSubOptions)
Create a new subscriber to the topic, with specific type string.
Definition DoubleTopic.h:303
DoubleTopic(NT_Topic handle)
Construct from a topic handle; recommended to use NetworkTableInstance::GetDoubleTopic() instead.
Definition DoubleTopic.h:254
DoubleTopic(Topic topic)
Construct from a generic topic.
Definition DoubleTopic.h:261
double ParamType
Definition DoubleTopic.h:241
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 DoubleTopic.h:351
static constexpr std::string_view kTypeString
The default type string for this topic type.
Definition DoubleTopic.h:244
SubscriberType Subscribe(ParamType defaultValue, const PubSubOptions &options=kDefaultPubSubOptions)
Create a new subscriber to the topic.
Definition DoubleTopic.h:279
DoubleTopic()=default
PublisherType Publish(const PubSubOptions &options=kDefaultPubSubOptions)
Create a new publisher to the topic.
Definition DoubleTopic.h:327
NetworkTables publisher.
Definition Topic.h:393
NT_Publisher m_pubHandle
NetworkTables handle.
Definition Topic.h:441
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 SetDefaultDouble(NT_Handle pubentry, double defaultValue)
Publish a default value.
bool SetDouble(NT_Handle pubentry, double 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_DOUBLE
Definition ntcore_c.h:56
Timestamped< double > TimestampedDouble
Timestamped Double.
Definition ntcore_cpp_types.h:292
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