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