tvm
with.h
Go to the documentation of this file.
1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one
3  * or more contributor license agreements. See the NOTICE file
4  * distributed with this work for additional information
5  * regarding copyright ownership. The ASF licenses this file
6  * to you under the Apache License, Version 2.0 (the
7  * "License"); you may not use this file except in compliance
8  * with the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing,
13  * software distributed under the License is distributed on an
14  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15  * KIND, either express or implied. See the License for the
16  * specific language governing permissions and limitations
17  * under the License.
18  */
19 
25 #ifndef TVM_SUPPORT_WITH_H_
26 #define TVM_SUPPORT_WITH_H_
27 
28 #include <exception>
29 #include <functional>
30 #include <memory>
31 #include <utility>
32 #include <vector>
33 
34 namespace tvm {
35 
58 template <typename ContextType>
59 class With {
60  public:
65  template <typename... Args>
66  explicit With(Args&&... args) : ctx_(std::forward<Args>(args)...) {
67  ctx_.EnterWithScope();
68  }
70  ~With() noexcept(false) { ctx_.ExitWithScope(); }
71 
72  // Disable copy and move construction. `With` is intended only for
73  // use in nested contexts that are exited in the reverse order of
74  // entry. Allowing context to be copied or moved would break this
75  // expectation.
76  With(const With& other) = delete;
77  With& operator=(const With& other) = delete;
78  With(With&& other) = delete;
79  With& operator=(With&& other) = delete;
80 
81  ContextType* get() { return &ctx_; }
82  const ContextType* get() const { return &ctx_; }
83 
84  ContextType* operator->() { return get(); }
85  const ContextType* operator->() const { return get(); }
86  ContextType& operator*() { return *get(); }
87  const ContextType* operator*() const { return *get(); }
88 
89  ContextType operator()() { return ctx_; }
90 
91  private:
93  ContextType ctx_;
94 };
95 
113 template <typename ContextType>
114 class WithGroup {
115  public:
116  WithGroup() = default;
117  WithGroup(WithGroup&&) = default;
119  WithGroup(const WithGroup&) = delete;
120  WithGroup& operator=(const WithGroup&) = delete;
121 
126  template <typename... Args>
127  void Emplace(Args&&... args) {
128  entries_.push_back(std::make_unique<With<ContextType>>(std::forward<Args>(args)...));
129  }
130 
132  size_t size() const { return entries_.size(); }
133 
144  ~WithGroup() noexcept(false) {
145  bool unwinding = std::uncaught_exceptions() > 0;
146  std::exception_ptr first_exc;
147  while (!entries_.empty()) {
148  // Move the last entry out of the vector first, then destroy it.
149  // This ensures entries_ shrinks even if ~With() throws.
150  auto entry = std::move(entries_.back());
151  entries_.pop_back();
152  try {
153  entry.reset(); // calls ~With<ContextType>() -> ExitWithScope()
154  } catch (...) {
155  if (!unwinding && !first_exc) {
156  first_exc = std::current_exception();
157  }
158  }
159  }
160  if (first_exc) std::rethrow_exception(first_exc);
161  }
162 
163  private:
164  std::vector<std::unique_ptr<With<ContextType>>> entries_;
165 };
166 
167 } // namespace tvm
168 #endif // TVM_SUPPORT_WITH_H_
A group of RAII contexts managed together.
Definition: with.h:114
~WithGroup() noexcept(false)
Destructor — exits all contexts in reverse order.
Definition: with.h:144
WithGroup(const WithGroup &)=delete
WithGroup()=default
void Emplace(Args &&... args)
Construct a context and enter its scope.
Definition: with.h:127
WithGroup & operator=(WithGroup &&)=default
WithGroup(WithGroup &&)=default
WithGroup & operator=(const WithGroup &)=delete
size_t size() const
Number of active contexts in this group.
Definition: with.h:132
RAII wrapper function to enter and exit a context object similar to python's with syntax.
Definition: with.h:59
With & operator=(With &&other)=delete
With(Args &&... args)
constructor. Enter the scope of the context.
Definition: with.h:66
const ContextType * operator*() const
Definition: with.h:87
With(With &&other)=delete
const ContextType * operator->() const
Definition: with.h:85
ContextType * get()
Definition: with.h:81
With & operator=(const With &other)=delete
ContextType operator()()
Definition: with.h:89
ContextType & operator*()
Definition: with.h:86
With(const With &other)=delete
~With() noexcept(false)
destructor, leaves the scope of the context.
Definition: with.h:70
const ContextType * get() const
Definition: with.h:82
ContextType * operator->()
Definition: with.h:84
An object that builds and maintains block scope and StmtSref mapping for Dependence analysis.
Definition: analyzer.h:37