2015-06-21 22:42:01 -07:00
|
|
|
/*----------------------------------------------------------------------------*/
|
|
|
|
|
/* Copyright (c) FIRST 2015. All Rights Reserved. */
|
|
|
|
|
/* Open Source Software - may be modified and shared by FRC teams. The code */
|
|
|
|
|
/* must be accompanied by the FIRST BSD license file in the root directory of */
|
|
|
|
|
/* the project. */
|
|
|
|
|
/*----------------------------------------------------------------------------*/
|
|
|
|
|
|
|
|
|
|
#ifndef NT_WIREENCODER_H_
|
|
|
|
|
#define NT_WIREENCODER_H_
|
|
|
|
|
|
|
|
|
|
#include <cassert>
|
|
|
|
|
#include <cstddef>
|
|
|
|
|
|
|
|
|
|
#include "ntcore.h"
|
|
|
|
|
|
2015-06-25 22:57:43 -07:00
|
|
|
namespace ntimpl {
|
|
|
|
|
|
|
|
|
|
class WireEncoder {
|
|
|
|
|
public:
|
|
|
|
|
explicit WireEncoder(unsigned int proto_rev);
|
|
|
|
|
~WireEncoder();
|
|
|
|
|
|
|
|
|
|
void SetProtocolRev(unsigned int proto_rev) { m_proto_rev = proto_rev; }
|
|
|
|
|
|
|
|
|
|
void Reset() {
|
|
|
|
|
m_cur = m_start;
|
|
|
|
|
m_error = nullptr;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const char* GetError() const { return m_error; }
|
|
|
|
|
|
|
|
|
|
const char* GetData() const { return m_start; }
|
|
|
|
|
|
|
|
|
|
std::size_t GetSize() const { return m_cur - m_start; }
|
|
|
|
|
|
|
|
|
|
void Reserve(std::size_t len) {
|
|
|
|
|
// assert(m_end > m_cur);
|
|
|
|
|
if (static_cast<size_t>(m_end - m_cur) < len) ReserveSlow(len);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Write8(unsigned int val) {
|
|
|
|
|
Reserve(1);
|
|
|
|
|
*m_cur++ = (char)(val & 0xff);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Write16(unsigned int val) {
|
|
|
|
|
Reserve(2);
|
|
|
|
|
*m_cur++ = (char)((val >> 8) & 0xff);
|
|
|
|
|
*m_cur++ = (char)(val & 0xff);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Write32(unsigned long val) {
|
|
|
|
|
Reserve(4);
|
|
|
|
|
*m_cur++ = (char)((val >> 24) & 0xff);
|
|
|
|
|
*m_cur++ = (char)((val >> 16) & 0xff);
|
|
|
|
|
*m_cur++ = (char)((val >> 8) & 0xff);
|
|
|
|
|
*m_cur++ = (char)(val & 0xff);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void WriteDouble(double val);
|
|
|
|
|
|
|
|
|
|
void WriteULEB128(unsigned long val);
|
|
|
|
|
void WriteType(NT_Type type);
|
|
|
|
|
void WriteValue(const NT_Value& value);
|
|
|
|
|
void WriteString(const NT_String& str);
|
|
|
|
|
|
|
|
|
|
std::size_t GetValueSize(const NT_Value& value);
|
|
|
|
|
std::size_t GetStringSize(const NT_String& str);
|
|
|
|
|
|
|
|
|
|
WireEncoder(const WireEncoder&) = delete;
|
|
|
|
|
WireEncoder& operator=(const WireEncoder&) = delete;
|
|
|
|
|
|
|
|
|
|
protected:
|
|
|
|
|
unsigned int m_proto_rev;
|
|
|
|
|
const char* m_error;
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
void ReserveSlow(std::size_t len);
|
|
|
|
|
|
|
|
|
|
char* m_start;
|
|
|
|
|
char* m_cur;
|
|
|
|
|
char* m_end;
|
2015-06-21 22:42:01 -07:00
|
|
|
};
|
|
|
|
|
|
2015-06-25 22:57:43 -07:00
|
|
|
} // namespace ntimpl
|
2015-06-21 22:42:01 -07:00
|
|
|
|
2015-06-25 22:57:43 -07:00
|
|
|
#endif // NT_WIREENCODER_H_
|