blob: 7c4ad34e35c38e5812f798ef31d31f2a88afd55a [file] [log] [blame]
Joe Onorato1754d742016-11-21 17:51:35 -08001/*
2 * Copyright (C) 2016 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 GENERIC_MESSAGE_H
18#define GENERIC_MESSAGE_H
19
20#include <map>
21#include <string>
22
23using namespace std;
24
25/**
26 * Class to represent a protobuf Message, where we don't actually
27 * know what any of the fields are, just their type codes. In other
Kweku Adamsee649592017-10-04 16:03:57 -070028 * words, this losslessly stores a parsed protobuf object without
Joe Onorato1754d742016-11-21 17:51:35 -080029 * having the .proto file that generated it.
30 */
31class GenericMessage
32{
33public:
34 GenericMessage();
35 ~GenericMessage();
36
37 enum {
38 TYPE_VALUE32,
39 TYPE_VALUE64,
40 TYPE_MESSAGE,
41 TYPE_STRING,
42 TYPE_DATA
43 };
44
45 struct Node {
46 uint32_t type;
47 union {
48 uint32_t value32;
49 uint64_t value64;
50 GenericMessage* message;
51 string* str;
52 string* data;
53 };
54 };
55
56 void addInt32(int32_t fieldId, uint32_t value);
57 void addInt64(int32_t fieldId, uint64_t value);
58 GenericMessage* addMessage(int32_t fieldId);
59 void addString(int32_t fieldId, const string& value);
60
61 typedef multimap<int32_t,Node>::const_iterator const_iterator;
62 typedef pair<const_iterator,const_iterator> const_iterator_pair;
63
64 const_iterator_pair find(int fieldId) const;
65
66private:
67 multimap<int,Node> mNodes;
68};
69
70#endif // GENERIC_MESSAGE_H
71