• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright 2019 Huawei Technologies Co., Ltd
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 MINDSPORE_CORE_IR_SCOPE_H_
18 #define MINDSPORE_CORE_IR_SCOPE_H_
19 #include <string>
20 #include <memory>
21 #include <stack>
22 #include "utils/visible.h"
23 
24 namespace mindspore {
25 class Scope;
26 using ScopePtr = std::shared_ptr<Scope>;
27 extern const ScopePtr kDefaultScope;
28 
29 class MS_CORE_API Scope {
30  public:
31   // using the default scope
Scope(const std::string & name)32   explicit Scope(const std::string &name) : name_(name) {}
33   ~Scope() = default;
name()34   std::string name() const { return name_; }
35 
36  private:
37   std::string name_;
38 };
39 
40 class MS_CORE_API ScopeManager {
41  public:
GetInstance()42   static ScopeManager &GetInstance() noexcept {
43     static ScopeManager instance;
44     return instance;
45   }
46   ScopeManager(const ScopeManager &) = delete;
47   ScopeManager &operator=(const ScopeManager &) = delete;
48   ~ScopeManager() = default;
49   void EnterScope(const ScopePtr &scope);
50   void LeaveScope(const ScopePtr &scope) noexcept;
51   ScopePtr GetCurrentScope();
52   void ClearScope();
53 
54  private:
55   ScopeManager() = default;
56   std::stack<ScopePtr> scope_stack_;
57 };
58 // ScopeGuard is a class that help generate the anf node of specified scope
59 // in the current c++ action scope.
60 class ScopeGuard {
61  public:
ScopeGuard(const ScopePtr & scope)62   explicit ScopeGuard(const ScopePtr &scope) {
63     scope_ = scope;
64     ScopeManager::GetInstance().EnterScope(scope);
65   }
~ScopeGuard()66   ~ScopeGuard() { ScopeManager::GetInstance().LeaveScope(scope_); }
67 
68  private:
69   ScopePtr scope_;
70 };
71 }  // namespace mindspore
72 #endif  // MINDSPORE_CORE_IR_SCOPE_H_
73