0.9.8.10
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
ByteString.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2007-2015 Hypertable, Inc.
3  *
4  * This file is part of Hypertable.
5  *
6  * Hypertable is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 3
9  * of the License, or any later version.
10  *
11  * Hypertable is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19  * 02110-1301, USA.
20  */
21 
27 #ifndef HYPERTABLE_BYTESTRING_H
28 #define HYPERTABLE_BYTESTRING_H
29 
30 #include <iostream>
31 
32 #include <boost/shared_array.hpp>
33 #include <boost/shared_ptr.hpp>
34 
35 #include "DynamicBuffer.h"
36 #include "Serialization.h"
37 
38 namespace Hypertable {
39 
47  class ByteString {
48  public:
50  ByteString() : ptr(0) { }
51 
56  ByteString(const uint8_t *buf) : ptr(buf) { }
57 
62  size_t length() const {
63  if (ptr == 0)
64  return 1;
65  const uint8_t *tmp_ptr = ptr;
66  uint32_t len = Serialization::decode_vi32(&tmp_ptr);
67  return (tmp_ptr - ptr) + len;
68  }
69 
71  uint8_t *next() {
72  uint8_t *rptr = (uint8_t *)ptr;
73  uint32_t len = Serialization::decode_vi32(&ptr);
74  ptr += len;
75  return rptr;
76  }
77 
83  size_t decode_length(const uint8_t **dptr) const {
84  *dptr = ptr;
85  return Serialization::decode_vi32(dptr);
86  }
87 
93  size_t write(uint8_t *dst) const {
94  size_t len = length();
95  if (ptr == 0)
97  else
98  memcpy(dst, ptr, len);
99  return len;
100  }
101 
106  const char *str() const {
107  const uint8_t *rptr = ptr;
109  return (const char *)rptr;
110  }
111 
116  operator bool () const {
117  return ptr != 0;
118  }
119 
121  const uint8_t *ptr;
122  };
123 
130  inline void append_as_byte_string(DynamicBuffer &dst_buf, const void *value,
131  uint32_t value_len) {
132  dst_buf.ensure(7 + value_len);
133  Serialization::encode_vi32(&dst_buf.ptr, value_len);
134  if (value_len > 0) {
135  memcpy(dst_buf.ptr, value, value_len);
136  dst_buf.ptr += value_len;
137  *dst_buf.ptr = 0;
138  }
139  }
140 
146  inline void append_as_byte_string(DynamicBuffer &dst_buf, const char *str) {
147  append_as_byte_string(dst_buf, str, strlen(str));
148  }
149 
152 } // namespace Hypertable
153 
154 
155 #endif // HYPERTABLE_BYTESTRING_H
156 
Po::typed_value< String > * str(String *v=0)
Definition: Properties.h:166
uint8_t * ptr
Pointer to the end of the used part of the buffer.
size_t write(uint8_t *dst) const
Writes the data of this ByteString into a pointer.
Definition: ByteString.h:93
A dynamic, resizable and reference counted memory buffer.
Definition: DynamicBuffer.h:42
void append_as_byte_string(DynamicBuffer &dst_buf, const void *value, uint32_t value_len)
Serializes and appends a byte array to a DynamicBuffer object.
Definition: ByteString.h:130
A class managing one or more serializable ByteStrings.
Definition: ByteString.h:47
const char * str() const
Returns a pointer to the String's deserialized data.
Definition: ByteString.h:106
ByteString(const uint8_t *buf)
Overloaded constructor: takes ownership of a pointer.
Definition: ByteString.h:56
A dynamic, resizable memory buffer.
ByteString()
Default constructor: starts with an empty string.
Definition: ByteString.h:50
Functions to serialize/deserialize primitives to/from a memory buffer.
size_t length() const
Retrieves the length of the serialized string.
Definition: ByteString.h:62
const uint8_t * ptr
The pointer to the serialized data.
Definition: ByteString.h:121
Hypertable definitions
void encode_vi32(uint8_t **bufp, uint32_t val)
Encode a integer (up to 32-bit) in variable length encoding.
size_t decode_length(const uint8_t **dptr) const
Retrieves the decoded length and returns a pointer to the string.
Definition: ByteString.h:83
void ensure(size_t len)
Ensure space for additional data Will grow the space to 1.5 of the needed space with existing data un...
Definition: DynamicBuffer.h:82
uint32_t decode_vi32(const uint8_t **bufp, size_t *remainp)
Decode a variable length encoded integer up to 32-bit.
uint8_t * next()
Retrieves the next serialized String in the buffer.
Definition: ByteString.h:71