variable.h 3.5 KB
Newer Older
D
dzhwinter 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13
//  Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserve.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//    http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
Y
Yi Wang 已提交
14 15 16 17 18 19 20 21 22 23 24 25 26 27
/*
  Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserve.
  Licensed under the Apache License, Version 2.0 (the "License");
  you may not use this file except in compliance with the License.
  You may obtain a copy of the License at
  http://www.apache.org/licenses/LICENSE-2.0
  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.
*/
#pragma once

Y
Yi Wang 已提交
28 29
#include <memory>
#include <typeindex>
Y
Yi Wang 已提交
30 31
#include <typeinfo>

Y
Yu Yang 已提交
32
#include "paddle/platform/enforce.h"
Y
Yi Wang 已提交
33

Y
Yi Wang 已提交
34 35 36 37 38 39 40
namespace paddle {
namespace framework {

class Variable {
 public:
  template <typename T>
  const T& Get() const {
Y
Yu Yang 已提交
41 42 43 44
    PADDLE_ENFORCE(holder_ != nullptr, "Variable must hold some thing");
    PADDLE_ENFORCE(IsType<T>(),
                   "Variable must be type %s, the holding type is %s",
                   typeid(T).name(), holder_->Type().name());
Y
Yi Wang 已提交
45 46 47
    return *static_cast<const T*>(holder_->Ptr());
  }

48 49
  bool IsInitialized() const { return holder_ != nullptr; }

Y
Yi Wang 已提交
50 51
  template <typename T>
  T* GetMutable() {
Y
Yu Yang 已提交
52
    if (!IsType<T>()) {
Y
Yi Wang 已提交
53
      holder_.reset(new PlaceholderImpl<T>(new T()));
Y
Yi Wang 已提交
54
    }
Y
Yi Wang 已提交
55
    return static_cast<T*>(holder_->Ptr());
Y
Yi Wang 已提交
56 57
  }

Y
Yu Yang 已提交
58 59 60 61 62 63
  template <typename T>
  bool IsType() const {
    return holder_ != nullptr &&
           std::type_index(typeid(T)) == std::type_index(holder_->Type());
  }

Y
Yu Yang 已提交
64 65
  void Clear() { holder_.reset(); }

66 67 68 69 70
  std::type_index Type() const {
    PADDLE_ENFORCE(holder_ != nullptr, "Must hold memory");
    return holder_->Type();
  }

Y
Yi Wang 已提交
71 72 73 74 75 76 77 78 79 80 81 82 83 84
 private:
  struct Placeholder {
    virtual ~Placeholder() {}
    virtual const std::type_info& Type() const = 0;
    virtual void* Ptr() const = 0;
  };

  // Placeholder hides type T, so it doesn't appear as a template
  // parameter of Variable.
  template <typename T>
  struct PlaceholderImpl : public Placeholder {
    PlaceholderImpl(T* ptr) : ptr_(ptr), type_(typeid(T)) {}

    virtual const std::type_info& Type() const { return type_; }
Y
Yi Wang 已提交
85
    virtual void* Ptr() const { return static_cast<void*>(ptr_.get()); }
Y
Yi Wang 已提交
86

Y
Yi Wang 已提交
87
    std::unique_ptr<T> ptr_;
Y
Yi Wang 已提交
88 89 90
    const std::type_info& type_;
  };

Y
Yi Wang 已提交
91 92
  std::unique_ptr<Placeholder>
      holder_;  // pointers to a PlaceholderImpl object indeed.
Y
Yi Wang 已提交
93 94 95 96 97 98 99 100 101 102

  // name_ is only meaningful with a Scope and accessible by it.
  //
  // NOTE: Please don't expose name_ by adding methods like
  // Variable::Name or Scope::VarName!  A variable could have a human
  // readable name or an auto-generated scope-unique name.  In the
  // former case, the caller knows the name and doesn't need to access
  // the name; in the latter case, the variable should be identified
  // by its address but not the unreadable name.
  friend class Scope;
Y
Yi Wang 已提交
103
  const std::string* name_;
Y
Yi Wang 已提交
104 105 106 107
};

}  // namespace framework
}  // namespace paddle