From 2d262c091fa0c99b7fcd4de8a7c6be31b45bd447 Mon Sep 17 00:00:00 2001 From: Pierre-Luc Beaudoin Date: Thu, 11 Jun 2009 20:40:15 -0400 Subject: [PATCH] Define ChamplainPoint as a G_TYPE_BOXED --- champlain/Makefile.am | 4 ++ champlain/champlain-defines.h | 6 --- champlain/champlain-point.c | 94 +++++++++++++++++++++++++++++++++++ champlain/champlain-point.h | 43 ++++++++++++++++ champlain/champlain-polygon.c | 10 ++-- champlain/champlain-polygon.h | 2 +- champlain/champlain.h | 1 + 7 files changed, 146 insertions(+), 14 deletions(-) create mode 100644 champlain/champlain-point.c create mode 100644 champlain/champlain-point.h diff --git a/champlain/Makefile.am b/champlain/Makefile.am index fd5b2ea..c9922ae 100644 --- a/champlain/Makefile.am +++ b/champlain/Makefile.am @@ -17,6 +17,7 @@ lib_LTLIBRARIES = libchamplain-0.3.la libchamplain_headers = \ champlain.h \ champlain-defines.h \ + champlain-point.h \ champlain-view.h \ champlain-layer.h \ champlain-base-marker.h \ @@ -46,6 +47,7 @@ libchamplain_0_3_la_SOURCES = \ champlain-map-source.c \ champlain-network-map-source.c \ champlain-map-source-factory.c \ + champlain-point.c \ champlain-cache.c \ champlain-polygon.c @@ -55,6 +57,7 @@ noinst_HEADERS = \ champlain-base-marker.h \ champlain-marker.h \ champlain-private.h \ + champlain-point.h \ champlain-map.h \ champlain-zoom-level.h \ champlain-tile.h \ @@ -72,6 +75,7 @@ libchamplain_include_HEADERS = \ champlain.h \ champlain-view.h \ champlain-defines.h \ + champlain-point.h \ champlain-enum-types.h \ champlain-layer.h \ champlain-map-source.h \ diff --git a/champlain/champlain-defines.h b/champlain/champlain-defines.h index 1f98d31..54056b7 100644 --- a/champlain/champlain-defines.h +++ b/champlain/champlain-defines.h @@ -47,10 +47,4 @@ typedef enum CHAMPLAIN_STATE_DONE } ChamplainState; -typedef struct -{ - double lat; - double lon; -} ChamplainPoint; - #endif diff --git a/champlain/champlain-point.c b/champlain/champlain-point.c new file mode 100644 index 0000000..4997890 --- /dev/null +++ b/champlain/champlain-point.c @@ -0,0 +1,94 @@ +/* + * Copyright (C) 2008 Pierre-Luc Beaudoin + * + * This file is inspired by clutter-color.c which is + * Copyright (C) 2006 OpenedHand, and has the same license. + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#include "champlain-point.h" + +GType +champlain_point_get_type (void) +{ + static GType type = 0; + + if (G_UNLIKELY (type == 0)) + { + type = g_boxed_type_register_static (g_intern_static_string ("ChamplainPoint"), + (GBoxedCopyFunc) champlain_point_copy, + (GBoxedFreeFunc) champlain_point_free); + } + + return type; +} + +/** + * champlain_point_copy: + * @point: a #ChamplainPoint + * + * Makes a copy of the point structure. The result must be + * freed using champlain_point_free(). + * + * Return value: an allocated copy of @point. + * + * Since: 0.4 + */ +ChamplainPoint * +champlain_point_copy (const ChamplainPoint *point) +{ + if (G_LIKELY (point != NULL)) + return g_slice_dup (ChamplainPoint, point); + + return NULL; +} + +/** + * champlain_point_free: + * @point: a #ChamplainPoint + * + * Frees a point structure created with #champlain_point_new or + * #champlain_point_copy + * + * Since: 0.4 + */ +void +champlain_point_free (ChamplainPoint *point) +{ + if (G_LIKELY (point != NULL)) + g_slice_free (ChamplainPoint, point); +} + +/** + * champlain_point_new: + * @point: a #ChamplainPoint + * + * Frees a point structure created with champlain_point_copy(). + * + * Since: 0.4 + */ +ChamplainPoint * +champlain_point_new (gdouble lat, gdouble lon) +{ + ChamplainPoint *point; + + point = g_slice_new (ChamplainPoint); + + point->lat = lat; + point->lon = lon; + + return point; +} diff --git a/champlain/champlain-point.h b/champlain/champlain-point.h new file mode 100644 index 0000000..3356f92 --- /dev/null +++ b/champlain/champlain-point.h @@ -0,0 +1,43 @@ +/* + * Copyright (C) 2009 Pierre-Luc Beaudoin + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#if !defined (__CHAMPLAIN_CHAMPLAIN_H_INSIDE__) && !defined (CHAMPLAIN_COMPILATION) +#error "Only can be included directly." +#endif + +#ifndef CHAMPLAIN_POINT_H +#define CHAMPLAIN_POINT_H + +#include + +typedef struct +{ + double lat; + double lon; +} ChamplainPoint; + +GType champlain_point_get_type (void) G_GNUC_CONST; +#define CHAMPLAIN_TYPE_POINT (champlain_point_get_type ()) + +ChamplainPoint * champlain_point_copy (const ChamplainPoint *point); + +void champlain_point_free (ChamplainPoint *point); + +ChamplainPoint * champlain_point_new (gdouble lat, gdouble lon); + +#endif diff --git a/champlain/champlain-polygon.c b/champlain/champlain-polygon.c index 820ddff..7cb316e 100644 --- a/champlain/champlain-polygon.c +++ b/champlain/champlain-polygon.c @@ -317,9 +317,7 @@ champlain_polygon_append_point (ChamplainPolygon *self, { g_return_val_if_fail (CHAMPLAIN_IS_POLYGON (self), NULL); - ChamplainPoint *point = g_new0 (ChamplainPoint, 1); - point->lat = lat; - point->lon = lon; + ChamplainPoint *point = champlain_point_new (lat, lon); self->priv->points = g_list_append (self->priv->points, point); return point; @@ -346,9 +344,7 @@ champlain_polygon_insert_point (ChamplainPolygon *self, { g_return_val_if_fail (CHAMPLAIN_IS_POLYGON (self), NULL); - ChamplainPoint *point = g_new0 (ChamplainPoint, 1); - point->lat = lat; - point->lon = lon; + ChamplainPoint *point = champlain_point_new (lat, lon); self->priv->points = g_list_insert (self->priv->points, point, pos); return point; @@ -370,7 +366,7 @@ champlain_polygon_clear_points (ChamplainPolygon *self) GList *next = self->priv->points; while (next != NULL) { - g_free (next->data); + champlain_point_free (next->data); next = g_list_next (next); } g_list_free (self->priv->points); diff --git a/champlain/champlain-polygon.h b/champlain/champlain-polygon.h index 4bdaa74..8671bfa 100644 --- a/champlain/champlain-polygon.h +++ b/champlain/champlain-polygon.h @@ -23,7 +23,7 @@ #ifndef CHAMPLAIN_POLYGON_H #define CHAMPLAIN_POLYGON_H -#include +#include #include #include diff --git a/champlain/champlain.h b/champlain/champlain.h index c8c14b5..7f45eb6 100644 --- a/champlain/champlain.h +++ b/champlain/champlain.h @@ -35,6 +35,7 @@ #include "champlain/champlain-marker.h" #include "champlain/champlain-view.h" #include "champlain/champlain-enum-types.h" +#include "champlain/champlain-point.h" #include "champlain/champlain-map-source.h" #include "champlain/champlain-map-source-factory.h" #include "champlain/champlain-network-map-source.h" -- 2.39.5