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 */ }
static corto_int16 corto_ser_initObservable( corto_walk_opt* s, corto_value* v, void* userData) { corto_member m = v->is.member.member; int create_mask = CORTO_DECLARE|CORTO_FORCE_TYPE|CORTO_DEFINE; /* Initialize member to a new object of member type */ corto_type t = corto_value_typeof(v); corto_object p = corto_value_objectof(v); void* ptr = corto_value_ptrof(v); if ((m->modifiers & CORTO_SINGLETON) == CORTO_SINGLETON) { /* If member is a singleton, create observable member in the scope of * the composite type */ p = corto_parentof(t); } else { /* If this is a regular observable member, create object as orphan */ create_mask |= CORTO_ORPHAN; } /* Create observable that is not added to the scope of its parent */ corto_object o = corto(create_mask, { .parent = p, .id = corto_idof(m), .type = t });
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 */ }
corto_bool json_deserMustSkip(corto_member m, void *ptr) { if (corto_instanceof(corto_target_o, corto_parentof(m))) { corto_bool owned = corto_owned(ptr); corto_bool isActual = !strcmp("actual", corto_idof(m)); if ((owned && !isActual) || (!owned && isActual)) { return TRUE; } } return FALSE; }
/* * Returns TRUE if `ancestor` is recursively a parent of `o`, except if * `ancestor` is root_o. */ static corto_bool md_isAncestor(corto_object ancestor, corto_object o) { corto_bool isAncestor = FALSE; corto_object p = o; while (p && !isAncestor) { p = corto_parentof(p); if (p == ancestor) { isAncestor = TRUE; } } return isAncestor; }
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 */ }
corto_void _test_OptionalStringApi_tc_createChildNotSet( test_OptionalStringApi _this) { /* $begin(test/OptionalStringApi/tc_createChildNotSet) */ test_OptionalString *o = test_OptionalStringCreateChild( root_o, "a", NotSet, "Bar"); test_assert(o != NULL); 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_assertstr(o->b, "Bar"); /* $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_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_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 */ }
static corto_int16 cpp_fluent_visitMember(corto_serializer s, corto_value *info, void *userData) { cpp_classWalk_t *data = userData; corto_type t = corto_value_getType(info); corto_member m = info->is.member.t; corto_id id, cId; cpp_typeId(data->g, t, Cpp_Return, Cpp_ByVal, id); cpp_typeId(data->g, corto_parentof(m), Cpp_Return, Cpp_ByCRef, cId); /* Write implementation */ g_fileWrite(data->header, "T& %s(%s value)", corto_idof(m), id); g_fileWrite(data->header, "{ ((%s)this->m_ptr)->%s = value; return this->m_this; }\n", cId, corto_idof(m)); return 0; }
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 */ }
/* $CORTO_GENERATED * * DDP_Subscription.c * * Only code written between the begin and end tags will be preserved * when the file is regenerated. */ #include <corto/web/server/server.h> /* $header() */ #define SERVER_DDP_SUBSCRIPTION_READY "{\"msg\":\"ready\",\"subs\":[\"%s\"]}" server_DDP_Session server_DDP_Subscription_getSession(server_DDP_Subscription this) { return server_DDP_Session(corto_parentof(corto_parentof(this))); } /* $end */ corto_void _server_DDP_Subscription_ready( server_DDP_Subscription this) { /* $begin(corto/web/server/DDP/Subscription/ready) */ server_DDP_Session session = server_DDP_Subscription_getSession(this); corto_string msg = NULL; corto_asprintf(&msg, SERVER_DDP_SUBSCRIPTION_READY, this->id); server_HTTP_Connection_write(session->conn, msg); corto_dealloc(msg);
* * This example shows basic usage of core API functions to create, update * and delete objects. */ /* Callback for observer */ void onUpdate( corto_object this, corto_eventMask event, corto_object o, corto_observer observer) { /* Show information about the object that triggered the observer */ printf("update received for id='%s' parent='%s' type='%s' value='%d'\n", corto_idof(o), corto_fullpath(NULL, corto_parentof(o)), corto_fullpath(NULL, corto_typeof(o)), *corto_int32(o)); } int objectsMain(int argc, char *argv[]) { /* Create a signed integer object in the global object hierarchy */ corto_int32 *i = corto_createChild(root_o, "i", corto_int32_o); if (!i) { goto error; } /* Observe the object for updates */ corto_observer observer = corto_observe(CORTO_ON_UPDATE, i).callback(onUpdate);
void printInfo(corto_object o) { printf("object '%s' created with parent=%s, type=%s\n", corto_fullpath(NULL, o), corto_fullpath(NULL, corto_parentof(o)), corto_fullpath(NULL, corto_typeof(o))); }
#include "shapes.h" void onUpdate(corto_object this, corto_eventMask event, corto_object shape, corto_observer observer) { printf("Update %s %s = %s\n", corto_idof(corto_parentof(shape)), corto_idof(shape), corto_contentof(NULL, "text/json", shape)); } int shapesMain(int argc, char *argv[]) { if (argc < 3) { printf("Usage: shapes [topic] [color]\n"); printf(" Example: shapes Circle RED\n"); return -1; } /* Connect shape topic */ ospl_Connector connector = ospl_ConnectorCreateChild( root_o, /* create connector in root */ argv[1], /* object name of connector (same as topic) */ NULL, /* store instances in scope of connector */ "/ShapeType", /* type */ NULL, /* default policy */ argv[1], /* topic */ "color" /* keylist */ ); /* Observe updates in connector scope */ corto_observe(CORTO_ON_UPDATE|CORTO_ON_SCOPE, connector).callback(onUpdate); /* Create shape */ ShapeType *s = ShapeTypeDeclareChild(connector, argv[2]);
{ /* $begin(test/Packages/tc_class) */ char text[] = "# test::Package0\n" "## Class0\n" "Description of class\n" ; md_parseToCorto(test_docs_o, text); corto_object h1 = corto_resolve(test_docs_o, "test_Package0"); test_assert(h1 != NULL); test_assert(corto_instanceof(md_Doc_o, h1)); test_assert(md_Doc(h1)->o != NULL); test_assert(!strcmp(corto_idof(md_Doc(h1)->o), "Package0")); test_assert(corto_parentof(md_Doc(h1)->o) == test_o); test_assert(md_Doc(h1)->text == NULL); test_assert(md_Doc(h1)->level == 1); corto_release(h1); corto_object h2 = corto_resolve(test_docs_o, "test_Package0::Class0"); test_assert(h2 != NULL); test_assert(corto_instanceof(md_Doc_o, h2)); test_assert(md_Doc(h2)->o != NULL); test_assert(!strcmp(corto_idof(md_Doc(h2)->o), "Class0")); test_assert(corto_parentof(md_Doc(h2)->o) == md_Doc(h1)->o); test_assert(!strcmp(md_Doc(h2)->description, "Description of class\n")); test_assert(md_Doc(h2)->level == 2); corto_release(h2); /* $end */
* Only code written between the begin and end tags will be preserved * when the file is regenerated. */ #include "corto.h" /* $header() */ #include "corto__class.h" /* $end */ corto_int16 _corto_observer_bind(corto_observer this) { /* $begin(::corto::lang::observer::bind) */ /* If this is a scoped observer, automatically bind with parent if it's a class. */ if (corto_checkAttr(this, CORTO_ATTR_SCOPED)) { if (corto_class_instanceof(corto_class_o, corto_parentof(this))) { corto_class_bindObserver(corto_parentof(this), this); } } corto_function(this)->size = sizeof(corto_object) * 3; /* Check if mask specifies either SELF or CHILDS, if not enable SELF */ if (!(this->mask & (CORTO_ON_SELF|CORTO_ON_SCOPE|CORTO_ON_TREE))) { this->mask |= CORTO_ON_SELF; } /* Check if mask specifies either VALUE or METAVALUE, if not enable VALUE */ if (!((this->mask & CORTO_ON_VALUE) || (this->mask & CORTO_ON_METAVALUE))) { this->mask |= CORTO_ON_VALUE; }
static int html_walkDocChilds(corto_object o, void *userData) { htmlData_t *data = userData; corto_string description = doc_getDescriptionFromDoc(o, data); corto_string text = doc_getTextFromDoc(o, data); corto_string niceName = doc_getNiceNameFromDoc(o, data); corto_object *docs = corto_alloc(sizeof(corto_object) * corto_scopeSize(o)); corto_id index; corto_object stack[CORTO_MAX_SCOPE_DEPTH]; corto_object ptr = o; corto_int32 sp = data->level; corto_id link; corto_bool noIndex = FALSE; corto_id upper; strcpy(upper, niceName); corto_strupper(upper); if (niceName[0] == '#') { noIndex = TRUE; niceName ++; } corto_string parentId = corto_idof(corto_parentof(o)); if (parentId[0] == '#') parentId++; strcpy(link, parentId); strcat(link, "_"); if (corto_idof(o)[0] == '#') { strcat(link, corto_idof(o) + 1); } else { strcat(link, corto_idof(o)); } /* If level is 0, parse heading as title */ if (!data->level) { data->title = niceName; } else { /* Add index number to header */ index[0] = '\0'; if ((sp > 1) && !noIndex) { while (--sp) { stack[sp - 1] = ptr; ptr = corto_parentof(ptr); } for (; sp < data->level - 1; sp ++) { char num[15]; sprintf(num, "%d", doc_getIndexFromDoc(stack[sp], data)); strcat(index, num); strcat(index, "."); } if (data->level > 2) { index[strlen(index) - 1] = '\0'; } strcat(index, " "); } if (data->level == 1) { corto_buffer_append(&data->content, "<a name=\"%s\"></a>\n<p class=\"section\">%s</p>\n", link, niceName); } else { corto_buffer_append(&data->content, "<a name=\"%s\"></a>\n<h%d>%s%s</h%d>\n", link, data->level, index, niceName, data->level); } if (data->level < 3) { corto_buffer_append(&data->index, "<li class=\"index-h%d\" onclick=\"window.location='#%s'\">%s</li>\n", data->level, link, data->level == 1 ? upper : niceName); } corto_buffer_append(&data->content, "<div class=\"indent\">\n"); if (description && strlen(description)) { if (data->level < 3) { corto_buffer_append(&data->content, "<p class=\"description\">%s</p>\n", description); } else { corto_buffer_append(&data->content, "<p>%s</p>\n", description); } } corto_string parsedText = doc_parse(text); corto_buffer_append(&data->content, "%s\n", parsedText); corto_dealloc(parsedText); } /* Collect documents in order */ corto_objectseq seq = corto_scopeClaim(o); corto_objectseqForeach(seq, doc) { docs[doc_getIndexFromDoc(doc, data) - 1] = doc; } corto_scopeRelease(seq); /* Walk documents in order */ data->level ++; int i; for (i = 0; i < corto_scopeSize(o); i ++) { if (!html_walkDocChilds(docs[i], userData)) { goto error; } } data->level --; corto_buffer_append(&data->content, "</div>\n"); corto_dealloc(docs); return 1; error: return 0; }
#include <corto/lang/lang.h> /* $header() */ corto_int16 corto_type_bindMetaprocedure( corto_type this, corto_metaprocedure procedure); /* $end */ corto_int16 _corto_metaprocedure_bind( corto_metaprocedure this) { /* $begin(corto/lang/metaprocedure/bind) */ corto_object parent; parent = corto_parentof(this); if (corto_instanceof(corto_type(corto_type_o), parent)) { if (corto_type_bindMetaprocedure(corto_type(parent), this)) { goto error; } } else { corto_seterr("invalid declaration of '%s' in non-type scope '%s'", corto_fullpath(NULL, this), corto_fullpath(NULL, parent)); goto error; } return corto_function_bind(corto_function(this)); error: return -1; /* $end */ }
static corto_int16 json_deserComposite(void* p, corto_type t, JSON_Value *v) { corto_assert(t->kind == CORTO_COMPOSITE, "not deserializing composite"); if (json_value_get_type(v) != JSONObject) { corto_seterr("expected object, got %s", json_valueTypeToString(v)); goto error; } JSON_Object* o = json_value_get_object(v); size_t count = json_object_get_count(o); size_t i; corto_bool isUnion = corto_interface(t)->kind == CORTO_UNION; corto_int32 discriminator = 0; corto_member unionMember = NULL; for (i = 0; i < count; i++) { const char* memberName = json_object_get_name(o, i); corto_member member_o; if (!strcmp(memberName, "super")) { JSON_Value* value = json_object_get_value(o, memberName); if (json_deserItem(p, corto_type(corto_interface(t)->base), value)) { goto error; } } else if (!strcmp(memberName, "_d") && isUnion) { JSON_Value* value = json_object_get_value(o, memberName); if (json_deserPrimitive(&discriminator, corto_union(t)->discriminator, value)) { goto error; } unionMember = corto_union_findCase(t, discriminator); if (!unionMember) { corto_seterr("discriminator '%d' invalid for union '%s'", discriminator, corto_fullpath(NULL, t)); } } else { member_o = corto_interface_resolveMember(t, (char*)memberName); /* Ensure that we're not resolving members from a base type */ if (!member_o || (corto_parentof(member_o) != t)) { corto_seterr( "cannot find member '%s' in type '%s'", memberName, corto_fullpath(NULL, t)); goto error; } if (isUnion && (unionMember != member_o)) { corto_seterr( "member '%s' does not match discriminator '%d' (expected member '%s')", memberName, discriminator, corto_idof(unionMember)); goto error; } else if (isUnion) { corto_int32 prev = *(corto_int32*)p; if (prev != discriminator) { corto_member prevMember = corto_union_findCase(t, prev); corto_deinitp(CORTO_OFFSET(p, prevMember->offset), prevMember->type); memset(CORTO_OFFSET(p, member_o->offset), 0, member_o->type->size); } *(corto_int32*)p = discriminator; } if (!json_deserMustSkip(member_o, p)) { JSON_Value* value = json_object_get_value(o, memberName); void *offset = CORTO_OFFSET(p, member_o->offset); if (member_o->modifiers & CORTO_OBSERVABLE) { offset = *(void**)offset; if (json_deserType(offset, member_o->type, value)) { goto error; } } else { if (member_o->modifiers & CORTO_OPTIONAL) { if (*(void**)offset) { corto_deinitp(*(void**)offset, member_o->type); memset(*(void**)offset, 0, member_o->type->size); } else { *(void**)offset = corto_calloc(member_o->type->size); } offset = *(void**)offset; } if (json_deserItem(offset, member_o->type, value)) { goto error; } } } } } return 0; error: return -1; }