Пример #1
0
corto_int16 json_deserReference(void* p, corto_type t, JSON_Value* v)
{
    switch(json_value_get_type(v)) {
    case JSONString: {
        const char* reference = json_value_get_string(v);
        corto_object o = corto_resolve(NULL, (corto_string)reference);
        if (!o) {
            corto_error("unresolved reference \"%s\"", reference);
            goto error;
        }

        if (!corto_instanceof(t, o)) {
            corto_error("%s is not an instance of %s", reference, corto_idof(t));
        }

        corto_setref(p, o);
        corto_release(o);
        break;
    }
    case JSONObject: {
        JSON_Object* obj = json_value_get_object(v);

        JSON_Value* type = json_object_get_value(obj, "type");
        if (json_value_get_type(type) != JSONString) {
            corto_seterr("type parameter of anonymous object must be a string");
            goto error;
        }
        corto_type cortoType = corto_resolve(NULL, (char*)json_value_get_string(type));
        if (!cortoType) {
            corto_seterr("type '%s' not found for anonymous object", json_value_get_string(type));
            goto error;
        }

        corto_object cortoObj = *(corto_object*)p;
        if (!cortoObj || (corto_typeof(cortoObj) != cortoType)) {
            cortoObj = corto_create(cortoType);
            corto_setref(p, cortoObj);
            corto_release(cortoObj);
        }
        corto_release(cortoType);

        JSON_Value* value = json_object_get_value(obj, "value");
        if (json_deserType(cortoObj, cortoType, value)) {
            goto error;
        }
        break;
    }
    case JSONNull:
        corto_setref(p, NULL);
        break;
    default:
        corto_seterr("expected string, null or object (reference), got %s", json_valueTypeToString(v));
        break;
    }

    return 0;
error:
    return -1;
}
Пример #2
0
int dynamic_structMain(int argc, char *argv[]) {

    /* Create a new struct */
    corto_struct Point = corto_declareChild(root_o, "Point", corto_struct_o);
    if (!Point) {
        goto error;
    }

    /* Create x member */
    corto_member x = corto_declareChild(Point, "x", corto_member_o);
    if (!x) {
        goto error;
    }
    if (!corto_checkState(x, CORTO_DEFINED)) {
        corto_setref(&x->type, corto_int32_o);
        if (corto_define(x)) {
            goto error;
        }
    }

    /* Create y member */
    corto_member y = corto_declareChild(Point, "y", corto_member_o);
    if (!y) {
        goto error;
    }
    if (!corto_checkState(y, CORTO_DEFINED)) {
        corto_setref(&y->type, corto_int32_o);
        if (corto_define(y)) {
            goto error;
        }
    }

    /* Finalize struct */
    if (corto_define(Point)) {
        goto error;
    }

    /* Create an instance of Point */
    corto_object p = corto_createChild(root_o, "p", Point);
    if (!p) {
        goto error;
    }

    *(corto_int32*)CORTO_OFFSET(p, x->offset) = 10;
    *(corto_int32*)CORTO_OFFSET(p, y->offset) = 20;

    printf("p = %s\n", corto_contentof(NULL, "text/corto", p));

    return 0;
error:
    corto_error("error: %s", corto_lasterr());
    return -1;
}
Пример #3
0
md_Doc _md_DocAssign(md_Doc _this, corto_object o, corto_uint8 level, corto_string niceName) {
    CORTO_UNUSED(_this);
    corto_setref(&((md_Doc)_this)->o, o);
    ((md_Doc)_this)->level = level;
    corto_setstr(&((md_Doc)_this)->niceName, niceName);
    return _this;
}
Пример #4
0
corto_int16 _md_DocDefine(md_Doc _this, corto_object o, corto_uint8 level, corto_string niceName) {
    CORTO_UNUSED(_this);
    corto_setref(&((md_Doc)_this)->o, o);
    ((md_Doc)_this)->level = level;
    corto_setstr(&((md_Doc)_this)->niceName, niceName);
    return corto_define(_this);
}
Пример #5
0
/* Create a class that mounts fibonacci numbers in corto */
corto_class createFiboClass(void) {
    corto_class FiboClass = corto_declareChild(root_o, "FiboConnector", corto_class_o);
    if (!FiboClass) {
        goto error;
    }

    /* Inherit from the corto mount class */
    corto_setref(&corto_interface(FiboClass)->base, corto_mount_o);

    /* Create onNotify method (called when the mount receives an update) */
    corto_method onRequestMethod = corto_declareChild(
        FiboClass,
        "onRequest(core/request request)",
        corto_method_o);
    if (!onRequestMethod) {
        goto error;
    }

    corto_function(onRequestMethod)->kind = CORTO_PROCEDURE_CDECL;
    corto_function(onRequestMethod)->fptr = (corto_word)onRequest;

    if (corto_define(onRequestMethod)) {
        goto error;
    }

    /* Finalize class */
    if (corto_define(FiboClass)) {
        goto error;
    }

    return FiboClass;
error:
    return NULL;
}
Пример #6
0
corto_int16 _md_DocUpdate(md_Doc _this, corto_object o, corto_uint8 level, corto_string niceName) {
    CORTO_UNUSED(_this);
    if (!corto_updateBegin(_this)) {
        if ((corto_typeof(corto_typeof(_this)) == (corto_type)corto_target_o) && !corto_owned(_this)) {
            corto_setref(&((md_Doc)((md_Doc)CORTO_OFFSET(_this, ((corto_type)md_Doc_o)->size)))->o, o);
            ((md_Doc)((md_Doc)CORTO_OFFSET(_this, ((corto_type)md_Doc_o)->size)))->level = level;
            corto_setstr(&((md_Doc)((md_Doc)CORTO_OFFSET(_this, ((corto_type)md_Doc_o)->size)))->niceName, niceName);
        } else {
            corto_setref(&((md_Doc)_this)->o, o);
            ((md_Doc)_this)->level = level;
            corto_setstr(&((md_Doc)_this)->niceName, niceName);
        }
        corto_updateEnd(_this);
    } else {
        return -1;
    }
    return 0;
}
Пример #7
0
corto_int16 _corto_delegate_bind(corto_function object) {
/* $begin(::corto::lang::delegate::bind) */
    corto_object parent = corto_parentof(object);

    if (corto_class_instanceof(corto_interface_o, corto_typeof(parent))) {
        corto_interface type = corto_interface(corto_typeof(parent));
        corto_id functionName;
        corto_member m = NULL;

        /* Get function name, lookup delegate, assign function */
        corto_signatureName(corto_nameof(object), functionName);
        if (corto_checkState(corto_type_o, CORTO_DEFINED) && (m = corto_interface_resolveMember(type, functionName)) &&
            (m->type->kind == CORTO_COMPOSITE) && (corto_interface(m->type)->kind == CORTO_DELEGATE)) {
            if (corto_delegate_instanceof(corto_delegate(m->type), object)) {
                /* Bind instance of function is a method */
                if (corto_procedure(corto_typeof(object))->kind == CORTO_METHOD) {
                    corto_setref(&((corto_delegatedata *) CORTO_OFFSET(parent, m->offset))->instance, parent);
                }
                /* Bind procedure */
                corto_setref(&((corto_delegatedata *) CORTO_OFFSET(parent, m->offset))->procedure, object);    
            } else {
                /* If there is a member that corresponds to a delegate but has a non matching
                 * signature, always report error */
                corto_id id1, id2;
                corto_error("member '%s' of delegate type '%s' does not match signature of '%s'",
                    corto_nameof(m), corto_fullname(m->type, id1), corto_fullname(object, id2));
                goto error;
            }
        }
    }

    return 0;
error:
    return -1;
/* $end */
}
Пример #8
0
md_Doc _md_DocCreate(corto_object o, corto_uint8 level, corto_string niceName) {
    md_Doc _this;
    _this = md_Doc(corto_declare(md_Doc_o));
    if (!_this) {
        return NULL;
    }
    if (!corto_checkState(_this, CORTO_DEFINED)) {
        corto_setref(&((md_Doc)_this)->o, o);
        ((md_Doc)_this)->level = level;
        corto_setstr(&((md_Doc)_this)->niceName, niceName);
        if (corto_define(_this)) {
            corto_release(_this);
            _this = NULL;
        }
    }
    return _this;
}
Пример #9
0
int select_contentTypeMain(int argc, char *argv[]) {

    /* Create a Point type, so we have something to serialize to JSON */
    corto_struct Point = corto_declareChild(root_o, "Point", corto_struct_o);
    if (!Point) {
        goto error;
    }

    /* Create x member */
    corto_member x = corto_declareChild(Point, "x", corto_member_o);
    if (!x) {
        goto error;
    }
    if (!corto_checkState(x, CORTO_DEFINED)) {
        corto_setref(&x->type, corto_int32_o);
        if (corto_define(x)) {
            goto error;
        }
    }

    /* Create y member */
    corto_member y = corto_declareChild(Point, "y", corto_member_o);
    if (!y) {
        goto error;
    }
    if (!corto_checkState(y, CORTO_DEFINED)) {
        corto_setref(&y->type, corto_int32_o);
        if (corto_define(y)) {
            goto error;
        }
    }

    /* Finalize Point struct */
    if (corto_define(Point)) {
        goto error;
    }

    /* Create two instances of Point. */
    corto_object p1 = corto_declareChild(root_o, "p1", Point);
    if (!p1) {
        goto error;
    }

    if (!corto_checkState(p1, CORTO_DEFINED)) {
        *(corto_int32*)CORTO_OFFSET(p1, x->offset) = 10;
        *(corto_int32*)CORTO_OFFSET(p1, y->offset) = 10;
        if (corto_define(p1)) {
            goto error;
        }
    }

    corto_object p2 = corto_declareChild(root_o, "p2", Point);
    if (!p2) {
        goto error;
    }

    if (!corto_checkState(p2, CORTO_DEFINED)) {
        *(corto_int32*)CORTO_OFFSET(p2, x->offset) = 20;
        *(corto_int32*)CORTO_OFFSET(p2, y->offset) = 30;
        if (corto_define(p2)) {
            goto error;
        }
    }

    /* Select all instances of type Point, get value in JSON */
    corto_iter it;
    corto_int16 ret = corto_select("/", "*")
      .contentType("text/json")
      .type("/Point")
      .iter(&it);
    if (ret) {
        goto error;
    }

    while (corto_iterHasNext(&it)) {
        corto_result *r = corto_iterNext(&it);
        printf("id: %s, value: %s\n", r->id, corto_result_getText(r));
    }

    return 0;
error:
    corto_error("error: %s", corto_lasterr());
    return -1;
}