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