corto_void _test_compositeApi_tc_define( test_compositeApi _this) { /* $begin(test/compositeApi/tc_define) */ corto_int16 result; test_compositeType *o = test_compositeTypeDeclareChild(root_o, "o"); test_assert(o != NULL); test_assert(corto_typeof(o) == corto_type(test_compositeType_o)); test_assert(!corto_checkState(o, CORTO_DEFINED)); test_assert(corto_checkAttr(o, CORTO_ATTR_SCOPED)); test_assert(corto_parentof(o) == root_o); test_assert(!strcmp(corto_idof(o), "o")); test_assert(o->x == 1); test_assert(o->y == 2); result = test_compositeTypeDefine(o, 10, 20); test_assert(result == 0); test_assert(corto_checkState(o, CORTO_DEFINED)); test_assert(o->x == 10); test_assert(o->y == 20); test_assert(!corto_delete(o)); /* $end */ }
corto_void _test_compositeUnionApi_tc_defineDefault( test_compositeUnionApi _this) { /* $begin(test/compositeUnionApi/tc_defineDefault) */ corto_int16 result; test_compositeUnionType *o = test_compositeUnionTypeDeclareChild(root_o, "o"); test_assert(o != NULL); test_assert(corto_typeof(o) == corto_type(test_compositeUnionType_o)); test_assert(!corto_checkState(o, CORTO_DEFINED)); test_assert(corto_checkAttr(o, CORTO_ATTR_SCOPED)); test_assert(corto_parentof(o) == root_o); test_assert(!strcmp(corto_idof(o), "o")); test_assert(o->d == 2); test_assertstr(o->is.str, "foo"); result = test_compositeUnionTypeDefine_other(o, 4, 10); test_assert(result == 0); test_assert(corto_checkState(o, CORTO_DEFINED)); test_assert(o->d == 4); test_assert(o->is.other == 10); test_assert(!corto_delete(o)); /* $end */ }
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; }
static corto_string doc_parse(corto_string input) { corto_string result = NULL; corto_function f = corto_function(corto_resolve(NULL, "md::parse")); if (corto_checkState(f, CORTO_DEFINED)) { corto_call(f, &result, input); } return result; }
corto_void _test_OptionalStringApi_tc_declare( test_OptionalStringApi _this) { /* $begin(test/OptionalStringApi/tc_declare) */ test_OptionalString *o = test_OptionalStringDeclare(); test_assert(o != NULL); test_assert(!corto_checkState(o, CORTO_DEFINED)); test_assert(o->a == NULL); test_assert(o->b == NULL); /* $end */ }
corto_void _test_voidReferenceApi_tc_createAuto( test_voidReferenceApi _this) { /* $begin(test/voidReferenceApi/tc_createAuto) */ test_voidReferenceTypeCreate_auto(o); test_assert(o != NULL); test_assert(corto_typeof(o) == test_voidReferenceType_o); test_assert(corto_checkState(o, CORTO_DEFINED)); test_assert(!corto_delete(o)); /* $end */ }
corto_void _test_compositeUnionApi_tc_createAutoDefault( test_compositeUnionApi _this) { /* $begin(test/compositeUnionApi/tc_createAutoDefault) */ test_compositeUnionTypeCreate_other_auto(o, 4, 10); test_assert(o != NULL); test_assert(corto_typeof(o) == corto_type(test_compositeUnionType_o)); test_assert(corto_checkState(o, CORTO_DEFINED)); test_assert(o->d == 4); test_assert(o->is.other == 10); test_assert(!corto_delete(o)); /* $end */ }
corto_void _test_compositeUnionApi_tc_declareAuto( test_compositeUnionApi _this) { /* $begin(test/compositeUnionApi/tc_declareAuto) */ test_compositeUnionTypeDeclare_auto(o); test_assert(o != NULL); test_assert(corto_typeof(o) == corto_type(test_compositeUnionType_o)); test_assert(!corto_checkState(o, CORTO_DEFINED)); test_assert(o->d == 2); test_assertstr(o->is.str, "foo"); test_assert(!corto_delete(o)); /* $end */ }
corto_void _test_compositeApi_tc_createAuto( test_compositeApi _this) { /* $begin(test/compositeApi/tc_createAuto) */ test_compositeTypeCreate_auto(o, 10, 20); test_assert(o != NULL); test_assert(corto_typeof(o) == corto_type(test_compositeType_o)); test_assert(corto_checkState(o, CORTO_DEFINED)); test_assert(o->x == 10); test_assert(o->y == 20); test_assert(!corto_delete(o)); /* $end */ }
corto_void _test_compositeApi_tc_declare( test_compositeApi _this) { /* $begin(test/compositeApi/tc_declare) */ test_compositeType *o = test_compositeTypeDeclare(); test_assert(o != NULL); test_assert(corto_typeof(o) == corto_type(test_compositeType_o)); test_assert(!corto_checkState(o, CORTO_DEFINED)); test_assert(o->x == 1); test_assert(o->y == 2); test_assert(!corto_delete(o)); /* $end */ }
corto_void _test_compositeUnionApi_tc_create( test_compositeUnionApi _this) { /* $begin(test/compositeUnionApi/tc_create) */ test_compositeUnionType *o = test_compositeUnionTypeCreate_flt(1, 10.5); test_assert(o != NULL); test_assert(corto_typeof(o) == corto_type(test_compositeUnionType_o)); test_assert(corto_checkState(o, CORTO_DEFINED)); test_assert(o->d == 1); test_assert(o->is.flt == 10.5); test_assert(!corto_delete(o)); /* $end */ }
corto_void _test_voidReferenceApi_tc_createChildAuto( test_voidReferenceApi _this) { /* $begin(test/voidReferenceApi/tc_createChildAuto) */ test_voidReferenceTypeCreateChild_auto(root_o, o); test_assert(o != NULL); test_assert(corto_typeof(o) == test_voidReferenceType_o); test_assert(corto_checkState(o, CORTO_DEFINED)); test_assert(corto_checkAttr(o, CORTO_ATTR_SCOPED)); test_assert(corto_parentof(o) == root_o); test_assert(!strcmp(corto_idof(o), "o")); test_assert(!corto_delete(o)); /* $end */ }
corto_void _test_OptionalStringApi_tc_declareChild( test_OptionalStringApi _this) { /* $begin(test/OptionalStringApi/tc_declareChild) */ test_OptionalString *o = test_OptionalStringDeclareChild(root_o, "a"); test_assert(o != NULL); test_assert(!corto_checkState(o, CORTO_DEFINED)); test_assert(corto_checkAttr(o, CORTO_ATTR_SCOPED)); test_assertstr(corto_idof(o), "a"); test_assert(corto_parentof(o) == root_o); test_assert(o->a == NULL); test_assert(o->b == NULL); /* $end */ }
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; }
corto_void _test_OptionalStringApi_tc_defineNotSet( test_OptionalStringApi _this) { /* $begin(test/OptionalStringApi/tc_defineNotSet) */ test_OptionalString *o = test_OptionalStringDeclare(); test_assert(o != NULL); test_assert(!corto_checkState(o, CORTO_DEFINED)); test_assert(o->a == NULL); test_assert(o->b == NULL); test_OptionalStringDefine(o, NotSet, "Bar"); test_assert(o->a == NULL); test_assertstr(o->b, "Bar"); /* $end */ }
corto_void _test_compositeUnionApi_tc_declareChildAuto( test_compositeUnionApi _this) { /* $begin(test/compositeUnionApi/tc_declareChildAuto) */ test_compositeUnionTypeDeclareChild_auto(root_o, o); test_assert(o != NULL); test_assert(corto_typeof(o) == corto_type(test_compositeUnionType_o)); test_assert(!corto_checkState(o, CORTO_DEFINED)); test_assert(corto_checkAttr(o, CORTO_ATTR_SCOPED)); test_assert(corto_parentof(o) == root_o); test_assert(!strcmp(corto_idof(o), "o")); test_assert(o->d == 2); test_assertstr(o->is.str, "foo"); test_assert(!corto_delete(o)); /* $end */ }
corto_void _test_compositeApi_tc_createChildAuto( test_compositeApi _this) { /* $begin(test/compositeApi/tc_createChildAuto) */ test_compositeTypeCreateChild_auto(root_o, o, 10, 20); test_assert(o != NULL); test_assert(corto_typeof(o) == corto_type(test_compositeType_o)); test_assert(corto_checkState(o, CORTO_DEFINED)); test_assert(corto_checkAttr(o, CORTO_ATTR_SCOPED)); test_assert(corto_parentof(o) == root_o); test_assert(!strcmp(corto_idof(o), "o")); test_assert(o->x == 10); test_assert(o->y == 20); test_assert(!corto_delete(o)); /* $end */ }
corto_void _test_compositeUnionApi_tc_createChildAutoDefault( test_compositeUnionApi _this) { /* $begin(test/compositeUnionApi/tc_createChildAutoDefault) */ test_compositeUnionTypeCreateChild_other_auto( root_o, o, 4, 10); test_assert(o != NULL); test_assert(corto_typeof(o) == corto_type(test_compositeUnionType_o)); test_assert(corto_checkState(o, CORTO_DEFINED)); test_assert(corto_checkAttr(o, CORTO_ATTR_SCOPED)); test_assert(corto_parentof(o) == root_o); test_assert(!strcmp(corto_idof(o), "o")); test_assert(o->d == 4); test_assert(o->is.other == 10); test_assert(!corto_delete(o)); /* $end */ }
corto_void _test_compositeUnionApi_tc_createChild( test_compositeUnionApi _this) { /* $begin(test/compositeUnionApi/tc_createChild) */ test_compositeUnionType *o = test_compositeUnionTypeCreateChild_flt( root_o, "o", 1, 10.5); test_assert(o != NULL); test_assert(corto_typeof(o) == corto_type(test_compositeUnionType_o)); test_assert(corto_checkState(o, CORTO_DEFINED)); test_assert(corto_checkAttr(o, CORTO_ATTR_SCOPED)); test_assert(corto_parentof(o) == root_o); test_assert(!strcmp(corto_idof(o), "o")); test_assert(o->d == 1); test_assert(o->is.flt == 10.5); test_assert(!corto_delete(o)); /* $end */ }
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 */ }
static void mqtt_onMessage( struct mosquitto *client, void *data, const struct mosquitto_message *msg) { corto_id nameBuffer; char *name = nameBuffer; corto_object o = NULL; mqtt_Connector this = data; corto_bool isDelete = FALSE; /* If the payload has been serialized as a corto string, the typename is * potentially prefixed to the value */ char *valueStr = strchr(msg->payload, '{'); /* mqtt is the owner of this thread. This ensures that all subsequent create * update / delete actions are performed with the right owner. Ownership * ensures to not trigger on own updates, and to only forward data from * other connectors (or the application). */ corto_object prevOwner = corto_setOwner(this); /* Remove topic from name, so that name is relative to mount point. */ strcpy(name, msg->topic); if (this->topic) { name += strlen(this->topic) + 1; } char *lastElem = strrchr(name, '/'); if (lastElem && !strcmp(lastElem, "/_d")) { *lastElem = '\0'; isDelete = TRUE; } corto_debug("mqtt: %s: received '%s'", msg->topic, msg->payload); /* If object doesn't yet exist in the store, create it */ if (!(o = corto_lookup(corto_mount(this)->mount, name)) && !isDelete) { corto_id buffer; corto_debug("mqtt: creating new object for '%s'", name); /* If the mount has been configured with a fixed type, use that type to * create a new object. Otherwise, look for type in payload. */ if (corto_observer(this)->type) { strcpy(buffer, corto_observer(this)->type); } else { char *typeStr = strchr(msg->payload, '{'); memcpy(buffer, msg->payload, typeStr - (char*)msg->payload); buffer[typeStr - (char*)msg->payload] = '\0'; } /* Resolve type. If type wasn't yet loaded in corto, corto_resolve will * do a lookup on the package repository. If it doesn't exist there * either throw an error. Currently, the MQTT connector does not align * types. */ corto_type type = corto_resolve(NULL, buffer); if (!type) { corto_error("mqtt: type '%s' not found", buffer); goto error; } corto_debug("mqtt: creating '%s' with type '%s'", name, buffer); /* Create a new object under the mountpoint. The name is derived from * the MQTT topic name. */ o = corto_declareChild(corto_mount(this)->mount, name, type); if (!o) { corto_error("mqtt: failed to create object '%s'", name); goto error; } } else { corto_debug("mqtt: found '%s' for '%s'", corto_fullpath(NULL, o), name); } /* Only continue updating object when it is owned by mqtt */ if (o && corto_owned(o)) { if (isDelete) { if (corto_delete(o)) { corto_error("mqtt: failed to delete '%s': %s", name, corto_lasterr()); } corto_release(o); } else { /* Start updating object (takes a writelock) */ if (!corto_updateBegin(o)) { /* Serialize value from JSON string */ if (corto_fromcontent(o, "text/json", valueStr)) { corto_error("mqtt: failed to deserialize for %s: %s (%s)\n", name, corto_lasterr(), msg->payload); /* If deserialization fails, cancel the update. No notification * will be sent. */ corto_updateCancel(o); goto error; } /* Successful update. Send notification and unlock object */ if (corto_updateEnd(o)) { corto_error("mqtt: failed to update '%s': %s", name, corto_lasterr()); goto error; } } else { /* For some reason, couldn't start updating object */ corto_error("mqtt: failed to start updating '%s': %s", name, corto_lasterr()); goto error; } } } else if (o) { corto_debug("mqtt: '%s' not owned by me (%s, defined = %d), ignoring", corto_fullpath(NULL, o), corto_ownerof(o) ? corto_fullpath(NULL, o) : "local", corto_checkState(o, CORTO_DEFINED)); } error: /* Restore previous owner */ corto_setOwner(prevOwner); }
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; }