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 <dmlc/common.h>
29 
30 #include <functional>
31 #include <utility>
32 
33 namespace tvm {
34 
57 template <typename ContextType>
58 class With {
59  public:
64  template <typename... Args>
65  explicit With(Args&&... args) : ctx_(std::forward<Args>(args)...) {
66  ctx_.EnterWithScope();
67  }
69  ~With() DMLC_THROW_EXCEPTION { ctx_.ExitWithScope(); }
70 
71  ContextType* get() { return &ctx_; }
72  const ContextType* get() const { return &ctx_; }
73 
74  ContextType* operator->() { return get(); }
75  const ContextType* operator->() const { return get(); }
76  ContextType& operator*() { return *get(); }
77  const ContextType* operator*() const { return *get(); }
78 
79  ContextType operator()() { return ctx_; }
80 
81  private:
83  ContextType ctx_;
84 };
85 
91  public:
99  template <class FEnter, class FExit>
100  explicit ContextManager(FEnter f_enter, FExit f_exit) : f_enter_(f_enter), f_exit_(f_exit) {}
101 
102  private:
103  void EnterWithScope() {
104  if (f_enter_) f_enter_();
105  }
106  void ExitWithScope() {
107  if (f_exit_) f_exit_();
108  }
109  std::function<void()> f_enter_;
110  std::function<void()> f_exit_;
111  template <typename>
112  friend class With;
113 };
114 
115 } // namespace tvm
116 #endif // TVM_SUPPORT_WITH_H_
ContextType operator()()
Definition: with.h:79
~With() DMLC_THROW_EXCEPTION
destructor, leaves the scope of the context.
Definition: with.h:69
runtime implementation for LibTorch/TorchScript.
Definition: analyzer.h:36
With(Args &&... args)
constructor. Enter the scope of the context.
Definition: with.h:65
Definition: loop_state.h:456
ContextType & operator*()
Definition: with.h:76
A context type that delegates EnterWithScope and ExitWithScope to user-provided functions.
Definition: with.h:90
const ContextType * operator*() const
Definition: with.h:77
RAII wrapper function to enter and exit a context object similar to python&#39;s with syntax...
Definition: with.h:58
ContextManager(FEnter f_enter, FExit f_exit)
Constructor of ContextManager.
Definition: with.h:100
const ContextType * operator->() const
Definition: with.h:75
ContextType * operator->()
Definition: with.h:74