提交 7978f05d 编写于 作者: Y Yu Yang 提交者: GitHub

Merge pull request #2616 from reyoung/feature/variable_is_type

Add `Variable::IsType`
...@@ -25,21 +25,24 @@ class Variable { ...@@ -25,21 +25,24 @@ class Variable {
public: public:
template <typename T> template <typename T>
const T& Get() const { const T& Get() const {
PADDLE_ASSERT(holder_ != nullptr); PADDLE_ASSERT(IsType<T>());
PADDLE_ASSERT(std::type_index(typeid(T)) ==
std::type_index(holder_->Type()));
return *static_cast<const T*>(holder_->Ptr()); return *static_cast<const T*>(holder_->Ptr());
} }
template <typename T> template <typename T>
T* GetMutable() { T* GetMutable() {
if (holder_ == nullptr || if (!IsType<T>()) {
std::type_index(typeid(T)) != std::type_index(holder_->Type())) {
holder_.reset(new PlaceholderImpl<T>(new T())); holder_.reset(new PlaceholderImpl<T>(new T()));
} }
return static_cast<T*>(holder_->Ptr()); return static_cast<T*>(holder_->Ptr());
} }
template <typename T>
bool IsType() const {
return holder_ != nullptr &&
std::type_index(typeid(T)) == std::type_index(holder_->Type());
}
private: private:
struct Placeholder { struct Placeholder {
virtual ~Placeholder() {} virtual ~Placeholder() {}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册