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