robot-rust 0.2.0

The robot-rust project, bindings for wpilib, ctre, and rev in rust.
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
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
// 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 <cassert>
#include <concepts>
#include <initializer_list>
#include <memory>
#include <span>
#include <string>
#include <string_view>
#include <utility>
#include <vector>

#include "ntcore_c.h"

namespace nt {

// Forward declare here to avoid circular dependency on ntcore_cpp.h
int64_t Now();

#if __GNUC__ >= 13
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wmaybe-uninitialized"
#endif

/**
 * A network table entry value.
 * @ingroup ntcore_cpp_api
 */
class Value final {
  struct private_init {};

 public:
  Value() {
    m_val.type = NT_UNASSIGNED;
    m_val.last_change = 0;
    m_val.server_time = 0;
  }

  Value(NT_Type type, size_t size, int64_t time, const private_init&)
      : Value{type, size, time == 0 ? Now() : time, 1, private_init{}} {}

  Value(NT_Type type, size_t size, int64_t time, int64_t serverTime,
        const private_init&)
      : m_size{size} {
    m_val.type = type;
    m_val.last_change = time;
    m_val.server_time = serverTime;
    switch (type) {
      case NT_BOOLEAN_ARRAY:
        m_val.data.arr_boolean.arr = nullptr;
        break;
      case NT_INTEGER_ARRAY:
        m_val.data.arr_int.arr = nullptr;
        break;
      case NT_FLOAT_ARRAY:
        m_val.data.arr_float.arr = nullptr;
        break;
      case NT_DOUBLE_ARRAY:
        m_val.data.arr_double.arr = nullptr;
        break;
      case NT_STRING_ARRAY:
        m_val.data.arr_string.arr = nullptr;
        break;
      default:
        break;
    }
  }

  explicit operator bool() const { return m_val.type != NT_UNASSIGNED; }

  /**
   * Get the data type.
   *
   * @return The type.
   */
  NT_Type type() const { return m_val.type; }

  /**
   * Get the data value stored.
   *
   * @return The type.
   */
  const NT_Value& value() const { return m_val; }

  /**
   * Get the creation time of the value, in local time.
   *
   * @return The time, in the units returned by nt::Now().
   */
  int64_t last_change() const { return m_val.last_change; }

  /**
   * Get the creation time of the value, in local time.
   *
   * @return The time, in the units returned by nt::Now().
   */
  int64_t time() const { return m_val.last_change; }

  /**
   * Get the approximate in-memory size of the value in bytes. This is zero for
   * values that do not require additional memory beyond the memory of the Value
   * itself.
   *
   * @return The size in bytes.
   */
  size_t size() const { return m_size; }

  /**
   * Set the local creation time of the value.
   *
   * @param time The time.
   */
  void SetTime(int64_t time) { m_val.last_change = time; }

  /**
   * Get the creation time of the value, in server time.
   *
   * @return The server time.
   */
  int64_t server_time() const { return m_val.server_time; }

  /**
   * Set the creation time of the value, in server time.
   *
   * @param time The server time.
   */
  void SetServerTime(int64_t time) { m_val.server_time = time; }

  /**
   * @{
   * @name Type Checkers
   */

  /**
   * Determine if entry value contains a value or is unassigned.
   *
   * @return True if the entry value contains a value.
   */
  bool IsValid() const { return m_val.type != NT_UNASSIGNED; }

  /**
   * Determine if entry value contains a boolean.
   *
   * @return True if the entry value is of boolean type.
   */
  bool IsBoolean() const { return m_val.type == NT_BOOLEAN; }

  /**
   * Determine if entry value contains an integer.
   *
   * @return True if the entry value is of integer type.
   */
  bool IsInteger() const { return m_val.type == NT_INTEGER; }

  /**
   * Determine if entry value contains a float.
   *
   * @return True if the entry value is of float type.
   */
  bool IsFloat() const { return m_val.type == NT_FLOAT; }

  /**
   * Determine if entry value contains a double.
   *
   * @return True if the entry value is of double type.
   */
  bool IsDouble() const { return m_val.type == NT_DOUBLE; }

  /**
   * Determine if entry value contains a string.
   *
   * @return True if the entry value is of string type.
   */
  bool IsString() const { return m_val.type == NT_STRING; }

  /**
   * Determine if entry value contains a raw.
   *
   * @return True if the entry value is of raw type.
   */
  bool IsRaw() const { return m_val.type == NT_RAW; }

  /**
   * Determine if entry value contains a boolean array.
   *
   * @return True if the entry value is of boolean array type.
   */
  bool IsBooleanArray() const { return m_val.type == NT_BOOLEAN_ARRAY; }

  /**
   * Determine if entry value contains an integer array.
   *
   * @return True if the entry value is of integer array type.
   */
  bool IsIntegerArray() const { return m_val.type == NT_INTEGER_ARRAY; }

  /**
   * Determine if entry value contains a float array.
   *
   * @return True if the entry value is of float array type.
   */
  bool IsFloatArray() const { return m_val.type == NT_FLOAT_ARRAY; }

  /**
   * Determine if entry value contains a double array.
   *
   * @return True if the entry value is of double array type.
   */
  bool IsDoubleArray() const { return m_val.type == NT_DOUBLE_ARRAY; }

  /**
   * Determine if entry value contains a string array.
   *
   * @return True if the entry value is of string array type.
   */
  bool IsStringArray() const { return m_val.type == NT_STRING_ARRAY; }

  /** @} */

  /**
   * @{
   * @name Type-Safe Getters
   */

  /**
   * Get the entry's boolean value.
   *
   * @return The boolean value.
   */
  bool GetBoolean() const {
    assert(m_val.type == NT_BOOLEAN);
    return m_val.data.v_boolean != 0;
  }

  /**
   * Get the entry's integer value.
   *
   * @return The integer value.
   */
  int64_t GetInteger() const {
    assert(m_val.type == NT_INTEGER);
    return m_val.data.v_int;
  }

  /**
   * Get the entry's float value.
   *
   * @return The float value.
   */
  float GetFloat() const {
    assert(m_val.type == NT_FLOAT);
    return m_val.data.v_float;
  }

  /**
   * Get the entry's double value.
   *
   * @return The double value.
   */
  double GetDouble() const {
    assert(m_val.type == NT_DOUBLE);
    return m_val.data.v_double;
  }

  /**
   * Get the entry's string value.
   *
   * @return The string value.
   */
  std::string_view GetString() const {
    assert(m_val.type == NT_STRING);
    return {m_val.data.v_string.str, m_val.data.v_string.len};
  }

  /**
   * Get the entry's raw value.
   *
   * @return The raw value.
   */
  std::span<const uint8_t> GetRaw() const {
    assert(m_val.type == NT_RAW);
    return {m_val.data.v_raw.data, m_val.data.v_raw.size};
  }

  /**
   * Get the entry's boolean array value.
   *
   * @return The boolean array value.
   */
  std::span<const int> GetBooleanArray() const {
    assert(m_val.type == NT_BOOLEAN_ARRAY);
    return {m_val.data.arr_boolean.arr, m_val.data.arr_boolean.size};
  }

  /**
   * Get the entry's integer array value.
   *
   * @return The integer array value.
   */
  std::span<const int64_t> GetIntegerArray() const {
    assert(m_val.type == NT_INTEGER_ARRAY);
    return {m_val.data.arr_int.arr, m_val.data.arr_int.size};
  }

  /**
   * Get the entry's float array value.
   *
   * @return The float array value.
   */
  std::span<const float> GetFloatArray() const {
    assert(m_val.type == NT_FLOAT_ARRAY);
    return {m_val.data.arr_float.arr, m_val.data.arr_float.size};
  }

  /**
   * Get the entry's double array value.
   *
   * @return The double array value.
   */
  std::span<const double> GetDoubleArray() const {
    assert(m_val.type == NT_DOUBLE_ARRAY);
    return {m_val.data.arr_double.arr, m_val.data.arr_double.size};
  }

  /**
   * Get the entry's string array value.
   *
   * @return The string array value.
   */
  std::span<const std::string> GetStringArray() const {
    assert(m_val.type == NT_STRING_ARRAY);
    return *static_cast<std::vector<std::string>*>(m_storage.get());
  }

  /** @} */

  /**
   * @{
   * @name Factory functions
   */

  /**
   * Creates a boolean entry value.
   *
   * @param value the value
   * @param time if nonzero, the creation time to use (instead of the current
   *             time)
   * @return The entry value
   */
  static Value MakeBoolean(bool value, int64_t time = 0) {
    Value val{NT_BOOLEAN, 0, time, private_init{}};
    val.m_val.data.v_boolean = value;
    return val;
  }

  /**
   * Creates an integer entry value.
   *
   * @param value the value
   * @param time if nonzero, the creation time to use (instead of the current
   *             time)
   * @return The entry value
   */
  static Value MakeInteger(int64_t value, int64_t time = 0) {
    Value val{NT_INTEGER, 0, time, private_init{}};
    val.m_val.data.v_int = value;
    return val;
  }

  /**
   * Creates a float entry value.
   *
   * @param value the value
   * @param time if nonzero, the creation time to use (instead of the current
   *             time)
   * @return The entry value
   */
  static Value MakeFloat(float value, int64_t time = 0) {
    Value val{NT_FLOAT, 0, time, private_init{}};
    val.m_val.data.v_float = value;
    return val;
  }

  /**
   * Creates a double entry value.
   *
   * @param value the value
   * @param time if nonzero, the creation time to use (instead of the current
   *             time)
   * @return The entry value
   */
  static Value MakeDouble(double value, int64_t time = 0) {
    Value val{NT_DOUBLE, 0, time, private_init{}};
    val.m_val.data.v_double = value;
    return val;
  }

  /**
   * Creates a string entry value.
   *
   * @param value the value
   * @param time if nonzero, the creation time to use (instead of the current
   *             time)
   * @return The entry value
   */
  static Value MakeString(std::string_view value, int64_t time = 0) {
    auto data = std::make_shared<std::string>(value);
    Value val{NT_STRING, data->capacity(), time, private_init{}};
    val.m_val.data.v_string.str = const_cast<char*>(data->c_str());
    val.m_val.data.v_string.len = data->size();
    val.m_storage = std::move(data);
    return val;
  }

  /**
   * Creates a string entry value.
   *
   * @param value the value
   * @param time if nonzero, the creation time to use (instead of the current
   *             time)
   * @return The entry value
   */
  template <std::same_as<std::string> T>
  static Value MakeString(T&& value, int64_t time = 0) {
    auto data = std::make_shared<std::string>(std::forward<T>(value));
    Value val{NT_STRING, data->capacity(), time, private_init{}};
    val.m_val.data.v_string.str = const_cast<char*>(data->c_str());
    val.m_val.data.v_string.len = data->size();
    val.m_storage = std::move(data);
    return val;
  }

  /**
   * Creates a raw entry value.
   *
   * @param value the value
   * @param time if nonzero, the creation time to use (instead of the current
   *             time)
   * @return The entry value
   */
  static Value MakeRaw(std::span<const uint8_t> value, int64_t time = 0) {
    auto data =
        std::make_shared<std::vector<uint8_t>>(value.begin(), value.end());
    Value val{NT_RAW, data->capacity(), time, private_init{}};
    val.m_val.data.v_raw.data = const_cast<uint8_t*>(data->data());
    val.m_val.data.v_raw.size = data->size();
    val.m_storage = std::move(data);
    return val;
  }

  /**
   * Creates a raw entry value.
   *
   * @param value the value
   * @param time if nonzero, the creation time to use (instead of the current
   *             time)
   * @return The entry value
   */
  template <std::same_as<std::vector<uint8_t>> T>
  static Value MakeRaw(T&& value, int64_t time = 0) {
    auto data = std::make_shared<std::vector<uint8_t>>(std::forward<T>(value));
    Value val{NT_RAW, data->capacity(), time, private_init{}};
    val.m_val.data.v_raw.data = const_cast<uint8_t*>(data->data());
    val.m_val.data.v_raw.size = data->size();
    val.m_storage = std::move(data);
    return val;
  }

  /**
   * Creates a boolean array entry value.
   *
   * @param value the value
   * @param time if nonzero, the creation time to use (instead of the current
   *             time)
   * @return The entry value
   */
  static Value MakeBooleanArray(std::span<const bool> value, int64_t time = 0);

  /**
   * Creates a boolean array entry value.
   *
   * @param value the value
   * @param time if nonzero, the creation time to use (instead of the current
   *             time)
   * @return The entry value
   */
  static Value MakeBooleanArray(std::initializer_list<bool> value,
                                int64_t time = 0) {
    return MakeBooleanArray(std::span(value.begin(), value.end()), time);
  }

  /**
   * Creates a boolean array entry value.
   *
   * @param value the value
   * @param time if nonzero, the creation time to use (instead of the current
   *             time)
   * @return The entry value
   */
  static Value MakeBooleanArray(std::span<const int> value, int64_t time = 0);

  /**
   * Creates a boolean array entry value.
   *
   * @param value the value
   * @param time if nonzero, the creation time to use (instead of the current
   *             time)
   * @return The entry value
   */
  static Value MakeBooleanArray(std::initializer_list<int> value,
                                int64_t time = 0) {
    return MakeBooleanArray(std::span(value.begin(), value.end()), time);
  }

  /**
   * Creates a boolean array entry value.
   *
   * @param value the value
   * @param time if nonzero, the creation time to use (instead of the current
   *             time)
   * @return The entry value
   *
   * @note This function moves the values out of the vector.
   */
  static Value MakeBooleanArray(std::vector<int>&& value, int64_t time = 0);

  /**
   * Creates an integer array entry value.
   *
   * @param value the value
   * @param time if nonzero, the creation time to use (instead of the current
   *             time)
   * @return The entry value
   */
  static Value MakeIntegerArray(std::span<const int64_t> value,
                                int64_t time = 0);

  /**
   * Creates an integer array entry value.
   *
   * @param value the value
   * @param time if nonzero, the creation time to use (instead of the current
   *             time)
   * @return The entry value
   */
  static Value MakeIntegerArray(std::initializer_list<int64_t> value,
                                int64_t time = 0) {
    return MakeIntegerArray(std::span(value.begin(), value.end()), time);
  }

  /**
   * Creates an integer array entry value.
   *
   * @param value the value
   * @param time if nonzero, the creation time to use (instead of the current
   *             time)
   * @return The entry value
   *
   * @note This function moves the values out of the vector.
   */
  static Value MakeIntegerArray(std::vector<int64_t>&& value, int64_t time = 0);

  /**
   * Creates a float array entry value.
   *
   * @param value the value
   * @param time if nonzero, the creation time to use (instead of the current
   *             time)
   * @return The entry value
   */
  static Value MakeFloatArray(std::span<const float> value, int64_t time = 0);

  /**
   * Creates a float array entry value.
   *
   * @param value the value
   * @param time if nonzero, the creation time to use (instead of the current
   *             time)
   * @return The entry value
   */
  static Value MakeFloatArray(std::initializer_list<float> value,
                              int64_t time = 0) {
    return MakeFloatArray(std::span(value.begin(), value.end()), time);
  }

  /**
   * Creates a float array entry value.
   *
   * @param value the value
   * @param time if nonzero, the creation time to use (instead of the current
   *             time)
   * @return The entry value
   *
   * @note This function moves the values out of the vector.
   */
  static Value MakeFloatArray(std::vector<float>&& value, int64_t time = 0);

  /**
   * Creates a double array entry value.
   *
   * @param value the value
   * @param time if nonzero, the creation time to use (instead of the current
   *             time)
   * @return The entry value
   */
  static Value MakeDoubleArray(std::span<const double> value, int64_t time = 0);

  /**
   * Creates a double array entry value.
   *
   * @param value the value
   * @param time if nonzero, the creation time to use (instead of the current
   *             time)
   * @return The entry value
   */
  static Value MakeDoubleArray(std::initializer_list<double> value,
                               int64_t time = 0) {
    return MakeDoubleArray(std::span(value.begin(), value.end()), time);
  }

  /**
   * Creates a double array entry value.
   *
   * @param value the value
   * @param time if nonzero, the creation time to use (instead of the current
   *             time)
   * @return The entry value
   *
   * @note This function moves the values out of the vector.
   */
  static Value MakeDoubleArray(std::vector<double>&& value, int64_t time = 0);

  /**
   * Creates a string array entry value.
   *
   * @param value the value
   * @param time if nonzero, the creation time to use (instead of the current
   *             time)
   * @return The entry value
   */
  static Value MakeStringArray(std::span<const std::string> value,
                               int64_t time = 0);

  /**
   * Creates a string array entry value.
   *
   * @param value the value
   * @param time if nonzero, the creation time to use (instead of the current
   *             time)
   * @return The entry value
   */
  static Value MakeStringArray(std::initializer_list<std::string> value,
                               int64_t time = 0) {
    return MakeStringArray(std::span(value.begin(), value.end()), time);
  }

  /**
   * Creates a string array entry value.
   *
   * @param value the value
   * @param time if nonzero, the creation time to use (instead of the current
   *             time)
   * @return The entry value
   *
   * @note This function moves the values out of the vector.
   */
  static Value MakeStringArray(std::vector<std::string>&& value,
                               int64_t time = 0);

  /** @} */

  friend bool operator==(const Value& lhs, const Value& rhs);

 private:
  NT_Value m_val = {};
  std::shared_ptr<void> m_storage;
  size_t m_size = 0;
};

#if __GNUC__ >= 13
#pragma GCC diagnostic pop
#endif

bool operator==(const Value& lhs, const Value& rhs);

/**
 * NetworkTable Value alias for similarity with Java.
 * @ingroup ntcore_cpp_api
 */
using NetworkTableValue = Value;

}  // namespace nt