/* 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; }
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; }
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); }
corto_native_type _corto_native_type__create(corto_object _parent, const char *_id, const char * name, bool is_ptr) { corto_native_type _this; _this = (corto_native_type)corto_declare(_parent, _id, corto_native_type_o); if (!_this) { return NULL; } if (!corto_check_state(_this, CORTO_VALID)) { corto_set_str(&((corto_native_type)_this)->name, name); ((corto_native_type)_this)->is_ptr = is_ptr; if (corto_define(_this)) { corto_release(_this); _this = NULL; } } return _this; }
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; }
/* Create instance of Fibo class */ corto_mount createMountInstance(corto_class mountClass) { corto_mount connector = corto_declareChild(root_o, "fibo", mountClass); if (!connector) { goto error; } /* Return data in the corto string format */ corto_setstr(&corto_subscriber(connector)->contentType, "text/corto"); if (corto_define(connector)) { goto error; } return connector; error: return NULL; }
static corto_string corto_string_parseAnonymous( corto_string str, corto_string value, struct corto_string_deserIndexInfo *info, corto_string_deser_t *data) { corto_object o = NULL; char *ptr = str; char *valuePtr = value; corto_uint32 index = 0; void *offset; if (corto_string_getDestinationPtr(info, data, &offset)) { corto_seterr("%s: %s", str, corto_lasterr()); goto error; } /* Check if this is a 'named' anonymous object in which case it is * prefixed with <[id]> */ if ((valuePtr = corto_string_deserParseAnonymousId(valuePtr, &index))) { if (data->anonymousObjects && (index <= corto_ll_size(data->anonymousObjects))) { if (!*valuePtr) { o = corto_ll_get(data->anonymousObjects, index - 1); corto_claim(o); } else { /* If the <n> is followed up with more data, a new anonymous * object is being created while one existed already with the * specified number */ corto_seterr("identifier <%d> is already defined (%s)", index, value); goto error; } } } else { valuePtr = value; } /* If no object has been referenced, create an anonymous object */ if (!o && *valuePtr) { corto_object type = corto_resolve(NULL, valuePtr); if (!type) { corto_seterr("unresolved type '%s'", valuePtr); goto error; } if (offset) { o = *(corto_object*)offset; if (!o || (type != corto_typeof(o))) { if (!corto_instanceof(corto_type_o, type)) { corto_seterr("'%s' is not a type", corto_fullpath(NULL, type)); goto error; } o = corto_declare(type); if (!o) { corto_seterr("failed to declare %s: %s", value, corto_lasterr()); goto error; } } else { corto_claim(o); } } corto_string_deser_t privateData = { .out = data->out, // Preserve original out to determine ownership .ptr = o, .type = type, .anonymousObjects = data->anonymousObjects, .scope = data->scope, .isObject = data->isObject }; if (corto_type(type)->kind == CORTO_PRIMITIVE) { ptr ++; if (!(ptr = corto_string_deserParse(ptr, NULL, &privateData))) { goto error; } } else { if (!(ptr = corto_string_deserParseScope(ptr, NULL, &privateData))) { goto error; } } if (o) { if (corto_define(o)) { goto error; } data->anonymousObjects = privateData.anonymousObjects; if (!data->anonymousObjects) { data->anonymousObjects = corto_ll_new(); } corto_ll_append(data->anonymousObjects, o); } } if (offset) corto_ptr_setref(offset, o); if (o) corto_release(o); return ptr; error: if (o) corto_release(o); return NULL; }
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; }