Impala
Impalaistheopensource,nativeanalyticdatabaseforApacheHadoop.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
buffer-builder.h
Go to the documentation of this file.
1 // Copyright 2012 Cloudera Inc.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 
16 #ifndef IMPALA_UTIL_BUFFER_BUILDER_H
17 #define IMPALA_UTIL_BUFFER_BUILDER_H
18 
19 #include <stdlib.h>
20 #include <boost/cstdint.hpp>
21 
22 namespace impala {
23 
26  public:
27  BufferBuilder(uint8_t* dst_buffer, int dst_len)
28  : buffer_(dst_buffer), capacity_(dst_len), size_(0) {
29  }
30 
31  BufferBuilder(char* dst_buffer, int dst_len)
32  : buffer_(reinterpret_cast<uint8_t*>(dst_buffer)),
33  capacity_(dst_len), size_(0) {
34  }
35 
36  inline void Append(const void* buffer, int len) {
37  DCHECK_LE(size_ + len, capacity_);
38  memcpy(buffer_ + size_, buffer, len);
39  size_ += len;
40  }
41 
42  template<typename T>
43  inline void Append(const T& v) {
44  Append(&v, sizeof(T));
45  }
46 
47  int capacity() const { return capacity_; }
48  int size() const { return size_; }
49 
50  private:
51  uint8_t* buffer_;
52  int capacity_;
53  int size_;
54 };
55 
56 }
57 
58 #endif
BufferBuilder(uint8_t *dst_buffer, int dst_len)
void Append(const void *buffer, int len)
Utility class to build an in-memory buffer.
void Append(const T &v)
BufferBuilder(char *dst_buffer, int dst_len)