c++ 实现 Go 中的 interface

  • 琐碎的知识点

    1
    2
    system("pause"); // for windows
    std::cin.get(); // use this to pause process if linux/mac
  • 单例

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    #include <mutex>

    class Noncopyable
    {
    public:
    Noncopyable() = default;
    Noncopyable(const Noncopyable &) = delete;
    Noncopyable &operator=(const Noncopyable &) = delete;
    Noncopyable(Noncopyable &&) = delete;
    Noncopyable &operator=(Noncopyable &&) = delete;
    };

    template <typename T>
    class SingleTon : public Noncopyable
    {
    private:
    static std::once_flag once_;
    static T *instance_;

    public:
    static T *instance()
    {
    std::call_once(once_, &SingleTon<T>::Init);
    return instance_;
    }

    static T &get_instance()
    {
    return *instance();
    }

    static void ShutDown()
    {
    delete instance_;
    }

    private:
    static void Init()
    {
    instance_ = new T();
    }
    };

    template <typename T>
    std::once_flag SingleTon<T>::once_;

    template <typename T>
    T *SingleTon<T>::instance_ = nullptr;
  • 可以存储任意值的东西

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    #include <unordered_map>
    #include "singleTon.hpp"

    // 数据类型
    class DataType final : public SingleTon<DataType>
    {
    public:
    using ptr = DataType *;
    using Type = uint16_t;

    private:
    Type count_{0};

    public:
    Type NewTypeID()
    {
    return ++count_;
    }
    };

    // 基础数据,标明是什么数据类型
    // 该类数据都继承于他
    class Base
    {
    public:
    using ptr = Base *;

    private:
    DataType::Type type_id_{0U};

    public:
    Base(DataType::Type type_id)
    : type_id_(type_id)
    {
    }

    virtual ~Base() = default;

    template <typename T>
    typename T::ptr Cast()
    {
    if (type_id_ != T::type_id_)
    {
    return nullptr;
    }

    return dynamic_cast<typename T::ptr>(this);
    }
    };

    template <typename T>
    class Any final : public Base
    {
    public:
    using ptr = Any *;
    using Type = T;

    static T default_data_;
    static DataType::Type type_id_;

    private:
    T data_{default_data_};

    public:
    Any(const T &data)
    : Base(type_id_), data_(data)
    {
    }

    ~Any() = default;

    void SetValue(const T &data)
    {
    data_ = data;
    }

    const T &GetValue()
    {
    return data_;
    }
    };

    template <typename T>
    T Any<T>::default_data_ = T();

    template <typename T>
    DataType::Type Any<T>::type_id_ = DataType::instance()->NewTypeID();

    class CustomDataMap final
    {
    public:
    using DataList = std::unordered_map<std::string, Base::ptr>;

    private:
    DataList data_list_;

    public:
    CustomDataMap() = default;
    ~CustomDataMap()
    {
    for (auto &iter : data_list_)
    {
    auto base_data = iter.second;
    if (base_data == nullptr)
    {
    continue;
    }

    delete base_data;
    }

    data_list_.clear();
    }

    bool HasValue(const std::string &key)
    {
    auto iter = data_list_.find(key);
    if (iter == data_list_.end())
    {
    return false;
    }

    return true;
    }

    template <typename T>
    bool AddValue(const std::string &key, const T &value)
    {
    auto iter = data_list_.find(key);
    if (iter != data_list_.end())
    {
    return false;
    }

    auto data = new Any<T>(value);
    auto result = data_list_.insert(std::make_pair(key, data)).second;
    if (!result)
    {
    delete data;
    return false;
    }

    return true;
    }

    template <typename T>
    bool SetValue(const std::string &key, const T &value)
    {
    auto iter = data_list_.find(key);
    if (iter == data_list_.end())
    {
    return false;
    }

    auto data_base = iter->second;
    if (data_base == nullptr)
    {
    return false;
    }

    auto data = data_base->Cast<Any<T>>();
    if (data == nullptr)
    {
    return false;
    }

    data->SetValue(value);
    return true;
    }

    template <typename T>
    const T &GetValue(const std::string &key)
    {
    auto iter = data_list_.find(key);
    if (iter == data_list_.end())
    {
    return Any<T>::default_data_;
    }

    auto data_base = iter->second;
    if (data_base == nullptr)
    {
    return Any<T>::default_data_;
    }

    auto data = data_base->Cast<Any<T>>();
    if (data == nullptr)
    {
    return Any<T>::default_data_;
    }

    return data->GetValue();
    }
    };
  • 测试

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    #include <iostream>
    #include "base/any.hpp"

    struct Test
    {
    int a;
    bool b;
    };

    int main()
    {
    Test t;
    t.a = 12;
    t.b = true;

    CustomDataMap map;
    map.AddValue<Test>("test", t);
    auto value = map.GetValue<Test>("test");
    std::cout << value.a << " - " << value.b << std::endl;
    return 0;
    }