• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2020 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 INCLUDE_PERFETTO_PROTOZERO_ROOT_MESSAGE_H_
18 #define INCLUDE_PERFETTO_PROTOZERO_ROOT_MESSAGE_H_
19 
20 #include "perfetto/protozero/message.h"
21 #include "perfetto/protozero/message_arena.h"
22 
23 namespace protozero {
24 
25 // Helper class to hand out messages using the default MessageArena.
26 // Usage:
27 // RootMessage<perfetto::protos::zero::MyMessage> msg;
28 // msg.Reset(stream_writer);
29 // msg.set_foo(...);
30 // auto* nested = msg.set_nested();
31 template <typename T = Message>
32 class RootMessage : public T {
33  public:
RootMessage()34   RootMessage() { T::Reset(nullptr, &root_arena_); }
35 
36   // Disallow copy and move.
37   RootMessage(const RootMessage&) = delete;
38   RootMessage& operator=(const RootMessage&) = delete;
39   RootMessage(RootMessage&&) = delete;
40   RootMessage& operator=(RootMessage&&) = delete;
41 
Reset(ScatteredStreamWriter * writer)42   void Reset(ScatteredStreamWriter* writer) {
43     root_arena_.Reset();
44     Message::Reset(writer, &root_arena_);
45   }
46 
47  private:
48   MessageArena root_arena_;
49 };
50 
51 }  // namespace protozero
52 
53 #endif  // INCLUDE_PERFETTO_PROTOZERO_ROOT_MESSAGE_H_
54