/* 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. */ #include #include "common/enforce.h" #include "common/log.h" #pragma once namespace paddle_mobile { template struct IDToType { typedef Type type_t; }; template struct VariantHelper { static const size_t size = sizeof(F) > VariantHelper::size ? sizeof(F) : VariantHelper::size; inline static void Destroy(size_t id, void *data) { if (id == typeid(F).hash_code()) { reinterpret_cast(data)->~F(); } else { VariantHelper::Destroy(id, data); } } }; template struct VariantHelper { static const size_t size = sizeof(F); inline static void Destroy(size_t id, void *data) { if (id == typeid(F).hash_code()) { // reinterpret_cast(data)->~F(); } else { // std::cout << "未匹配到 " << std::endl; } } }; template class RawData { public: char data[size]; RawData() {} RawData(const RawData &raw_data) { strcpy(data, raw_data.data); } }; template struct Variant { Variant(const Variant &variant) { type_id = variant.type_id; data = variant.data; } Variant() : type_id(invalid_type()) {} ~Variant() { // helper::Destroy(type_id, &data); } template void Set(Args &&... args) { helper::Destroy(type_id, &data); new (&data) T(std::forward(args)...); type_id = typeid(T).hash_code(); } template T &Get() const { if (type_id == typeid(T).hash_code()) { return *const_cast(reinterpret_cast(&data)); } else { PADDLE_MOBILE_THROW_EXCEPTION(" bad cast in variant "); exit(0); } } size_t TypeId() const { return type_id; } private: static inline size_t invalid_type() { return typeid(void).hash_code(); } typedef VariantHelper helper; size_t type_id; RawData data; }; template struct Vistor { typedef T type_t; }; } // namespace paddle_mobile