Skip to content

Instantly share code, notes, and snippets.

@erichocean
Created November 12, 2010 00:09
Show Gist options
  • Save erichocean/673484 to your computer and use it in GitHub Desktop.
Save erichocean/673484 to your computer and use it in GitHub Desktop.
Coding with Erich's SproutCore-style C application framework.
//
// core_main.c
// FOHR
//
// Created by Erich Ocean on 11/10/10.
// Copyright (c) 2010 Erich Atlas Ocean. All rights reserved.
//
#include "core.h"
#include <string.h> // strcmp
#include <stdio.h> // printf
#include <assert.h>
// MyObject.h
struct MyClass {
struct Object base;
int32_t foo;
double bar;
};
extern class_t MyClass;
extern properties_t MyClassProperties;
// MyClass.c
static void *ClassProperties(const void *obj, keyspec_t keyspec, void *getptr, void *setptr)
{
// Downcast to our class type.
struct MyClass *me = (struct MyClass *)obj;
// Handle key-value coding properties.
switch (keyspec) {
CLASSINFO(MyClass)
case foo_i32: getset(foo, int32_t)
case bar_d: getset(bar, double)
default: return ObjectProperties; // Our superclass' properties handler.
}
return NULL; // NULL means: we handled keyspec successfully.
}
static int fooChanges = 0;
static void PropertiesConfiguration()
{
// Register observers, default bindings, default animations, etc. here.
observe(foo_i32, ^(obj_t target, keyspec_t keyspec, void *val) {
fooChanges++;
printf("foo_i32 changed to %d", *(int32_t *)val);
});
}
static void ClassConstructor(struct Object *obj)
{
// Initialize our superclass.
Object(obj);
// Install our own properties handler.
obj->properties = ClassProperties;
configure_properties(ClassProperties, PropertiesConfiguration);
}
// Export to others.
class_t MyClass = ClassConstructor;
properties_t MyClassProperties = ClassProperties;
// main.c
int main(int argc, const char **argv)
{
pool_create();
obj_t myobject = obj_create(MyClass);
// Testing retain/release.
retain(myobject);
release(myobject);
int32_t a = 4;
set(myobject, foo_i32, &a);
double b = 5.0;
set(myobject, bar_d, &b);
int32_t c = 0;
get(myobject, foo_i32, &c);
double d = 0.0;
get(myobject, bar_d, &d);
class_t klass;
get(myobject, CLASS, &klass);
size_t sz;
get(myobject, SIZEOF, &sz);
char *classname;
get(myobject, CLASSNAME, &classname);
// Did we get back the values we set?
assert(a==c);
assert(b==d);
// Did our observers fire?
assert(fooChanges == 1);
// Is our CLASSINFO correct?
assert(klass == MyClass);
assert(sz == sizeof(struct MyClass));
assert(0==strcmp("MyClass", classname));
// Do we have the right retain count?
assert(0==retain_count(myobject));
// Try a nested pool with retain counts.
pool_create();
retain(myobject);
// Do we have the right retain count?
assert(1==retain_count(myobject));
pool_release();
// Do we have the right retain count in the parent pool?
assert(1==retain_count(myobject));
release(myobject);
pool_release();
return 0;
}
@geoffreyd
Copy link

This looks neat, I'm a lot rusty with my C, but looks like you've covered most of SC

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment