]>
git.proxmox.com Git - ceph.git/blob - ceph/src/jaegertracing/thrift/lib/py/src/ext/binary.h
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
20 #ifndef THRIFT_PY_BINARY_H
21 #define THRIFT_PY_BINARY_H
24 #include "ext/protocol.h"
25 #include "ext/endian.h"
32 class BinaryProtocol
: public ProtocolBase
<BinaryProtocol
> {
34 virtual ~BinaryProtocol() {}
36 void writeI8(int8_t val
) { writeBuffer(reinterpret_cast<char*>(&val
), sizeof(int8_t)); }
38 void writeI16(int16_t val
) {
39 int16_t net
= static_cast<int16_t>(htons(val
));
40 writeBuffer(reinterpret_cast<char*>(&net
), sizeof(int16_t));
43 void writeI32(int32_t val
) {
44 int32_t net
= static_cast<int32_t>(htonl(val
));
45 writeBuffer(reinterpret_cast<char*>(&net
), sizeof(int32_t));
48 void writeI64(int64_t val
) {
49 int64_t net
= static_cast<int64_t>(htonll(val
));
50 writeBuffer(reinterpret_cast<char*>(&net
), sizeof(int64_t));
53 void writeDouble(double dub
) {
54 // Unfortunately, bitwise_cast doesn't work in C. Bad C!
63 void writeBool(int v
) { writeByte(static_cast<uint8_t>(v
)); }
65 void writeString(PyObject
* value
, int32_t len
) {
67 writeBuffer(PyBytes_AS_STRING(value
), len
);
70 bool writeListBegin(PyObject
* value
, const SetListTypeArgs
& parsedargs
, int32_t len
) {
71 writeByte(parsedargs
.element_type
);
76 bool writeMapBegin(PyObject
* value
, const MapTypeArgs
& parsedargs
, int32_t len
) {
77 writeByte(parsedargs
.ktag
);
78 writeByte(parsedargs
.vtag
);
83 bool writeStructBegin() { return true; }
84 bool writeStructEnd() { return true; }
85 bool writeField(PyObject
* value
, const StructItemSpec
& parsedspec
) {
86 writeByte(static_cast<uint8_t>(parsedspec
.type
));
87 writeI16(parsedspec
.tag
);
88 return encodeValue(value
, parsedspec
.type
, parsedspec
.typeargs
);
91 void writeFieldStop() { writeByte(static_cast<uint8_t>(T_STOP
)); }
93 bool readBool(bool& val
) {
95 if (!readBytes(&buf
, 1)) {
102 bool readI8(int8_t& val
) {
104 if (!readBytes(&buf
, 1)) {
111 bool readI16(int16_t& val
) {
113 if (!readBytes(&buf
, sizeof(int16_t))) {
116 memcpy(&val
, buf
, sizeof(int16_t));
121 bool readI32(int32_t& val
) {
123 if (!readBytes(&buf
, sizeof(int32_t))) {
126 memcpy(&val
, buf
, sizeof(int32_t));
131 bool readI64(int64_t& val
) {
133 if (!readBytes(&buf
, sizeof(int64_t))) {
136 memcpy(&val
, buf
, sizeof(int64_t));
141 bool readDouble(double& val
) {
147 if (!readI64(transfer
.f
)) {
154 int32_t readString(char** buf
) {
156 if (!readI32(len
) || !checkLengthLimit(len
, stringLimit()) || !readBytes(buf
, len
)) {
162 int32_t readListBegin(TType
& etype
) {
165 if (!readByte(b
) || !readI32(len
) || !checkLengthLimit(len
, containerLimit())) {
168 etype
= static_cast<TType
>(b
);
172 int32_t readMapBegin(TType
& ktype
, TType
& vtype
) {
175 if (!readByte(k
) || !readByte(v
) || !readI32(len
) || !checkLengthLimit(len
, containerLimit())) {
178 ktype
= static_cast<TType
>(k
);
179 vtype
= static_cast<TType
>(v
);
183 bool readStructBegin() { return true; }
184 bool readStructEnd() { return true; }
186 bool readFieldBegin(TType
& type
, int16_t& tag
);
188 #define SKIPBYTES(n) \
190 if (!readBytes(&dummy_buf_, (n))) { \
196 bool skipBool() { SKIPBYTES(1); }
197 bool skipByte() { SKIPBYTES(1); }
198 bool skipI16() { SKIPBYTES(2); }
199 bool skipI32() { SKIPBYTES(4); }
200 bool skipI64() { SKIPBYTES(8); }
201 bool skipDouble() { SKIPBYTES(8); }
217 #endif // THRIFT_PY_BINARY_H