From 3c2222a82c3ebcbfb0f04d0fb8fd1a196dfbffb0 Mon Sep 17 00:00:00 2001 From: Adam Sutton Date: Wed, 24 Apr 2013 16:39:36 +0100 Subject: [PATCH] WIP: messing about with idnode/prop stuff. --- Makefile | 1 + src/idnode.c | 43 +++++++++++++++++++++++++++--- src/idnode.h | 6 +++++ src/main.c | 4 +++ src/prop.h | 8 +++++- src/test.c | 75 ++++++++++++++++++++++++++++++++++++++++++++++++++++ src/test.h | 32 ++++++++++++++++++++++ 7 files changed, 165 insertions(+), 4 deletions(-) create mode 100644 src/test.c create mode 100644 src/test.h diff --git a/Makefile b/Makefile index 0be7119d..7e0ca1d0 100644 --- a/Makefile +++ b/Makefile @@ -66,6 +66,7 @@ endif # Core # SRCS = src/version.c \ + src/test.c \ src/main.c \ src/tvhlog.c \ src/idnode.c \ diff --git a/src/idnode.c b/src/idnode.c index 8894affe..72bae592 100644 --- a/src/idnode.c +++ b/src/idnode.c @@ -6,6 +6,7 @@ #include "idnode.h" #include "notify.h" +#include "settings.h" static int randfd = 0; @@ -267,7 +268,7 @@ idnode_serialize(struct idnode *self) * */ static void -idnode_save(idnode_t *in) +idnode_updated(idnode_t *in) { const idclass_t *ic = in->in_class; @@ -307,7 +308,7 @@ idnode_set_prop(idnode_t *in, const char *key, const char *value) break; } if(do_save) - idnode_save(in); + idnode_updated(in); } @@ -324,7 +325,7 @@ idnode_update_all_props(idnode_t *in, for(;ic != NULL; ic = ic->ic_super) do_save |= prop_update_all(in, ic->ic_properties, getvalue, opaque); if(do_save) - idnode_save(in); + idnode_updated(in); } @@ -340,3 +341,39 @@ idnode_notify_title_changed(void *obj) htsmsg_add_str(m, "text", idnode_get_title(in)); notify_by_msg("idnodeNameChanged", m); } + +inode_t * +idnode_create ( size_t alloc, const idclass_t *class, const char *uuid ) +{ + idnode_t *self = calloc(1, alloc); + idnode_insert(self, uuid, class); + return self; +} + +void +idnode_save ( idnode_t *self, const char *path ) +{ + // serialize + // save +} + +idnode_t * +idnode_load ( htsmsg_field_t *cfg, void*(*create)(const char*) ) +{ + htsmsg_t *m; + idnode_t *self; + if (!(m = htsmsg_get_map_by_field(cfg))) return NULL; + if (!(self = create(cfg->hmf_name))) return NULL; + // todo deserialize settings + return self; +} + +void +idnode_load_all ( const char *path, void*(*create)(const char *) ) +{ + htsmsg_t *m; + htsmsg_field_t *f; + if ((m = hts_settings_load(path))) + HTSMSG_FOREACH(f, m) + idnode_load(f, create); +} diff --git a/src/idnode.h b/src/idnode.h index 01d06ead..b6bfbf62 100644 --- a/src/idnode.h +++ b/src/idnode.h @@ -50,4 +50,10 @@ void idnode_update_all_props(idnode_t *in, void idnode_notify_title_changed(void *obj); +void idnode_save ( idnode_t *self, const char *path ); +idnode_t *idnode_load ( htsmsg_field_t *cfg, void*(*create)(const char*) ); + +void idnode_load_all ( const char *path, void *(*create)(const char*) ); + +idnode_t *idnode_create ( size_t alloc, const idclass_t *class, const char *uuid ) diff --git a/src/main.c b/src/main.c index 99a57a7b..b013d327 100644 --- a/src/main.c +++ b/src/main.c @@ -66,6 +66,7 @@ #if ENABLE_LIBAV #include "libav.h" #endif +#include "test.h" /* Command line option struct */ typedef struct { @@ -652,7 +653,10 @@ main(int argc, char **argv) tvhlog_options &= ~TVHLOG_OPT_DECORATE; /* Initialise configuration */ + idnode_init(); hts_settings_init(opt_config); + obj_b_t *b = obj_b_create(NULL); + b->a_func1(b); /* Setup global mutexes */ pthread_mutex_init(&ffmpeg_lock, NULL); diff --git a/src/prop.h b/src/prop.h index e7bc63e9..226616c8 100644 --- a/src/prop.h +++ b/src/prop.h @@ -23,7 +23,6 @@ typedef struct property { } property_t; - void prop_add_params_to_msg(void *obj, const property_t *p, htsmsg_t *msg); void prop_write_values(void *ptr, const property_t *pl, htsmsg_t *m); @@ -35,3 +34,10 @@ int prop_set(void *obj, const property_t *p, const char *key, const char *val); int prop_update_all(void *obj, const property_t *p, const char *(*getvalue)(void *opaque, const char *key), void *opaque); + +#define PROPDEF1(_i, _n, _t, _o, _v)\ + .id = _i,\ + .name = _n,\ + .type = _t,\ + .off = offsetof(_o, _v) + diff --git a/src/test.c b/src/test.c new file mode 100644 index 00000000..02043132 --- /dev/null +++ b/src/test.c @@ -0,0 +1,75 @@ +#include "test.h" + +const idclass_t obj_a_class = { + .ic_class = "obj_a", + .ic_caption = "Object A", + .ic_properties = (const property_t[]){ + { PROPDEF1("int1", "Integer 1", PT_INT, obj_a_t, a_int1) }, + { PROPDEF1("bool1", "Boolean 1", PT_BOOL, obj_a_t, a_bool1) }, + { PROPDEF1("str1", "String 1", PT_STR, obj_a_t, a_str1) }, + } +}; + +static void obj_b_save ( idnode_t *self ); + +const idclass_t obj_b_class = { + .ic_super = &obj_a_class, + .ic_class = "obj_b", + .ic_caption = "Object B", + .ic_save = obj_b_save, + .ic_properties = (const property_t[]){ + { PROPDEF1("int2", "Integer 2", PT_INT, obj_b_t, b_int2) }, + { PROPDEF1("bool2", "Boolean 2", PT_BOOL, obj_b_t, b_bool2) }, + { PROPDEF1("str2", "String 2", PT_STR, obj_b_t, b_str2) }, + } +}; + +static void +obj_a_func1 ( void *self ) +{ + obj_a_t *a = self; + printf("a->a_int1 = %d\n", a->a_int1); +} + +static obj_a_t * +obj_a_create1 ( size_t alloc, const idclass_t *class, const char *uuid ) +{ + obj_a_t *a = idnode_create(alloc, class, uuid); + a->a_func1 = obj_a_func1; + return a; +} + +#define obj_a_create(type, uuid)\ + (struct type*)obj_a_create1(sizeof(struct type), &type##_class, uuid) + +static void +obj_b_func1 ( void *self ) +{ + obj_b_t *b = self; + printf("b->a_int1 = %d\n", b->a_int1); + printf("b->b_int2 = %d\n", b->b_int2); +} + +obj_b_t * +obj_b_create ( const char *uuid ) +{ + obj_b_t *b = obj_a_create(obj_b, uuid); + b->a_func1 = obj_b_func1; + b->a_bool1 = 0; + b->b_bool2 = 1; + b->a_int1 = 2; + b->b_int2 = 3; + b->a_str1 = strdup("HELLO"); + b->b_str2 = strdup("WORLD"); + return b; +} + +void obj_b_save ( idnode_t *self ) +{ + idnode_save(self, "test"); +} + +void obj_b_load_all ( void ) +{ + idnode_load_all("test", (void*(*)(const char*))obj_b_create); +} diff --git a/src/test.h b/src/test.h new file mode 100644 index 00000000..463aa17d --- /dev/null +++ b/src/test.h @@ -0,0 +1,32 @@ +#include "idnode.h" + +#include + +/* Parent */ +typedef struct obj_a +{ + idnode_t a_id; + + uint32_t a_int1; + int a_bool1; + char *a_str1; + + void (*a_func1) (void *a); + +} obj_a_t; + +extern const idclass_t obj_a_class; + +/* Child */ +typedef struct obj_b +{ + obj_a_t; + + uint32_t b_int2; + int b_bool2; + char *b_str2; + +} obj_b_t; + +obj_b_t *obj_b_create ( const char *uuid ); +void obj_b_load_all ( void );