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