WPILibC++ 2023.4.3
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 */
173 const PubSubOptions& options = kDefaultPubSubOptions);
174
175 /**
176 * Create a new subscriber to the topic.
177 *
178 * <p>The subscriber is only active as long as the returned object
179 * is not destroyed.
180 *
181 * @note Subscribers that do not match the published data type do not return
182 * any values. To determine if the data type matches, use the appropriate
183 * Topic functions.
184 *
185 * @param typeString type string
186 * @param options subscribe options
187 * @return subscriber
188 */
190 std::string_view typeString,
191 const PubSubOptions& options = kDefaultPubSubOptions);
192
193 /**
194 * Create a new publisher to the topic.
195 *
196 * The publisher is only active as long as the returned object
197 * is not destroyed.
198 *
199 * @note It is not possible to publish two different data types to the same
200 * topic. Conflicts between publishers are typically resolved by the
201 * server on a first-come, first-served basis. Any published values that
202 * do not match the topic's data type are dropped (ignored). To determine
203 * if the data type matches, use the appropriate Topic functions.
204 *
205 * @param typeString type string
206 * @param options publish options
207 * @return publisher
208 */
210 std::string_view typeString,
211 const PubSubOptions& options = kDefaultPubSubOptions);
212
213 /**
214 * Create a new publisher to the topic, with type string and initial
215 * properties.
216 *
217 * The publisher is only active as long as the returned object
218 * is not destroyed.
219 *
220 * @note It is not possible to publish two different data types to the same
221 * topic. Conflicts between publishers are typically resolved by the
222 * server on a first-come, first-served basis. Any published values that
223 * do not match the topic's data type are dropped (ignored). To determine
224 * if the data type matches, use the appropriate Topic functions.
225 *
226 * @param typeString type string
227 * @param properties JSON properties
228 * @param options publish options
229 * @return publisher
230 */
232 std::string_view typeString, const wpi::json& properties,
233 const PubSubOptions& options = kDefaultPubSubOptions);
234
235 /**
236 * Create a new generic entry for the topic.
237 *
238 * Entries act as a combination of a subscriber and a weak publisher. The
239 * subscriber is active as long as the entry is not destroyed. The publisher
240 * is created when the entry is first written to, and remains active until
241 * either Unpublish() is called or the entry is destroyed.
242 *
243 * @note It is not possible to use two different data types with the same
244 * topic. Conflicts between publishers are typically resolved by the
245 * server on a first-come, first-served basis. Any published values that
246 * do not match the topic's data type are dropped (ignored), and the entry
247 * will show no new values if the data type does not match. To determine
248 * if the data type matches, use the appropriate Topic functions.
249 *
250 * @param options publish and/or subscribe options
251 * @return entry
252 */
254 const PubSubOptions& options = kDefaultPubSubOptions);
255
256 /**
257 * Create a new generic entry for the topic.
258 *
259 * Entries act as a combination of a subscriber and a weak publisher. The
260 * subscriber is active as long as the entry is not destroyed. The publisher
261 * is created when the entry is first written to, and remains active until
262 * either Unpublish() is called or the entry is destroyed.
263 *
264 * @note It is not possible to use two different data types with the same
265 * topic. Conflicts between publishers are typically resolved by the
266 * server on a first-come, first-served basis. Any published values that
267 * do not match the topic's data type are dropped (ignored), and the entry
268 * will show no new values if the data type does not match. To determine
269 * if the data type matches, use the appropriate Topic functions.
270 *
271 * @param typeString type string
272 * @param options publish and/or subscribe options
273 * @return entry
274 */
276 std::string_view typeString,
277 const PubSubOptions& options = kDefaultPubSubOptions);
278
279 /**
280 * Equality operator. Returns true if both instances refer to the same
281 * native handle.
282 */
283 bool operator==(const Topic&) const = default;
284
285 protected:
287};
288
289/** NetworkTables subscriber. */
291 public:
292 virtual ~Subscriber();
293
294 Subscriber(const Subscriber&) = delete;
295 Subscriber& operator=(const Subscriber&) = delete;
296
299
300 /**
301 * Determines if the native handle is valid.
302 *
303 * @return True if the native handle is valid, false otherwise.
304 */
305 explicit operator bool() const { return m_subHandle != 0; }
306
307 /**
308 * Gets the native handle for the subscriber.
309 *
310 * @return Native handle
311 */
313
314 /**
315 * Determines if the topic is currently being published.
316 *
317 * @return True if the topic exists, false otherwise.
318 */
319 bool Exists() const;
320
321 /**
322 * Gets the last time the value was changed.
323 * Note: this is not atomic with Get(); use GetAtomic() to get
324 * both the value and last change as an atomic operation.
325 *
326 * @return Topic last change time
327 */
328 int64_t GetLastChange() const;
329
330 /**
331 * Gets the subscribed-to topic.
332 *
333 * @return Topic
334 */
335 Topic GetTopic() const;
336
337 protected:
338 Subscriber() = default;
339 explicit Subscriber(NT_Subscriber handle) : m_subHandle{handle} {}
340
342};
343
344/** NetworkTables publisher. */
346 public:
347 virtual ~Publisher();
348
349 Publisher(const Publisher&) = delete;
350 Publisher& operator=(const Publisher&) = delete;
351
354
355 /**
356 * Determines if the native handle is valid.
357 *
358 * @return True if the native handle is valid, false otherwise.
359 */
360 explicit operator bool() const { return m_pubHandle != 0; }
361
362 /**
363 * Gets the native handle for the publisher.
364 *
365 * @return Native handle
366 */
368
369 /**
370 * Gets the published-to topic.
371 *
372 * @return Topic
373 */
374 Topic GetTopic() const;
375
376 protected:
377 Publisher() = default;
378 explicit Publisher(NT_Publisher handle) : m_pubHandle{handle} {}
379
381};
382
383} // namespace nt
384
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:345
Publisher(const Publisher &)=delete
Publisher(NT_Publisher handle)
Definition: Topic.h:378
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:380
NT_Publisher GetHandle() const
Gets the native handle for the publisher.
Definition: Topic.h:367
NetworkTables subscriber.
Definition: Topic.h:290
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:312
virtual ~Subscriber()
Definition: Topic.inc:65
Subscriber & operator=(const Subscriber &)=delete
NT_Subscriber m_subHandle
Definition: Topic.h:341
Subscriber(NT_Subscriber handle)
Definition: Topic.h:339
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:286
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: ntcore_cpp.h:35
/file This file defines the SmallVector class.
Definition: AprilTagFieldLayout.h:18
NetworkTables publish/subscribe options.
Definition: ntcore_cpp.h:304
NetworkTables Topic Information.
Definition: ntcore_cpp.h:89