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