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


C++ Ptr::call方法代码示例

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


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

示例1:

TEST(GTestFunction, TestCall) {
    Function::Ptr add = GTestFunctionAdd::create();
    Function::Ptr addx2 = GTestFunctionAddx2::create();

    ASSERT_TRUE(add->call().instanceof(Type<Error>::id()));
    ASSERT_TRUE(add->call(1).equals(1));
    ASSERT_TRUE(add->call(1, 2).equals(3));
    ASSERT_TRUE(add->call(1, 2, 3).equals(6));
    ASSERT_TRUE(add->call(1, 2, 3, 4).equals(10));
    ASSERT_TRUE(add->call(1, 2, 3, 4, 5).equals(15));
    ASSERT_TRUE(add->call(1, 2, 3, 4, 5, 6).equals(21));
    ASSERT_TRUE(add->call(1, 2, 3, 4, 5, 6, 7).equals(28));
    ASSERT_TRUE(add->call(1, 2, 3, 4, 5, 6, 7, 8).equals(36));
    ASSERT_TRUE(add->call(1, 2, 3, 4, 5, 6, 7, 8, 9).equals(45));
    ASSERT_TRUE(addx2->call(1, 2, 3, 4, 5, 6, 7, 8, 9).equals(90));
}
开发者ID:louisyoo,项目名称:libj,代码行数:16,代码来源:gtest_function.cpp

示例2: lhs

    Variant& StackMachine::evaluate(const VariableStore& store, const FunctionRegistry& functions)
    {
        reset();

        for(const auto& instruction : _instructions) {
            switch(instruction._opCode) {
                case NOP: {
                    break;
                }
                case PUSH: {
                    _valueStack.emplace(instruction._value);
                    break;
                }
                case PUSHVAR: {
                    if(instruction._value.getType() != INT) {
                        CSVSQLDB_THROW(StackMachineException, "expected an INT as variable index");
                    }

                    int64_t index = instruction._value.asInt();
                    _valueStack.emplace(store[static_cast<size_t>(index)]);
                    break;
                }
                case ADD:
                case SUB:
                case DIV:
                case MOD:
                case MUL:
                case EQ:
                case NEQ:
                case IS:
                case ISNOT:
                case GT:
                case GE:
                case LT:
                case LE:
                case AND:
                case OR:
                case CONCAT: {
                    const Variant lhs(getNextValue());
                    Variant& rhs(getTopValue());
                    rhs = binaryOperation(mapOpCodeToBinaryOperationType(instruction._opCode), lhs, rhs);
                    break;
                }
                case NOT: {
                    Variant& rhs(getTopValue());
                    rhs = unaryOperation(OP_NOT, BOOLEAN, rhs);
                    break;
                }
                case PLUS: {
                    // this is a nop, as the value will not change, so just leave it on the stack
                    break;
                }
                case MINUS: {
                    Variant& rhs = getTopValue();
                    rhs = unaryOperation(OP_MINUS, rhs.getType(), rhs);
                    break;
                }
                case BETWEEN: {
                    const Variant lhs = getNextValue();
                    const Variant from = getNextValue();
                    Variant& to = getTopValue();

                    Variant result(BOOLEAN);
                    if(not(lhs.isNull() || from.isNull() || to.isNull())) {
                        if(binaryOperation(OP_GE, to, from).asBool()) {
                            result = binaryOperation(OP_GE, lhs, from);
                            if(result.asBool()) {
                                result = binaryOperation(OP_LE, lhs, to);
                            }
                        } else {
                            result = binaryOperation(OP_GE, lhs, to);
                            if(result.asBool()) {
                                result = binaryOperation(OP_LE, lhs, from);
                            }
                        }
                    }
                    to = result;
                    break;
                }
                case FUNC: {
                    if(instruction._value.getType() != STRING) {
                        CSVSQLDB_THROW(StackMachineException, "expected a string as variable name");
                    }

                    std::string funcname = instruction._value.asString();
                    Function::Ptr func = functions.getFunction(funcname);
                    if(!func) {
                        CSVSQLDB_THROW(StackMachineException, "function '" << funcname << "' not found");
                    }
                    Variants parameter;
                    size_t count = func->getParameterTypes().size();
                    for(const auto& param : func->getParameterTypes()) {
                        Variant v = getNextValue();
                        if(param != v.getType()) {
                            try {
                                v = unaryOperation(OP_CAST, param, v);
                            } catch(const std::exception&) {
                                CSVSQLDB_THROW(StackMachineException,
                                               "calling function '" << funcname << "' with wrong parameter");
                            }
//.........这里部分代码省略.........
开发者ID:bbannier,项目名称:csvsqldb,代码行数:101,代码来源:stack_machine.cpp


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