1 /*
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 
23 using 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
28  * words, this losslessly stores a parsed protobuf object without
29  * having the .proto file that generated it.
30  */
31 class GenericMessage
32 {
33 public:
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 
66 private:
67     multimap<int,Node> mNodes;
68 };
69 
70 #endif // GENERIC_MESSAGE_H
71 
72