variable.h 2.3 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 24

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

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

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

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

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

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

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

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

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

朔-望's avatar
朔-望 已提交
64
 private:
65 66 67
  struct Placeholder {
    Placeholder() = default;
    virtual ~Placeholder() = default;
朔-望's avatar
朔-望 已提交
68

69 70 71
    virtual const std::type_info &Type() const = 0;
    virtual void *Ptr() const = 0;
  };
朔-望's avatar
朔-望 已提交
72

朔-望's avatar
朔-望 已提交
73 74
  template <typename T>
  struct PlaceholderImp : public Placeholder {
75
    explicit PlaceholderImp(T *ptr) : ptr_(ptr), type_(typeid(T)) {}
朔-望's avatar
朔-望 已提交
76

77 78 79 80
    virtual const std::type_info &Type() const { return type_; }
    virtual void *Ptr() const override {
      return static_cast<void *>(ptr_.get());
    }
朔-望's avatar
朔-望 已提交
81

82 83 84
    std::unique_ptr<T> ptr_;
    const std::type_info &type_;
  };
W
wangliu 已提交
85
  Variant<int, bool, string, float, double> variant;
86 87
  std::unique_ptr<Placeholder> holder_;
  friend class Scope;
W
wangliu 已提交
88
  string name_;
朔-望's avatar
朔-望 已提交
89
};
朔-望's avatar
朔-望 已提交
90 91
}  // namespace framework
}  // namespace paddle_mobile