variant.h 3.7 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 {
24
    template <int ID, typename Type> struct IDToType { typedef Type type_t; };
朔-望's avatar
朔-望 已提交
25

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

31 32 33 34 35 36 37 38
        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
朔-望 已提交
39

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

51 52 53 54 55 56 57 58 59
    template <size_t size> 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);
        //      }
    };
朔-望's avatar
朔-望 已提交
60

61 62 63 64 65 66
    template <typename... Ts> struct Variant {
        Variant(const Variant &variant) {
            //        std::cout << " 赋值构造函数 " << std::endl;
            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

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

79 80 81 82 83 84 85 86
        template <typename T> T &Get() const {
            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();
            }
        }
朔-望's avatar
朔-望 已提交
87

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

90 91 92 93 94 95
      private:
        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
朔-望 已提交
96

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

L
liuruilong 已提交
99
} // namespace paddle_mobile