WPILibC++  unspecified
WireEncoder.h
1 /*----------------------------------------------------------------------------*/
2 /* Copyright (c) 2015-2018 FIRST. All Rights Reserved. */
3 /* Open Source Software - may be modified and shared by FRC teams. The code */
4 /* must be accompanied by the FIRST BSD license file in the root directory of */
5 /* the project. */
6 /*----------------------------------------------------------------------------*/
7 
8 #ifndef NTCORE_WIREENCODER_H_
9 #define NTCORE_WIREENCODER_H_
10 
11 #include <stdint.h>
12 
13 #include <cassert>
14 #include <cstddef>
15 
16 #include <wpi/SmallVector.h>
17 #include <wpi/StringRef.h>
18 
19 #include "networktables/NetworkTableValue.h"
20 
21 namespace nt {
22 
23 /* Encodes native data for network transmission.
24  * This class maintains an internal memory buffer for written data so that
25  * it can be efficiently bursted to the network after a number of writes
26  * have been performed. For this reason, all operations are non-blocking.
27  */
28 class WireEncoder {
29  public:
30  explicit WireEncoder(unsigned int proto_rev);
31 
32  /* Change the protocol revision (mostly affects value encoding). */
33  void set_proto_rev(unsigned int proto_rev) { m_proto_rev = proto_rev; }
34 
35  /* Get the active protocol revision. */
36  unsigned int proto_rev() const { return m_proto_rev; }
37 
38  /* Clears buffer and error indicator. */
39  void Reset() {
40  m_data.clear();
41  m_error = nullptr;
42  }
43 
44  /* Returns error indicator (a string describing the error). Returns nullptr
45  * if no error has occurred.
46  */
47  const char* error() const { return m_error; }
48 
49  /* Returns pointer to start of memory buffer with written data. */
50  const char* data() const { return m_data.data(); }
51 
52  /* Returns number of bytes written to memory buffer. */
53  size_t size() const { return m_data.size(); }
54 
55  wpi::StringRef ToStringRef() const {
56  return wpi::StringRef(m_data.data(), m_data.size());
57  }
58 
59  /* Writes a single byte. */
60  void Write8(unsigned int val) {
61  m_data.push_back(static_cast<char>(val & 0xff));
62  }
63 
64  /* Writes a 16-bit word. */
65  void Write16(unsigned int val) {
66  m_data.append(
67  {static_cast<char>((val >> 8) & 0xff), static_cast<char>(val & 0xff)});
68  }
69 
70  /* Writes a 32-bit word. */
71  void Write32(uint32_t val) {
72  m_data.append({static_cast<char>((val >> 24) & 0xff),
73  static_cast<char>((val >> 16) & 0xff),
74  static_cast<char>((val >> 8) & 0xff),
75  static_cast<char>(val & 0xff)});
76  }
77 
78  /* Writes a double. */
79  void WriteDouble(double val);
80 
81  /* Writes an ULEB128-encoded unsigned integer. */
82  void WriteUleb128(uint32_t val);
83 
84  void WriteType(NT_Type type);
85  void WriteValue(const Value& value);
86  void WriteString(wpi::StringRef str);
87 
88  /* Utility function to get the written size of a value (without actually
89  * writing it).
90  */
91  size_t GetValueSize(const Value& value) const;
92 
93  /* Utility function to get the written size of a string (without actually
94  * writing it).
95  */
96  size_t GetStringSize(wpi::StringRef str) const;
97 
98  protected:
99  /* The protocol revision. E.g. 0x0200 for version 2.0. */
100  unsigned int m_proto_rev;
101 
102  /* Error indicator. */
103  const char* m_error;
104 
105  private:
107 };
108 
109 } // namespace nt
110 
111 #endif // NTCORE_WIREENCODER_H_
Definition: WireEncoder.h:28
Definition: IStorage.h:21
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:49
A network table entry value.
Definition: NetworkTableValue.h:35
pointer data()
Return a pointer to the vector&#39;s buffer, even if empty().
Definition: SmallVector.h:151
void append(in_iter in_start, in_iter in_end)
Add the specified range to the end of the SmallVector.
Definition: SmallVector.h:402