Add:Core:Converted map and mapset to object functions
authormartin-s <martin-s@ffa7fe5e-494d-0410-b361-a75ebd5db220>
Tue, 20 Mar 2012 07:32:59 +0000 (07:32 +0000)
committermartin-s <martin-s@ffa7fe5e-494d-0410-b361-a75ebd5db220>
Tue, 20 Mar 2012 07:32:59 +0000 (07:32 +0000)
git-svn-id: https://navit.svn.sourceforge.net/svnroot/navit/trunk@4979 ffa7fe5e-494d-0410-b361-a75ebd5db220

navit/navit/attr.c
navit/navit/graphics.c
navit/navit/map.c
navit/navit/map.h
navit/navit/mapset.c
navit/navit/mapset.h
navit/navit/navit.c
navit/navit/xmlconfig.c
navit/navit/xmlconfig.h

index 530586a..0f8317e 100644 (file)
@@ -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);
index c345e61..2514634 100644 (file)
@@ -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);
 }
 
index e13b1fa..a48ea8f 100644 (file)
@@ -50,6 +50,7 @@
 #include "plugin.h"
 #include "callback.h"
 #include "country.h"
+#include "xmlconfig.h"
 
 /**
  * @brief Holds information about a map
  * 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(0<m->refcount) {
-               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,
+};
+
 
 
index 1dea19f..abcf0c6 100644 (file)
@@ -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);
index bc0dd09..5a6ec57 100644 (file)
@@ -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,
+};
+
+
+
index 54011bb..38ce100 100644 (file)
@@ -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);
index e7a6377..3c4d9fc 100644 (file)
@@ -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_);
index c37c69c..fd73a16 100644 (file)
@@ -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:
index 5465d91..88ec349 100644 (file)
@@ -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;