WIP: messing about with idnode/prop stuff.

This commit is contained in:
Adam Sutton 2013-04-24 16:39:36 +01:00
parent 139b654715
commit 3c2222a82c
7 changed files with 165 additions and 4 deletions

View file

@ -66,6 +66,7 @@ endif
# Core
#
SRCS = src/version.c \
src/test.c \
src/main.c \
src/tvhlog.c \
src/idnode.c \

View file

@ -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);
}

View file

@ -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 )

View file

@ -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);

View file

@ -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)

75
src/test.c Normal file
View file

@ -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);
}

32
src/test.h Normal file
View file

@ -0,0 +1,32 @@
#include "idnode.h"
#include <stdint.h>
/* 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 );