blob: 504d6e5d681be05591a5f9f10b5053ee8b9bf8f3 [file] [log] [blame]
Brian Silverman395d6252013-09-13 20:58:14 -07001// Copyright 2012 Google Inc. All Rights Reserved.
2//
Brian Silverman93871ee2013-09-14 18:15:28 -07003// Modified by FRC Team 971.
4//
Brian Silverman395d6252013-09-13 20:58:14 -07005// A buffer for dealing with data. Some special support for PTP is
6// available.
7
8#ifndef _GLIBUSB_GBUFFER_H_
9#define _GLIBUSB_GBUFFER_H_
10
11#include <stdint.h>
12#include <string>
Brian Silverman395d6252013-09-13 20:58:14 -070013
14namespace glibusb {
15
16// Buffer of bytes.
Brian Silverman93871ee2013-09-14 18:15:28 -070017// Only allocates memory when increasing the length beyond what the maximum
18// length for the lifetime of an instance.
Brian Silverman395d6252013-09-13 20:58:14 -070019class Buffer {
20 public:
Brian Silverman93871ee2013-09-14 18:15:28 -070021 typedef size_t size_type;
Brian Silverman395d6252013-09-13 20:58:14 -070022
23 // Destructor.
24 ~Buffer();
25
26 // Constructs an empty buffer.
27 Buffer();
28
29 // Constructs a buffer from memory of given length. Data is copied.
30 // (This is an interface to C functions, chiefly, libusb, and should
31 // only be used for such purposes.)
32 Buffer(const void *src, size_type length);
33
34 // Returns true iff the buffers contain the same data.
35 bool operator==(const Buffer &other) const;
36
37 // Returns true iff the buffer does not contain the same data.
38 bool operator!=(const Buffer &other) const;
39
40 // Returns a new allocated slice of the buffer.
41 Buffer *MakeSlice(size_type offset, size_type length) const;
42
43 // Returns the length.
Brian Silverman93871ee2013-09-14 18:15:28 -070044 size_type Length() const { return length_; }
Brian Silverman395d6252013-09-13 20:58:14 -070045
46 // Clears (as in std::vector) the buffer.
47 void Clear();
48
49 // Resizes (as in std::vector) the buffer.
50 void Resize(size_type length);
51
52 // Returns a pointer to the underlying store that of a guaranteed
53 // length (or CHECK), possibly beginning at a given offset. (These
54 // are interfaces to C functions, chiefly, libusb, and should only
55 // be used for such purposes.)
56 void *GetBufferPointer(size_type length);
57 const void *GetBufferPointer(size_type length) const;
58 void *GetBufferPointer(size_type offset, size_type length);
59 const void *GetBufferPointer(size_type offset, size_type length) const;
60
61 // Gets the value of integral type T from the buffer at the offset
62 // byte_offset, places it in value_out, and returns the length of
63 // the marshalled data. The integer is expected to be in little
64 // endian format. This template is specialized for
65 // {int,uint}{8,16,32,64}_t.
66 template <class T>
67 size_type Get(size_type byte_offset, T *value_out) const;
68
69 // Gets an ASCII string from the buffer at the offset byte_offset,
70 // places it in value_out, and returns the length of the marshalled
71 // data. The string data in the buffer is expected to be
72 // null-terminated.
73 size_type Get(size_type byte_offset, std::string *value_out) const;
74
75 // Puts the value of integral type T into the buffer offset
76 // byte_offset and returns the length of the marshalled data. Data
77 // are put in little endian format. This template is specialized for
78 // {int,uint}{8,16,32,64}_t.
79 template <class T>
80 size_type Put(size_type byte_offset, T value);
81
82 // Appends the value of type T into the buffer offset byte_offset
83 // and returns the length of the marshalled data. Data are appended
84 // in little endian format. This template is available for
85 // {int,uint}{8,16,32,64}_t.
86 template <class T> void Append(const T value);
87
88 // Append a buffer.
89 void Append(const Buffer &buffer);
90
91 // Inserts length bytes of (uninitialized) space at the beginning of
92 // the buffer.
93 void AddHeader(size_type length);
94
95 // Removes length bytes of space from the beginning of the buffer.
96 void RemoveHeader(size_type length);
97
98 // Copies the source buffer.
99 void Copy(const Buffer &source);
100
101#if 0
102 // Writes the contents of the buffer to the file, or dies.
103 void WriteOrDie(File *fp) const;
104
105 // Writes the contents of the buffer to the path, or dies.
106 void WriteToPathOrDie(const char *name) const;
107#endif
108
109 // Returns a hex dump of the buffer.
110 std::string Dump() const;
111
112 private:
113 // The underlying byte store.
Brian Silverman93871ee2013-09-14 18:15:28 -0700114 // An operator new[]-allocated array.
115 uint8_t *buffer_;
116 // How many bytes of buffer_ are valid. If this is 0, buffer_ might be NULL.
117 size_type length_;
118 // How many bytes long buffer_ is currently allocated as.
119 size_type allocated_length_;
Brian Silverman395d6252013-09-13 20:58:14 -0700120
121 Buffer(const Buffer &) = delete;
122 void operator=(const Buffer &) = delete;
123};
124
125
126// Template for Buffer::Append for integral values.
127template <typename T>
128void Buffer::Append(const T value) {
129 size_type offset = Length();
130 Resize(offset + sizeof(T));
131 Put(offset, value);
132}
133
134} // namespace glibusb
135
136#endif // _GLIBUSB_GBUFFER_H_