variable.h 3.1 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

/* 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

L
liuruilong 已提交
21
#include "paddle_mobile_object.h"
朔-望's avatar
朔-望 已提交
22 23 24 25 26 27 28 29 30
#include <iostream>
#include <memory>
#include <string>
#include <typeindex>
#include <typeinfo>

namespace paddle_mobile {
namespace framework {
class Variable : public PaddleMobileObject {
L
liuruilong 已提交
31
public:
朔-望's avatar
朔-望 已提交
32 33 34
  Variable() {}
  ~Variable() {}

L
liuruilong 已提交
35 36
  template <typename T> const T *Get() const {
    return static_cast<const T *>(holder_->Ptr());
朔-望's avatar
朔-望 已提交
37 38 39 40
  }

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

L
liuruilong 已提交
41
  const std::string *Name() { return name_; }
朔-望's avatar
朔-望 已提交
42

L
liuruilong 已提交
43
  template <typename T> T *GetMutable() {
朔-望's avatar
朔-望 已提交
44 45 46 47 48 49
    if (!IsType<T>()) {
      if (*Name() == "pixel") {
        //        std::cout << " reset " << *Name() << std::endl;
      }
      holder_.reset(new PlaceholderImp<T>(new T()));
    }
L
liuruilong 已提交
50
    return static_cast<T *>(holder_->Ptr());
朔-望's avatar
朔-望 已提交
51 52
  }

L
liuruilong 已提交
53
  template <typename T> bool IsType() const {
朔-望's avatar
朔-望 已提交
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
    if (holder_) {
      //                printf("not null \n");
      printf(" holder type : %s, this type %s \n", holder_->Type().name(),
             typeid(T).name());
    }

    //              std::cout << " " << holder_->Type() << " " <<  typeid(T) <<
    //              std::endl;
    return holder_ != nullptr && holder_->Type() == typeid(T);
  }

  void Clear() { holder_.reset(); }

  std::type_index Type() const { return holder_->Type(); }

L
liuruilong 已提交
69
  void SetName(const std::string *name) { name_ = name; }
朔-望's avatar
朔-望 已提交
70

L
liuruilong 已提交
71
private:
朔-望's avatar
朔-望 已提交
72 73 74 75
  struct Placeholder {
    Placeholder() = default;
    virtual ~Placeholder() = default;

L
liuruilong 已提交
76 77
    virtual const std::type_info &Type() const = 0;
    virtual void *Ptr() const = 0;
朔-望's avatar
朔-望 已提交
78 79
  };

L
liuruilong 已提交
80 81
  template <typename T> struct PlaceholderImp : public Placeholder {
    explicit PlaceholderImp(T *ptr) : ptr_(ptr), type_(typeid(T)) {}
朔-望's avatar
朔-望 已提交
82

L
liuruilong 已提交
83 84 85
    virtual const std::type_info &Type() const { return type_; }
    virtual void *Ptr() const override {
      return static_cast<void *>(ptr_.get());
朔-望's avatar
朔-望 已提交
86 87 88
    }

    std::unique_ptr<T> ptr_;
L
liuruilong 已提交
89
    const std::type_info &type_;
朔-望's avatar
朔-望 已提交
90 91 92 93
  };

  std::unique_ptr<Placeholder> holder_;
  friend class Scope;
L
liuruilong 已提交
94
  const std::string *name_;
朔-望's avatar
朔-望 已提交
95
};
L
liuruilong 已提交
96 97
} // namespace framework
} // namespace paddle_mobile