当前位置: 首页>>代码示例>>C++>>正文


C++ variant::is_valid方法代码示例

本文整理汇总了C++中variant::is_valid方法的典型用法代码示例。如果您正苦于以下问题:C++ variant::is_valid方法的具体用法?C++ variant::is_valid怎么用?C++ variant::is_valid使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在variant的用法示例。


在下文中一共展示了variant::is_valid方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。

示例1: swap

void variant::swap(variant& other)
{
    if (this == &other)
        return;

    const bool is_this_valid = is_valid();
    const bool is_other_valid = other.is_valid();

    if (!is_this_valid && !is_other_valid)
        return;

    if (is_this_valid && is_other_valid)
    {
        detail::variant_data tmp_data;
        detail::variant_policy_func tmp_policy_func = other.m_policy;
        other.m_policy(detail::variant_policy_operation::SWAP, other.m_data, tmp_data);

        m_policy(detail::variant_policy_operation::SWAP, m_data, other.m_data);
        other.m_policy = m_policy;

        tmp_policy_func(detail::variant_policy_operation::SWAP, tmp_data, m_data);
        m_policy = tmp_policy_func;
    }
    else
    {
        detail::variant_data& full_data = is_this_valid ? m_data : other.m_data;
        detail::variant_data& empty_data = is_this_valid ? other.m_data : m_data;
        detail::variant_policy_func full_policy_func = is_this_valid ? m_policy : other.m_policy;

        full_policy_func(detail::variant_policy_operation::SWAP, full_data, empty_data);

        std::swap(m_policy, other.m_policy);
    }
}
开发者ID:Victorique-GOSICK,项目名称:rttr,代码行数:34,代码来源:variant.cpp

示例2: REQUIRE

}

////////////////////////////////////////////////////////////////////////////////////////

TEST_CASE("constructor - invoke general", "[constructor]")
{
    type t = type::get<ctor_invoke_test>();
    REQUIRE(t.is_valid() == true);

    SECTION("invoke default ctor")
    {
        constructor ctor = t.get_constructor();
        REQUIRE(ctor.is_valid() == true);

        variant var = ctor.invoke();
        CHECK(var.is_valid() == true);
        CHECK(var.get_type() == type::get<ctor_invoke_test*>());
        ctor_invoke_test* obj = var.get_value<ctor_invoke_test*>();
        CHECK(obj->default_ctor_invoked == true);
        CHECK(t.get_destructor().invoke(var) == true);
    }

    SECTION("invoke copy-ctor")
    {
        constructor ctor = t.get_constructor({type::get<ctor_invoke_test>()});
        REQUIRE(ctor.is_valid() == true);

        variant var = ctor.invoke(12);
        CHECK(var.is_valid() == false);

        ctor_invoke_test obj_default;
开发者ID:The-Futur1st,项目名称:rttr,代码行数:31,代码来源:constructor_invoke_test.cpp

示例3: REQUIRE

}

/////////////////////////////////////////////////////////////////////////////////////////

TEST_CASE("Test method", "[method]") 
{
    type t_meth = type::get("method_test");
    REQUIRE(t_meth.is_valid() == true);
    variant inst = t_meth.create({});
    method_test& obj = *inst.get_value<method_test*>();

    ////////////////////////////////////////////////////////////
    // invoke tests
    variant ret = t_meth.get_method("method_1").invoke(inst);
    REQUIRE(obj.method_1_called == true);
    REQUIRE(ret.is_valid() == true);
    REQUIRE(ret.is_type<void>() == true);
    
    ////////////////////////////////////////
    obj.method_1_called = false; // reset
    method meth = t_meth.get_method("method_1");
    meth.invoke_variadic(inst, {});
    REQUIRE(obj.method_1_called == true);
    REQUIRE(meth.get_name() == "method_1");
    REQUIRE(meth.get_parameter_types().empty() == true);

    ////////////////////////////////////////
    t_meth.get_method("method_2").invoke(inst);
    REQUIRE(obj.method_2_called == true);
    obj.method_2_called = false;
    meth = t_meth.get_method("method_2");
开发者ID:sanathkumarv,项目名称:RestAPIWt,代码行数:31,代码来源:test_method_reflection.cpp

示例4: convert

bool variant::convert(const type& target_type, variant& target_var) const
{
    if (!is_valid())
        return false;

    bool ok = false;

    const type source_type = get_type();
    const bool source_is_arithmetic = source_type.is_arithmetic();
    const bool target_is_arithmetic = target_type.is_arithmetic();
    const type string_type = type::get<std::string>();
    if (target_type == source_type)
    {
        target_var = *this;
        return true; // the current variant is already the target type, we don't need to do anything
    }
    else if (!source_type.is_wrapper() && target_type.is_wrapper() &&
             target_type.get_wrapped_type() == source_type)
    {
        target_var = create_wrapped_value(target_type);
        ok = target_var.is_valid();
    }
    else if (source_type.is_wrapper() && !target_type.is_wrapper())
    {
        variant var = extract_wrapped_value();
        ok = var.convert(target_type);
        target_var = var;
    }
    else if ((source_is_arithmetic && target_is_arithmetic) ||
            (source_is_arithmetic && target_type == string_type) ||
            (source_type == string_type && target_is_arithmetic) ||
            (source_type.is_enumeration() && target_is_arithmetic) ||
            (source_type.is_enumeration() && target_type == string_type))
    {
        if (target_type == type::get<bool>())
        {
            bool value;
            if ((ok = try_basic_type_conversion(value)) == true)
                target_var = value;
        }
        else if (target_type == type::get<char>())
        {
            char value;
            if ((ok = try_basic_type_conversion(value)) == true)
                target_var = value;
        }
        else if (target_type == type::get<int8_t>())
        {
            int8_t value;
            if ((ok = try_basic_type_conversion(value)) == true)
                target_var = value;
        }
        else if (target_type == type::get<int16_t>())
        {
            int16_t value;
            if ((ok = try_basic_type_conversion(value)) == true)
                target_var = value;
        }
        else if (target_type == type::get<int32_t>())
        {
            int32_t value;
            if ((ok = try_basic_type_conversion(value)) == true)
                target_var = value;
        }
        else if (target_type == type::get<int64_t>())
        {
            int64_t value;
            if ((ok = try_basic_type_conversion(value)) == true)
                target_var = value;
        }
        else if (target_type == type::get<uint8_t>())
        {
            uint8_t value;
            if ((ok = try_basic_type_conversion(value)) == true)
                target_var = value;
        }
        else if (target_type == type::get<uint16_t>())
        {
            uint16_t value;
            if ((ok = try_basic_type_conversion(value)) == true)
                target_var = value;
        }
        else if (target_type == type::get<uint32_t>())
        {
            uint32_t value;
            if ((ok = try_basic_type_conversion(value)) == true)
                target_var = value;
        }
        else if (target_type == type::get<uint64_t>())
        {
            uint64_t value;
            if ((ok = try_basic_type_conversion(value)) == true)
                target_var = value;
        }
        else if (target_type == type::get<float>())
        {
            float value;
            if ((ok = try_basic_type_conversion(value)) == true)
                target_var = value;
        }
//.........这里部分代码省略.........
开发者ID:Victorique-GOSICK,项目名称:rttr,代码行数:101,代码来源:variant.cpp


注:本文中的variant::is_valid方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。