/* 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 #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); } // void operator=(const RawData &raw_data){ // strcpy(data, raw_data.data); // } }; template struct Variant { 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); } 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 { // std::cout << " bad cast in variant " << std::endl; throw std::bad_cast(); } } 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