variant.h 2.6 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. */

W
wangliu 已提交
15
#include "common/enforce.h"
16
#include "common/log.h"
L
liuruilong 已提交
17

朔-望's avatar
朔-望 已提交
18 19 20
#pragma once

namespace paddle_mobile {
朔-望's avatar
朔-望 已提交
21 22 23 24
template <int ID, typename Type>
struct IDToType {
  typedef Type type_t;
};
朔-望's avatar
朔-望 已提交
25

朔-望's avatar
朔-望 已提交
26 27
template <typename F, typename... Ts>
struct VariantHelper {
28 29 30
  static const size_t size = sizeof(F) > VariantHelper<Ts...>::size
                                 ? sizeof(F)
                                 : VariantHelper<Ts...>::size;
朔-望's avatar
朔-望 已提交
31

32 33 34 35 36
  inline static void Destroy(size_t id, void *data) {
    if (id == typeid(F).hash_code()) {
      reinterpret_cast<F *>(data)->~F();
    } else {
      VariantHelper<Ts...>::Destroy(id, data);
朔-望's avatar
朔-望 已提交
37
    }
38
  }
朔-望's avatar
朔-望 已提交
39
};
朔-望's avatar
朔-望 已提交
40

朔-望's avatar
朔-望 已提交
41 42
template <typename F>
struct VariantHelper<F> {
43 44 45 46 47 48
  static const size_t size = sizeof(F);
  inline static void Destroy(size_t id, void *data) {
    if (id == typeid(F).hash_code()) {
      //              reinterpret_cast<F*>(data)->~F();
    } else {
      //              std::cout << "未匹配到 " << std::endl;
朔-望's avatar
朔-望 已提交
49
    }
50
  }
朔-望's avatar
朔-望 已提交
51
};
朔-望's avatar
朔-望 已提交
52

朔-望's avatar
朔-望 已提交
53 54 55
template <size_t size>
class RawData {
 public:
56 57 58
  char data[size];
  RawData() {}
  RawData(const RawData &raw_data) { strcpy(data, raw_data.data); }
朔-望's avatar
朔-望 已提交
59
};
朔-望's avatar
朔-望 已提交
60

朔-望's avatar
朔-望 已提交
61 62
template <typename... Ts>
struct Variant {
63 64 65 66
  Variant(const Variant &variant) {
    type_id = variant.type_id;
    data = variant.data;
  }
朔-望's avatar
朔-望 已提交
67

68 69 70 71
  Variant() : type_id(invalid_type()) {}
  ~Variant() {
    //        helper::Destroy(type_id, &data);
  }
朔-望's avatar
朔-望 已提交
72

朔-望's avatar
朔-望 已提交
73 74
  template <typename T, typename... Args>
  void Set(Args &&... args) {
75 76 77 78
    helper::Destroy(type_id, &data);
    new (&data) T(std::forward<Args>(args)...);
    type_id = typeid(T).hash_code();
  }
朔-望's avatar
朔-望 已提交
79

朔-望's avatar
朔-望 已提交
80 81
  template <typename T>
  T &Get() const {
82 83 84
    if (type_id == typeid(T).hash_code()) {
      return *const_cast<T *>(reinterpret_cast<const T *>(&data));
    } else {
W
wangliu 已提交
85
      PADDLE_MOBILE_THROW_EXCEPTION(" bad cast in variant ");
86
      exit(0);
朔-望's avatar
朔-望 已提交
87
    }
88
  }
朔-望's avatar
朔-望 已提交
89

90
  size_t TypeId() const { return type_id; }
朔-望's avatar
朔-望 已提交
91

朔-望's avatar
朔-望 已提交
92
 private:
93 94 95 96
  static inline size_t invalid_type() { return typeid(void).hash_code(); }
  typedef VariantHelper<Ts...> helper;
  size_t type_id;
  RawData<helper::size> data;
朔-望's avatar
朔-望 已提交
97
};
朔-望's avatar
朔-望 已提交
98

朔-望's avatar
朔-望 已提交
99 100 101 102
template <typename T>
struct Vistor {
  typedef T type_t;
};
朔-望's avatar
朔-望 已提交
103

朔-望's avatar
朔-望 已提交
104
}  // namespace paddle_mobile