自定义sio::message::ptr工具类

class tool_map
{
public:
    static bool try_get_value(sio::message::ptr& out_data,
                              const char* key, sio::message::ptr const& data)
    {
        auto map=data->get_map()[key];
        if(map && sio::message::flag_object == map->get_flag())
        {
            out_data = map;
            return true;
        }

        return false;
    }

    static bool try_get_value(int64_t& out_data, const char* key, sio::message::ptr const& data)
    {
        auto map=data->get_map()[key];
        if(map && sio::message::flag_integer == map->get_flag())
        {
            out_data = map->get_int();
            return true;
        }

        return false;
    }

    static bool try_get_value(uint32_t& out_data, const char* key, sio::message::ptr const& data)
    {
        auto map=data->get_map()[key];
        if(map && sio::message::flag_integer == map->get_flag())
        {
            out_data = static_cast<uint32_t>(map->get_int());
            return true;
        }

        return false;
    }

    static bool try_get_value(int& out_data, const char* key, sio::message::ptr const& data)
    {
        auto map=data->get_map()[key];
        if(map && sio::message::flag_integer == map->get_flag())
        {
            out_data = static_cast<int>(map->get_int());
            return true;
        }

        return false;
    }

    static bool try_get_value(std::string& out_data, const char* key, sio::message::ptr const& data)
    {
        auto map=data->get_map()[key];
        if(map && sio::message::flag_string == map->get_flag())
        {
            out_data = map->get_string();
            return true;
        }

        return false;
    }

    static bool try_get_value(double& out_data, const char* key, sio::message::ptr const& data)
    {
        auto map=data->get_map()[key];
        if(map && sio::message::flag_double == map->get_flag())
        {
            out_data = map->get_double();
            return true;
        }

        return false;
    }

    static bool try_get_value(std::vector<sio::message::ptr>& out_data,
                               const char* key, sio::message::ptr const& data)
    {
        auto map=data->get_map()[key];
        if(map && sio::message::flag_array == map->get_flag())
        {
            out_data = map->get_vector();
            return true;
        }

        return false;
    }
};

猜你喜欢

转载自blog.csdn.net/f110300641/article/details/82984875