variable.h 2.4 KB
Newer Older
L
liuruilong 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14
/* Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved.

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

朔-望's avatar
朔-望 已提交
15 16 17 18 19 20 21
#pragma once

#include <iostream>
#include <memory>
#include <string>
#include <typeindex>
#include <typeinfo>
W
wangliu 已提交
22
#include "../common/variant.h"
朔-望's avatar
朔-望 已提交
23
#include "paddle_mobile_object.h"
朔-望's avatar
朔-望 已提交
24 25

namespace paddle_mobile {
朔-望's avatar
朔-望 已提交
26
namespace framework {
W
wangliu 已提交
27 28
using std::string;

朔-望's avatar
朔-望 已提交
29
class Variable : public PaddleMobileObject {
朔-望's avatar
朔-望 已提交
30 31 32
 public:
  template <typename T>
  const T *Get() const {
33 34 35
    return static_cast<const T *>(holder_->Ptr());
  }

W
wangliu 已提交
36 37 38 39 40 41 42 43 44 45
  template <typename T>
  const T GetValue() const {
    return variant.Get<T>();
  }

  template <typename T>
  void SetValue(T value) {
    variant.Set<T>(value);
  }

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

W
wangliu 已提交
48
  const std::string Name() { return name_; }
49

朔-望's avatar
朔-望 已提交
50 51
  template <typename T>
  T *GetMutable() {
52 53
    if (!IsType<T>()) {
      holder_.reset(new PlaceholderImp<T>(new T()));
朔-望's avatar
朔-望 已提交
54
    }
55 56 57
    return static_cast<T *>(holder_->Ptr());
  }

朔-望's avatar
朔-望 已提交
58 59
  template <typename T>
  bool IsType() const {
60 61
    return holder_ != nullptr && holder_->Type() == typeid(T);
  }
朔-望's avatar
朔-望 已提交
62

63
  void Clear() { holder_.reset(); }
朔-望's avatar
朔-望 已提交
64

65
  std::type_index Type() const { return holder_->Type(); }
朔-望's avatar
朔-望 已提交
66

W
wangliu 已提交
67
  void SetName(const string name) { name_ = name; }
朔-望's avatar
朔-望 已提交
68

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

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

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

82 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 89
    std::unique_ptr<T> ptr_;
    const std::type_info &type_;
  };
W
wangliu 已提交
90
  Variant<int, bool, string, float, double> variant;
91 92
  std::unique_ptr<Placeholder> holder_;
  friend class Scope;
W
wangliu 已提交
93
  string name_;
朔-望's avatar
朔-望 已提交
94
};
朔-望's avatar
朔-望 已提交
95 96
}  // namespace framework
}  // namespace paddle_mobile