variant.h 3.3 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 21 22 23
/* 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.
==============================================================================*/

#include <iostream>

#pragma once

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

L
liuruilong 已提交
26
template <typename F, typename... Ts> struct VariantHelper {
朔-望's avatar
朔-望 已提交
27 28 29 30 31 32 33 34 35 36 37 38 39
  static const size_t size = sizeof(F) > VariantHelper<Ts...>::size
                                 ? sizeof(F)
                                 : VariantHelper<Ts...>::size;

  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);
    }
  }
};

L
liuruilong 已提交
40
template <typename F> struct VariantHelper<F> {
朔-望's avatar
朔-望 已提交
41 42 43 44 45 46 47 48 49 50
  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;
    }
  }
};

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

L
liuruilong 已提交
61
template <typename... Ts> struct Variant {
朔-望's avatar
朔-望 已提交
62 63 64 65 66 67 68 69 70 71 72
  Variant(const Variant &variant) {
    //        std::cout << " 赋值构造函数 " << std::endl;
    type_id = variant.type_id;
    data = variant.data;
  }

  Variant() : type_id(invalid_type()) {}
  ~Variant() {
    //        helper::Destroy(type_id, &data);
  }

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

L
liuruilong 已提交
79
  template <typename T> T &Get() const {
朔-望's avatar
朔-望 已提交
80 81 82 83 84 85 86 87 88 89
    if (type_id == typeid(T).hash_code()) {
      return *const_cast<T *>(reinterpret_cast<const T *>(&data));
    } else {
      //      std::cout << " bad cast in variant " << std::endl;
      throw std::bad_cast();
    }
  }

  size_t TypeId() const { return type_id; }

L
liuruilong 已提交
90
private:
朔-望's avatar
朔-望 已提交
91 92 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;
};

L
liuruilong 已提交
97
template <typename T> struct Vistor { typedef T type_t; };
朔-望's avatar
朔-望 已提交
98

L
liuruilong 已提交
99
} // namespace paddle_mobile