scope.h 2.8 KB
Newer Older
朔-望's avatar
朔-望 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21

/* Copyright (c) 2016 Baidu, Inc. All Rights Reserved.
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
==============================================================================*/
#pragma once

#include "variable.h"
L
liuruilong 已提交
22 23 24
#include <list>          //std::list
#include <mutex>         //std::mutex
#include <unordered_map> //std::unordered_map
朔-望's avatar
朔-望 已提交
25 26

namespace paddle_mobile {
朔-望's avatar
朔-望 已提交
27 28
namespace framework {
class Scope {
29 30 31
public:
  Scope() {}
  ~Scope() {}
朔-望's avatar
朔-望 已提交
32

33
  Scope &NewScope() const;
朔-望's avatar
朔-望 已提交
34

35 36
  /// Create a variable with given name if it doesn't exist.
  Variable *Var(const std::string &name);
朔-望's avatar
朔-望 已提交
37

38 39
  /// Create a variable with a scope-unique name.
  Variable *Var(std::string *name = nullptr);
朔-望's avatar
朔-望 已提交
40

41
  void EraseVars(const std::vector<std::string> &var_names);
朔-望's avatar
朔-望 已提交
42

43 44 45
  /// Find a variable in the scope or any of its ancestors.  Returns
  /// nullptr if cannot find.
  Variable *FindVar(const std::string &name) const;
朔-望's avatar
朔-望 已提交
46

47
  const Scope *parent() const { return parent_; }
朔-望's avatar
朔-望 已提交
48

49 50 51
  /// Find the scope or an ancestor scope that contains the given
  /// variable.
  const Scope *FindScope(const Variable *var) const;
朔-望's avatar
朔-望 已提交
52

53
  void DeleteScope(Scope *scope) const;
朔-望's avatar
朔-望 已提交
54

55 56
  /// Drop all kids scopes belonged to this scope.
  void DropKids();
朔-望's avatar
朔-望 已提交
57

58 59
  // enumerate all the variables current contains.
  std::vector<std::string> LocalVarNames() const;
朔-望's avatar
朔-望 已提交
60

61 62 63
  // Rename variable to a new name
  void Rename(const std::string &origin_name,
              const std::string &new_name) const;
朔-望's avatar
朔-望 已提交
64

65 66
  // Rename variable to a new name and return the new name
  std::string Rename(const std::string &origin_name) const;
朔-望's avatar
朔-望 已提交
67

68
  Variable *FindVarLocally(const std::string &name) const;
朔-望's avatar
朔-望 已提交
69

70 71 72
private:
  // Call Scope::NewScope for a sub-scope.
  explicit Scope(Scope const *parent) : parent_(parent) {}
朔-望's avatar
朔-望 已提交
73

74 75 76
  mutable std::unordered_map<std::string, Variable *> vars_;
  mutable std::list<Scope *> kids_;
  Scope const *parent_{nullptr};
朔-望's avatar
朔-望 已提交
77

78
  mutable std::mutex mutex_;
朔-望's avatar
朔-望 已提交
79 80
};
} // namespace framework
L
liuruilong 已提交
81
} // namespace paddle_mobile