blob: 0f1ccedeaae2327a06e5213958deb849cfda8524 [file] [log] [blame]
Yi Jin974a9c22017-10-02 18:37:08 -07001/*
2 * Copyright (C) 2017 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#ifndef ANDROID_UTIL_PROTOOUTPUT_STREAM_H
18#define ANDROID_UTIL_PROTOOUTPUT_STREAM_H
19
20#include <android/util/EncodedBuffer.h>
21
22#include <stdint.h>
23#include <string>
24
25namespace android {
26namespace util {
27
28/**
29 * Class to write to a protobuf stream.
30 *
31 * Each write method takes an ID code from the protoc generated classes
32 * and the value to write. To make a nested object, call start
33 * and then end when you are done.
34 *
35 * See the java version implementation (ProtoOutputStream.java) for more infos.
36 */
37class ProtoOutputStream
38{
39public:
Yi Jin42711a02017-10-11 18:20:24 -070040 ProtoOutputStream();
Yi Jin974a9c22017-10-02 18:37:08 -070041 ~ProtoOutputStream();
42
43 /**
44 * Write APIs for dumping protobuf data. Returns true if the write succeeds.
45 */
46 bool write(uint64_t fieldId, double val);
47 bool write(uint64_t fieldId, float val);
48 bool write(uint64_t fieldId, int val);
49 bool write(uint64_t fieldId, long long val);
50 bool write(uint64_t fieldId, bool val);
51 bool write(uint64_t fieldId, std::string val);
52 bool write(uint64_t fieldId, const char* val);
53
54 /**
55 * Starts a sub-message write session.
56 * Returns a token of this write session.
57 * Must call end(token) when finish write this sub-message.
58 */
59 long long start(uint64_t fieldId);
60 void end(long long token);
61
62 /**
Yi Jin42711a02017-10-11 18:20:24 -070063 * Flushes the protobuf data out to given fd.
Yi Jin974a9c22017-10-02 18:37:08 -070064 */
Yi Jin42711a02017-10-11 18:20:24 -070065 size_t size();
66 EncodedBuffer::iterator data();
67 bool flush(int fd);
68
69 // Please don't use the following functions to dump protos unless you are sure about it.
70 void writeRawVarint(uint64_t varint);
71 void writeLengthDelimitedHeader(uint32_t id, size_t size);
72 void writeRawByte(uint8_t byte);
Yi Jin974a9c22017-10-02 18:37:08 -070073
74private:
75 EncodedBuffer mBuffer;
Yi Jin974a9c22017-10-02 18:37:08 -070076 size_t mCopyBegin;
77 bool mCompact;
78 int mDepth;
79 int mObjectId;
80 long long mExpectedObjectToken;
81
82 inline void writeDoubleImpl(uint32_t id, double val);
83 inline void writeFloatImpl(uint32_t id, float val);
84 inline void writeInt64Impl(uint32_t id, long long val);
85 inline void writeInt32Impl(uint32_t id, int val);
86 inline void writeUint64Impl(uint32_t id, uint64_t val);
87 inline void writeUint32Impl(uint32_t id, uint32_t val);
88 inline void writeFixed64Impl(uint32_t id, uint64_t val);
89 inline void writeFixed32Impl(uint32_t id, uint32_t val);
90 inline void writeSFixed64Impl(uint32_t id, long long val);
91 inline void writeSFixed32Impl(uint32_t id, int val);
92 inline void writeZigzagInt64Impl(uint32_t id, long long val);
93 inline void writeZigzagInt32Impl(uint32_t id, int val);
94 inline void writeEnumImpl(uint32_t id, int val);
95 inline void writeBoolImpl(uint32_t id, bool val);
96 inline void writeUtf8StringImpl(uint32_t id, const char* val, size_t size);
97
98 bool compact();
99 size_t editEncodedSize(size_t rawSize);
100 bool compactSize(size_t rawSize);
101};
102
103}
104}
105
106#endif // ANDROID_UTIL_PROTOOUTPUT_STREAM_H