1 /*
2  * Copyright (C) 2021 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 #include "perfetto/tracing/internal/checked_scope.h"
18 
19 #include <utility>
20 
21 namespace perfetto {
22 namespace internal {
23 
24 #if PERFETTO_DCHECK_IS_ON()
CheckedScope(CheckedScope * parent_scope)25 CheckedScope::CheckedScope(CheckedScope* parent_scope)
26     : parent_scope_(parent_scope) {
27   if (parent_scope_) {
28     PERFETTO_DCHECK(parent_scope_->is_active());
29     parent_scope_->set_is_active(false);
30   }
31 }
32 
~CheckedScope()33 CheckedScope::~CheckedScope() {
34   Reset();
35 }
36 
Reset()37 void CheckedScope::Reset() {
38   if (!is_active_) {
39     // The only case when inactive scope could be destroyed is when Reset() was
40     // called explicitly or the contents of the object were moved away.
41     PERFETTO_DCHECK(deleted_);
42     return;
43   }
44   is_active_ = false;
45   deleted_ = true;
46   if (parent_scope_)
47     parent_scope_->set_is_active(true);
48 }
49 
CheckedScope(CheckedScope && other)50 CheckedScope::CheckedScope(CheckedScope&& other) {
51   *this = std::move(other);
52 }
53 
operator =(CheckedScope && other)54 CheckedScope& CheckedScope::operator=(CheckedScope&& other) {
55   is_active_ = other.is_active_;
56   parent_scope_ = other.parent_scope_;
57   deleted_ = other.deleted_;
58 
59   other.is_active_ = false;
60   other.parent_scope_ = nullptr;
61   other.deleted_ = true;
62 
63   return *this;
64 }
65 #endif
66 
67 }  // namespace internal
68 }  // namespace perfetto
69