From fa5e05e40d47016500c8c96581b659ee6f6ce7f9 Mon Sep 17 00:00:00 2001 From: martin-s Date: Tue, 20 Mar 2012 07:32:59 +0000 Subject: Add:Core:Converted map and mapset to object functions git-svn-id: http://svn.code.sf.net/p/navit/code/trunk/navit@4979 ffa7fe5e-494d-0410-b361-a75ebd5db220 --- navit/attr.c | 9 +++++--- navit/graphics.c | 2 +- navit/map.c | 60 +++++++++++++++++++++++++++++------------------- navit/map.h | 3 ++- navit/mapset.c | 68 +++++++++++++++++++++++++++++++++++++++++++------------ navit/mapset.h | 1 + navit/navit.c | 13 +++++------ navit/xmlconfig.c | 8 +++---- navit/xmlconfig.h | 4 ++++ 9 files changed, 114 insertions(+), 54 deletions(-) diff --git a/navit/attr.c b/navit/attr.c index 530586a43..0f8317ef0 100644 --- a/navit/attr.c +++ b/navit/attr.c @@ -569,7 +569,7 @@ attr_free(struct attr *attr) { if (!attr) return; - if (attr->type == attr_navit || attr->type == attr_trackingo || attr->type == attr_vehicle) { + if (HAS_OBJECT_FUNC(attr->type)) { struct navit_object *obj=attr->u.data; if (obj && obj->func && obj->func->unref) obj->func->unref(obj); @@ -588,7 +588,7 @@ attr_dup_content(struct attr *src, struct attr *dst) if (src->type >= attr_type_int_begin && src->type <= attr_type_int_end) dst->u.num=src->u.num; else if (src->type >= attr_type_object_begin && src->type <= attr_type_object_end) { - if (src->type == attr_navit || src->type == attr_trackingo || src->type == attr_vehicle) { + if (HAS_OBJECT_FUNC(src->type)) { struct navit_object *obj=src->u.data; if (obj && obj->func && obj->func->ref) { dst->u.data=obj->func->ref(obj); @@ -626,9 +626,12 @@ attr_list_free(struct attr **attrs) struct attr ** attr_list_dup(struct attr **attrs) { - struct attr **ret=attrs; + struct attr **ret; int i,count=0; + if (!attrs) + return NULL; + while (attrs[count]) count++; ret=g_new0(struct attr *, count+1); diff --git a/navit/graphics.c b/navit/graphics.c index c345e61af..25146341e 100644 --- a/navit/graphics.c +++ b/navit/graphics.c @@ -405,9 +405,9 @@ void graphics_free(struct graphics *gra) graphics_gc_destroy(gra->gc[0]); graphics_gc_destroy(gra->gc[1]); graphics_gc_destroy(gra->gc[2]); - gra->meth.graphics_destroy(gra->priv); g_free(gra->default_font); graphics_font_destroy_all(gra); + gra->meth.graphics_destroy(gra->priv); g_free(gra); } diff --git a/navit/map.c b/navit/map.c index e13b1fabd..a48ea8fbe 100644 --- a/navit/map.c +++ b/navit/map.c @@ -50,6 +50,7 @@ #include "plugin.h" #include "callback.h" #include "country.h" +#include "xmlconfig.h" /** * @brief Holds information about a map @@ -57,11 +58,12 @@ * This structure holds information about a map. */ struct map { + struct object_func *func; /**< Object functions */ + int refcount; /**< Reference count */ + struct attr **attrs; /**< Attributes of this map */ struct map_methods meth; /**< Structure with pointers to the map plugin's functions */ struct map_priv *priv; /**< Private data of the map, only known to the map plugin */ - struct attr **attrs; /**< Attributes of this map */ struct callback_list *attr_cbl; /**< List of callbacks that are called when attributes change */ - int refcount; }; /** @@ -107,23 +109,22 @@ map_new(struct attr *parent, struct attr **attrs) m=g_new0(struct map, 1); m->attrs=attr_list_dup(attrs); + m->func=&map_func; + m->refcount = 1; m->attr_cbl=callback_list_new(); m->priv=maptype_new(&m->meth, attrs, m->attr_cbl); if (! m->priv) { - m->refcount = 1; map_destroy(m); m=NULL; } - else { - m->refcount = 0; - } return m; } -void +struct map * map_ref(struct map* m) { m->refcount++; + return m; } @@ -259,33 +260,30 @@ map_set_projection(struct map *this_, enum projection pro) this_->meth.pro=pro; } -void -map_destroy_do(struct map *m) -{ - if (m->priv) - m->meth.map_destroy(m->priv); - attr_list_free(m->attrs); - callback_list_destroy(m->attr_cbl); - g_free(m); -} - /** * @brief Destroys an opened map * * @param m The map to be destroyed */ + void map_destroy(struct map *m) { if (!m) return; + if (m->priv) + m->meth.map_destroy(m->priv); + attr_list_free(m->attrs); + callback_list_destroy(m->attr_cbl); + g_free(m); +} - if(0refcount) { - m->refcount--; - } - if(0 == m->refcount) { - map_destroy_do(m); - } +void +map_unref(struct map *m) +{ + m->refcount--; + if (m->refcount <= 0) + map_destroy(m); } /** @@ -710,5 +708,21 @@ map_rect_create_item(struct map_rect *mr, enum item_type type_) } } +struct object_func map_func = { + attr_map, + (object_func_new)map_new, + (object_func_get_attr)map_get_attr, + (object_func_iter_new)NULL, + (object_func_iter_destroy)NULL, + (object_func_set_attr)map_set_attr, + (object_func_add_attr)NULL, + (object_func_remove_attr)NULL, + (object_func_init)NULL, + (object_func_destroy)map_destroy, + (object_func_dup)NULL, + (object_func_ref)map_ref, + (object_func_unref)map_unref, +}; + diff --git a/navit/map.h b/navit/map.h index 1dea19f47..abcf0c602 100644 --- a/navit/map.h +++ b/navit/map.h @@ -235,7 +235,8 @@ struct map_search; struct map_selection; struct pcoord; struct map *map_new(struct attr *parent, struct attr **attrs); -void map_ref(struct map* m); +struct map *map_ref(struct map* m); +void map_unref(struct map* m); int map_get_attr(struct map *this_, enum attr_type type, struct attr *attr, struct attr_iter *iter); int map_set_attr(struct map *this_, struct attr *attr); void map_add_callback(struct map *this_, struct callback *cb); diff --git a/navit/mapset.c b/navit/mapset.c index bc0dd09bc..5a6ec5794 100644 --- a/navit/mapset.c +++ b/navit/mapset.c @@ -34,6 +34,7 @@ #include "mapset.h" #include "projection.h" #include "map.h" +#include "xmlconfig.h" /** * @brief A mapset @@ -41,6 +42,9 @@ * This structure holds a complete mapset */ struct mapset { + struct object_func *func; + int refcount; + struct attr **attrs; GList *maps; /**< Linked list of all the maps in the mapset */ }; @@ -58,10 +62,20 @@ struct mapset *mapset_new(struct attr *parent, struct attr **attrs) struct mapset *ms; ms=g_new0(struct mapset, 1); + ms->func=&mapset_func; + ms->refcount=1; + ms->attrs=attr_list_dup(attrs); return ms; } +struct mapset *mapset_dup(struct mapset *ms) +{ + struct mapset *ret=mapset_new(NULL, ms->attrs); + ret->maps=g_list_copy(ms->maps); + return ret; +} + struct attr_iter * mapset_attr_iter_new(void) @@ -86,8 +100,8 @@ mapset_add_attr(struct mapset *ms, struct attr *attr) { switch (attr->type) { case attr_map: + ms->attrs=attr_generic_add_attr(ms->attrs,attr); ms->maps=g_list_append(ms->maps, attr->u.map); - map_ref(attr->u.map); return 1; default: return 0; @@ -99,6 +113,7 @@ mapset_remove_attr(struct mapset *ms, struct attr *attr) { switch (attr->type) { case attr_map: + ms->attrs=attr_generic_remove_attr(ms->attrs,attr); ms->maps=g_list_remove(ms->maps, attr->u.map); return 1; default: @@ -130,14 +145,6 @@ mapset_get_attr(struct mapset *ms, enum attr_type type, struct attr *attr, struc return 0; } - -#if 0 -static void mapset_maps_free(struct mapset *ms) -{ - /* todo */ -} -#endif - /** * @brief Destroys a mapset. * @@ -148,15 +155,27 @@ static void mapset_maps_free(struct mapset *ms) */ void mapset_destroy(struct mapset *ms) { - GList *map; - map=ms->maps; - while (map) { - map_destroy(map->data); - map=g_list_next(map); - } + g_list_free(ms->maps); + attr_list_free(ms->attrs); g_free(ms); } +struct mapset * +mapset_ref(struct mapset* m) +{ + m->refcount++; + return m; +} + + +void +mapset_unref(struct mapset *m) +{ + m->refcount--; + if (m->refcount <= 0) + mapset_destroy(m); +} + /** * @brief Handle for a mapset in use * @@ -377,3 +396,22 @@ mapset_search_destroy(struct mapset_search *this_) g_free(this_); } } + +struct object_func mapset_func = { + attr_mapset, + (object_func_new)mapset_new, + (object_func_get_attr)mapset_get_attr, + (object_func_iter_new)mapset_attr_iter_new, + (object_func_iter_destroy)mapset_attr_iter_destroy, + (object_func_set_attr)NULL, + (object_func_add_attr)mapset_add_attr, + (object_func_remove_attr)mapset_remove_attr, + (object_func_init)NULL, + (object_func_destroy)mapset_destroy, + (object_func_dup)mapset_dup, + (object_func_ref)mapset_ref, + (object_func_unref)mapset_unref, +}; + + + diff --git a/navit/mapset.h b/navit/mapset.h index 54011bb3b..38ce100c8 100644 --- a/navit/mapset.h +++ b/navit/mapset.h @@ -34,6 +34,7 @@ struct mapset; struct mapset_handle; struct mapset_search; struct mapset *mapset_new(struct attr *parent, struct attr **attrs); +struct mapset *mapset_dup(struct mapset *ms); struct attr_iter *mapset_attr_iter_new(void); void mapset_attr_iter_destroy(struct attr_iter *iter); int mapset_add_attr(struct mapset *ms, struct attr *attr); diff --git a/navit/navit.c b/navit/navit.c index e7a6377f9..3c4d9fcd9 100644 --- a/navit/navit.c +++ b/navit/navit.c @@ -102,7 +102,7 @@ struct navit_vehicle { struct navit { struct object_func *func; int refcount; - struct attr *attrs; + struct attr **attrs; struct attr self; GList *mapsets; GList *layouts; @@ -1414,6 +1414,8 @@ navit_new(struct attr *parent, struct attr **attrs) command_add_table(this_->attr_cbl, commands, sizeof(commands)/sizeof(struct command_table), this_); this_->messages = messagelist_new(attrs); + + dbg(0,"return %p\n",this_); return this_; } @@ -3192,7 +3194,7 @@ navit_block(struct navit *this_, int block) void navit_destroy(struct navit *this_) { - struct mapset*ms; + dbg(0,"enter %p\n",this_); callback_list_call_attr_1(this_->attr_cbl, attr_destroy, this_); attr_list_free(this_->attrs); if (this_->bookmarks) { @@ -3220,16 +3222,13 @@ navit_destroy(struct navit *this_) callback_destroy(this_->predraw_callback); callback_destroy(this_->route_cb); - route_destroy(this_->route); + if (this_->route) + route_destroy(this_->route); map_destroy(this_->former_destination); graphics_displaylist_destroy(this_->displaylist); - ms = navit_get_mapset(this_); - if(ms) - mapset_destroy(ms); - graphics_free(this_->gra); g_free(this_); diff --git a/navit/xmlconfig.c b/navit/xmlconfig.c index c37c69c23..fd73a1686 100644 --- a/navit/xmlconfig.c +++ b/navit/xmlconfig.c @@ -252,8 +252,6 @@ static struct object_func object_funcs[] = { { attr_layer, NEW(layer_new), NULL, NULL, NULL, NULL, ADD(layer_add_attr)}, { attr_layout, NEW(layout_new), NULL, NULL, NULL, NULL, ADD(layout_add_attr)}, { attr_log, NEW(log_new)}, - { attr_map, NEW(map_new)}, - { attr_mapset, NEW(mapset_new), NULL, NULL, NULL, NULL, ADD(mapset_add_attr)}, { attr_navigation, NEW(navigation_new), GET(navigation_get_attr)}, { attr_osd, NEW(osd_new), GET(osd_get_attr), NULL, NULL, SET(osd_set_attr) }, { attr_plugins, NEW(plugins_new), NULL, NULL, NULL, NULL, NULL, NULL, INIT(plugins_init)}, @@ -267,13 +265,15 @@ static struct object_func object_funcs[] = { { attr_vehicleprofile, NEW(vehicleprofile_new), GET(vehicleprofile_get_attr), NULL, NULL, SET(vehicleprofile_set_attr), ADD(vehicleprofile_add_attr) }, }; -extern struct object_func navit_func, tracking_func, vehicle_func; - struct object_func * object_func_lookup(enum attr_type type) { int i; switch (type) { + case attr_map: + return &map_func; + case attr_mapset: + return &mapset_func; case attr_navit: return &navit_func; case attr_trackingo: diff --git a/navit/xmlconfig.h b/navit/xmlconfig.h index 5465d913e..88ec349ab 100644 --- a/navit/xmlconfig.h +++ b/navit/xmlconfig.h @@ -54,6 +54,10 @@ struct object_func { void *(*unref)(void *); }; +extern struct object_func map_func, mapset_func, navit_func, tracking_func, vehicle_func; + +#define HAS_OBJECT_FUNC(x) ((x) == attr_map || (x) == attr_mapset || (x) == attr_navit || (x) == attr_trackingo || (x) == attr_vehicle) + struct navit_object { struct object_func *func; int refcount; -- cgit v1.2.1