]>
git.proxmox.com Git - ceph.git/blob - ceph/src/rocksdb/third-party/fbson/FbsonStream.h
2 * Copyright (c) 2011-present, Facebook, Inc.
5 * This source code is licensed under the BSD-style license found in the
6 * LICENSE file in the root directory of this source tree. An additional grant
7 * of patent rights can be found in the PATENTS file in the same directory.
12 * This header file defines FbsonInBuffer and FbsonOutStream classes.
15 * FbsonInBuffer is a customer input buffer to wrap raw character buffer. Its
16 * object instances are used to create std::istream objects interally.
19 * FbsonOutStream is a custom output stream classes, to contain the FBSON
20 * serialized binary. The class is conveniently used to specialize templates of
21 * FbsonParser and FbsonWriter.
23 * @author Tian Xia <tianx@fb.com>
26 #ifndef FBSON_FBSONSTREAM_H
27 #define FBSON_FBSONSTREAM_H
29 #ifndef __STDC_FORMAT_MACROS
30 #define __STDC_FORMAT_MACROS
33 #if defined OS_WIN && !defined snprintf
34 #define snprintf _snprintf
42 // lengths includes sign
43 #define MAX_INT_DIGITS 11
44 #define MAX_INT64_DIGITS 20
45 #define MAX_DOUBLE_DIGITS 23 // 1(sign)+16(significant)+1(decimal)+5(exponent)
48 * FBSON's implementation of input buffer
50 class FbsonInBuffer
: public std::streambuf
{
52 FbsonInBuffer(const char* str
, uint32_t len
) {
53 // this is read buffer and the str will not be changed
54 // so we use const_cast (ugly!) to remove constness
55 char* pch(const_cast<char*>(str
));
56 setg(pch
, pch
, pch
+ len
);
61 * FBSON's implementation of output stream.
63 * This is a wrapper of a char buffer. By default, the buffer capacity is 1024
64 * bytes. We will double the buffer if realloc is needed for writes.
66 class FbsonOutStream
: public std::ostream
{
68 explicit FbsonOutStream(uint32_t capacity
= 1024)
69 : std::ostream(nullptr),
78 head_
= (char*)malloc(capacity_
);
81 FbsonOutStream(char* buffer
, uint32_t capacity
)
82 : std::ostream(nullptr),
87 assert(buffer
&& capacity_
> 0);
96 void put(char c
) { write(&c
, 1); }
98 void write(const char* c_str
) { write(c_str
, (uint32_t)strlen(c_str
)); }
100 void write(const char* bytes
, uint32_t len
) {
104 if (size_
+ len
> capacity_
) {
108 memcpy(head_
+ size_
, bytes
, len
);
112 // write the integer to string
114 // snprintf automatically adds a NULL, so we need one more char
115 if (size_
+ MAX_INT_DIGITS
+ 1 > capacity_
) {
116 realloc(MAX_INT_DIGITS
+ 1);
119 int len
= snprintf(head_
+ size_
, MAX_INT_DIGITS
+ 1, "%d", i
);
124 // write the 64bit integer to string
125 void write(int64_t l
) {
126 // snprintf automatically adds a NULL, so we need one more char
127 if (size_
+ MAX_INT64_DIGITS
+ 1 > capacity_
) {
128 realloc(MAX_INT64_DIGITS
+ 1);
131 int len
= snprintf(head_
+ size_
, MAX_INT64_DIGITS
+ 1, "%" PRIi64
, l
);
136 // write the double to string
137 void write(double d
) {
138 // snprintf automatically adds a NULL, so we need one more char
139 if (size_
+ MAX_DOUBLE_DIGITS
+ 1 > capacity_
) {
140 realloc(MAX_DOUBLE_DIGITS
+ 1);
143 int len
= snprintf(head_
+ size_
, MAX_DOUBLE_DIGITS
+ 1, "%.15g", d
);
148 pos_type
tellp() const { return size_
; }
150 void seekp(pos_type pos
) { size_
= (uint32_t)pos
; }
152 const char* getBuffer() const { return head_
; }
154 pos_type
getSize() const { return tellp(); }
157 void realloc(uint32_t len
) {
158 assert(capacity_
> 0);
161 while (capacity_
< size_
+ len
) {
166 char* new_buf
= (char*)::realloc(head_
, capacity_
);
170 char* new_buf
= (char*)::malloc(capacity_
);
172 memcpy(new_buf
, head_
, size_
);
187 #endif // FBSON_FBSONSTREAM_H