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