/* 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. */ #pragma once #include #include #include #include "common/enforce.h" #include "common/log.h" #include "common/type_define.h" 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(kTypeId_t type, void *data) { if (type == type_id()) { reinterpret_cast(data)->~F(); } else { VariantHelper::Destroy(type, data); } } }; template struct VariantHelper { static const size_t size = sizeof(F); inline static void Destroy(kTypeId_t type, void *data) { if (type == type_id()) { // reinterpret_cast(data)->~F(); } else { // std::cout << "未匹配到 " << std::endl; } } }; template class RawData { public: char data[size]; // NOLINT RawData() {} RawData(const RawData &raw_data) { memcpy(data, raw_data.data, size); } RawData &operator=(const RawData &raw_data) { memcpy(data, raw_data.data, size); return *this; } }; template struct Variant { Variant() : type_(invalid_type()) {} Variant(const Variant &variant) { type_ = variant.type_; data_ = variant.data_; } virtual ~Variant() { // helper::Destroy(type_id, &data); } template void Set(Args &&... args) { helper::Destroy(type_, data_.data); new (data_.data) T(std::forward(args)...); type_ = type_id().hash_code(); } void SetString(const std::string &string) { helper::Destroy(type_, data_.data); type_ = type_id().hash_code(); strcpy(data_.data, string.c_str()); // NOLINT } std::string GetString() const { if (type_ == type_id()) { return std::string(data_.data); } else { PADDLE_MOBILE_THROW_EXCEPTION( " bad cast in variant data type not a string "); exit(0); } } template T &Get() const { if (type_ == type_id()) { 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); } else { return *const_cast(reinterpret_cast(data_.data)); } } kTypeId_t TypeId() const { return type_; } private: static inline kTypeId_t invalid_type() { return type_id().hash_code(); } typedef VariantHelper helper; kTypeId_t type_ = type_id().hash_code(); // todo use an anto size to suite this. RawData<64> data_; }; template struct Vistor { typedef T type_t; }; } // namespace paddle_mobile