WPILibC++ 2023.4.3-108-ge5452e3
Topic.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#include <string>
10#include <string_view>
11#include <utility>
12#include <vector>
13
15#include "ntcore_c.h"
16#include "ntcore_cpp.h"
17
18namespace wpi {
19class json;
20} // namespace wpi
21
22namespace nt {
23
24class GenericEntry;
25class GenericPublisher;
26class GenericSubscriber;
27class NetworkTableInstance;
28
29/** NetworkTables Topic. */
30class Topic {
31 public:
32 Topic() = default;
33 explicit Topic(NT_Topic handle) : m_handle{handle} {}
34
35 /**
36 * Determines if the native handle is valid.
37 *
38 * @return True if the native handle is valid, false otherwise.
39 */
40 explicit operator bool() const { return m_handle != 0; }
41
42 /**
43 * Gets the native handle for the topic.
44 *
45 * @return Native handle
46 */
47 NT_Topic GetHandle() const { return m_handle; }
48
49 /**
50 * Gets the instance for the topic.
51 *
52 * @return Instance
53 */
55
56 /**
57 * Gets the name of the topic.
58 *
59 * @return the topic's name
60 */
61 std::string GetName() const;
62
63 /**
64 * Gets the type of the topic.
65 *
66 * @return the topic's type
67 */
69
70 /**
71 * Gets the type string of the topic. This may have more information
72 * than the numeric type (especially for raw values).
73 *
74 * @return the topic's type
75 */
76 std::string GetTypeString() const;
77
78 /**
79 * Make value persistent through server restarts.
80 *
81 * @param persistent True for persistent, false for not persistent.
82 */
83 void SetPersistent(bool persistent);
84
85 /**
86 * Returns whether the value is persistent through server restarts.
87 *
88 * @return True if the value is persistent.
89 */
90 bool IsPersistent() const;
91
92 /**
93 * Make the server retain the topic even when there are no publishers.
94 *
95 * @param retained True for retained, false for not retained.
96 */
97 void SetRetained(bool retained);
98
99 /**
100 * Returns whether the topic is retained by server when there are no
101 * publishers.
102 *
103 * @return True if the topic is retained.
104 */
105 bool IsRetained() const;
106
107 /**
108 * Determines if the topic is currently being published.
109 *
110 * @return True if the topic exists, false otherwise.
111 */
112 bool Exists() const;
113
114 /**
115 * Gets the current value of a property (as a JSON object).
116 *
117 * @param name property name
118 * @return JSON object; null object if the property does not exist.
119 */
121
122 /**
123 * Sets a property value.
124 *
125 * @param name property name
126 * @param value property value
127 */
129
130 /**
131 * Deletes a property. Has no effect if the property does not exist.
132 *
133 * @param name property name
134 */
136
137 /**
138 * Gets all topic properties as a JSON object. Each key in the object
139 * is the property name, and the corresponding value is the property value.
140 *
141 * @return JSON object
142 */
144
145 /**
146 * Updates multiple topic properties. Each key in the passed-in object is
147 * the name of the property to add/update, and the corresponding value is the
148 * property value to set for that property. Null values result in deletion
149 * of the corresponding property.
150 *
151 * @param properties JSON object with keys to add/update/delete
152 * @return False if properties is not an object
153 */
154 bool SetProperties(const wpi::json& properties);
155
156 /**
157 * Gets combined information about the topic.
158 *
159 * @return Topic information
160 */
161 TopicInfo GetInfo() const;
162
163 /**
164 * Create a new subscriber to the topic.
165 *
166 * <p>The subscriber is only active as long as the returned object
167 * is not destroyed.
168 *
169 * @param options subscribe options
170 * @return subscriber
171 */
172 [[nodiscard]]
174 const PubSubOptions& options = kDefaultPubSubOptions);
175
176 /**
177 * Create a new subscriber to the topic.
178 *
179 * <p>The subscriber is only active as long as the returned object
180 * is not destroyed.
181 *
182 * @note Subscribers that do not match the published data type do not return
183 * any values. To determine if the data type matches, use the appropriate
184 * Topic functions.
185 *
186 * @param typeString type string
187 * @param options subscribe options
188 * @return subscriber
189 */
190 [[nodiscard]]
192 std::string_view typeString,
193 const PubSubOptions& options = kDefaultPubSubOptions);
194
195 /**
196 * Create a new publisher to the topic.
197 *
198 * The publisher is only active as long as the returned object
199 * is not destroyed.
200 *
201 * @note It is not possible to publish two different data types to the same
202 * topic. Conflicts between publishers are typically resolved by the
203 * server on a first-come, first-served basis. Any published values that
204 * do not match the topic's data type are dropped (ignored). To determine
205 * if the data type matches, use the appropriate Topic functions.
206 *
207 * @param typeString type string
208 * @param options publish options
209 * @return publisher
210 */
211 [[nodiscard]]
213 std::string_view typeString,
214 const PubSubOptions& options = kDefaultPubSubOptions);
215
216 /**
217 * Create a new publisher to the topic, with type string and initial
218 * properties.
219 *
220 * The publisher is only active as long as the returned object
221 * is not destroyed.
222 *
223 * @note It is not possible to publish two different data types to the same
224 * topic. Conflicts between publishers are typically resolved by the
225 * server on a first-come, first-served basis. Any published values that
226 * do not match the topic's data type are dropped (ignored). To determine
227 * if the data type matches, use the appropriate Topic functions.
228 *
229 * @param typeString type string
230 * @param properties JSON properties
231 * @param options publish options
232 * @return publisher
233 */
234 [[nodiscard]]
236 std::string_view typeString, const wpi::json& properties,
237 const PubSubOptions& options = kDefaultPubSubOptions);
238
239 /**
240 * Create a new generic entry for the topic.
241 *
242 * Entries act as a combination of a subscriber and a weak publisher. The
243 * subscriber is active as long as the entry is not destroyed. The publisher
244 * is created when the entry is first written to, and remains active until
245 * either Unpublish() is called or the entry is destroyed.
246 *
247 * @note It is not possible to use two different data types with the same
248 * topic. Conflicts between publishers are typically resolved by the
249 * server on a first-come, first-served basis. Any published values that
250 * do not match the topic's data type are dropped (ignored), and the entry
251 * will show no new values if the data type does not match. To determine
252 * if the data type matches, use the appropriate Topic functions.
253 *
254 * @param options publish and/or subscribe options
255 * @return entry
256 */
257 [[nodiscard]]
259 const PubSubOptions& options = kDefaultPubSubOptions);
260
261 /**
262 * Create a new generic entry for the topic.
263 *
264 * Entries act as a combination of a subscriber and a weak publisher. The
265 * subscriber is active as long as the entry is not destroyed. The publisher
266 * is created when the entry is first written to, and remains active until
267 * either Unpublish() is called or the entry is destroyed.
268 *
269 * @note It is not possible to use two different data types with the same
270 * topic. Conflicts between publishers are typically resolved by the
271 * server on a first-come, first-served basis. Any published values that
272 * do not match the topic's data type are dropped (ignored), and the entry
273 * will show no new values if the data type does not match. To determine
274 * if the data type matches, use the appropriate Topic functions.
275 *
276 * @param typeString type string
277 * @param options publish and/or subscribe options
278 * @return entry
279 */
280 [[nodiscard]]
282 std::string_view typeString,
283 const PubSubOptions& options = kDefaultPubSubOptions);
284
285 /**
286 * Equality operator. Returns true if both instances refer to the same
287 * native handle.
288 */
289 bool operator==(const Topic&) const = default;
290
291 protected:
293};
294
295/** NetworkTables subscriber. */
297 public:
298 virtual ~Subscriber();
299
300 Subscriber(const Subscriber&) = delete;
301 Subscriber& operator=(const Subscriber&) = delete;
302
305
306 /**
307 * Determines if the native handle is valid.
308 *
309 * @return True if the native handle is valid, false otherwise.
310 */
311 explicit operator bool() const { return m_subHandle != 0; }
312
313 /**
314 * Gets the native handle for the subscriber.
315 *
316 * @return Native handle
317 */
319
320 /**
321 * Determines if the topic is currently being published.
322 *
323 * @return True if the topic exists, false otherwise.
324 */
325 bool Exists() const;
326
327 /**
328 * Gets the last time the value was changed.
329 * Note: this is not atomic with Get(); use GetAtomic() to get
330 * both the value and last change as an atomic operation.
331 *
332 * @return Topic last change time
333 */
334 int64_t GetLastChange() const;
335
336 /**
337 * Gets the subscribed-to topic.
338 *
339 * @return Topic
340 */
341 Topic GetTopic() const;
342
343 protected:
344 Subscriber() = default;
345 explicit Subscriber(NT_Subscriber handle) : m_subHandle{handle} {}
346
348};
349
350/** NetworkTables publisher. */
352 public:
353 virtual ~Publisher();
354
355 Publisher(const Publisher&) = delete;
356 Publisher& operator=(const Publisher&) = delete;
357
360
361 /**
362 * Determines if the native handle is valid.
363 *
364 * @return True if the native handle is valid, false otherwise.
365 */
366 explicit operator bool() const { return m_pubHandle != 0; }
367
368 /**
369 * Gets the native handle for the publisher.
370 *
371 * @return Native handle
372 */
374
375 /**
376 * Gets the published-to topic.
377 *
378 * @return Topic
379 */
380 Topic GetTopic() const;
381
382 protected:
383 Publisher() = default;
384 explicit Publisher(NT_Publisher handle) : m_pubHandle{handle} {}
385
387};
388
389} // namespace nt
390
NetworkTables generic entry.
Definition: GenericEntry.h:435
NetworkTables generic publisher.
Definition: GenericEntry.h:193
NetworkTables generic subscriber.
Definition: GenericEntry.h:24
NetworkTables Instance.
Definition: NetworkTableInstance.h:59
NetworkTables publisher.
Definition: Topic.h:351
Publisher(const Publisher &)=delete
Publisher(NT_Publisher handle)
Definition: Topic.h:384
Topic GetTopic() const
Gets the published-to topic.
Definition: Topic.inc:111
Publisher & operator=(const Publisher &)=delete
virtual ~Publisher()
Definition: Topic.inc:94
Publisher()=default
NT_Publisher m_pubHandle
Definition: Topic.h:386
NT_Publisher GetHandle() const
Gets the native handle for the publisher.
Definition: Topic.h:373
NetworkTables subscriber.
Definition: Topic.h:296
bool Exists() const
Determines if the topic is currently being published.
Definition: Topic.inc:82
int64_t GetLastChange() const
Gets the last time the value was changed.
Definition: Topic.inc:86
NT_Subscriber GetHandle() const
Gets the native handle for the subscriber.
Definition: Topic.h:318
virtual ~Subscriber()
Definition: Topic.inc:65
Subscriber & operator=(const Subscriber &)=delete
NT_Subscriber m_subHandle
Definition: Topic.h:347
Subscriber(NT_Subscriber handle)
Definition: Topic.h:345
Topic GetTopic() const
Gets the subscribed-to topic.
Definition: Topic.inc:90
Subscriber()=default
Subscriber(const Subscriber &)=delete
NetworkTables Topic.
Definition: Topic.h:30
GenericPublisher GenericPublishEx(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.
bool IsPersistent() const
Returns whether the value is persistent through server restarts.
Definition: Topic.inc:37
bool Exists() const
Determines if the topic is currently being published.
Definition: Topic.inc:49
Topic(NT_Topic handle)
Definition: Topic.h:33
bool SetProperties(const wpi::json &properties)
Updates multiple topic properties.
Definition: Topic.inc:57
void DeleteProperty(std::string_view name)
Deletes a property.
Definition: Topic.inc:53
Topic()=default
void SetRetained(bool retained)
Make the server retain the topic even when there are no publishers.
Definition: Topic.inc:41
std::string GetTypeString() const
Gets the type string of the topic.
Definition: Topic.inc:29
GenericEntry GetGenericEntry(std::string_view typeString, const PubSubOptions &options=kDefaultPubSubOptions)
Create a new generic entry for the topic.
GenericEntry GetGenericEntry(const PubSubOptions &options=kDefaultPubSubOptions)
Create a new generic entry for the topic.
NT_Topic GetHandle() const
Gets the native handle for the topic.
Definition: Topic.h:47
NetworkTableType GetType() const
Gets the type of the topic.
Definition: Topic.inc:25
std::string GetName() const
Gets the name of the topic.
Definition: Topic.inc:21
bool IsRetained() const
Returns whether the topic is retained by server when there are no publishers.
Definition: Topic.inc:45
NT_Topic m_handle
Definition: Topic.h:292
bool operator==(const Topic &) const =default
Equality operator.
void SetPersistent(bool persistent)
Make value persistent through server restarts.
Definition: Topic.inc:33
wpi::json GetProperties() const
Gets all topic properties as a JSON object.
GenericPublisher GenericPublish(std::string_view typeString, const PubSubOptions &options=kDefaultPubSubOptions)
Create a new publisher to the topic.
wpi::json GetProperty(std::string_view name) const
Gets the current value of a property (as a JSON object).
NetworkTableInstance GetInstance() const
Gets the instance for the topic.
Definition: Topic.inc:17
TopicInfo GetInfo() const
Gets combined information about the topic.
Definition: Topic.inc:61
void SetProperty(std::string_view name, const wpi::json &value)
Sets a property value.
GenericSubscriber GenericSubscribe(std::string_view typeString, const PubSubOptions &options=kDefaultPubSubOptions)
Create a new subscriber to the topic.
GenericSubscriber GenericSubscribe(const PubSubOptions &options=kDefaultPubSubOptions)
Create a new subscriber to the topic.
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
NetworkTableType
NetworkTable entry type.
Definition: NetworkTableType.h:15
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
NetworkTables Topic Information.
Definition: ntcore_cpp.h:89