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
#pragma once

#include <memory>
#include <string>
19
#include "common/variant.h"
朔-望's avatar
朔-望 已提交
20 21

namespace paddle_mobile {
朔-望's avatar
朔-望 已提交
22
namespace framework {
W
wangliu 已提交
23

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

W
wangliu 已提交
31 32
  template <typename T>
  const T GetValue() const {
33
    if (type_id<T>().name() == type_id<std::string>().name()) {
34 35 36 37 38 39
      PADDLE_MOBILE_THROW_EXCEPTION(
          "Please use getString to get an string (to avoid of an issue with "
          "gcc "
          "stl lib with string copy)");
      exit(0);
    }
W
wangliu 已提交
40 41 42 43 44 45 46 47
    return variant.Get<T>();
  }

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

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

朔-望'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
    return holder_ != nullptr && holder_->Type() == type_id<T>().name();
61
  }
朔-望's avatar
朔-望 已提交
62

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

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

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

72
    virtual std::string Type() const = 0;
73 74
    virtual void *Ptr() const = 0;
  };
朔-望's avatar
朔-望 已提交
75

朔-望's avatar
朔-望 已提交
76 77
  template <typename T>
  struct PlaceholderImp : public Placeholder {
78
    explicit PlaceholderImp(T *ptr) : ptr_(ptr), type_(type_id<T>().name()) {}
朔-望's avatar
朔-望 已提交
79

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

83
    std::unique_ptr<T> ptr_;
84
    std::string type_;
85
  };
86

87
  friend class Scope;
88 89 90 91

  Variant<int, bool, std::string, float, double> variant;
  std::unique_ptr<Placeholder> holder_;
  std::string name_;
朔-望's avatar
朔-望 已提交
92
};
93

朔-望's avatar
朔-望 已提交
94 95
}  // namespace framework
}  // namespace paddle_mobile