shumate/shumate-coordinate.c
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /* | ||
| 2 | * Copyright (C) 2011-2013 Jiri Techet <techet@gmail.com> | ||
| 3 | * Copyright (C) 2019 Marcus Lundblad <ml@update.uu.se> | ||
| 4 | * | ||
| 5 | * This library is free software; you can redistribute it and/or | ||
| 6 | * modify it under the terms of the GNU Lesser General Public | ||
| 7 | * License as published by the Free Software Foundation; either | ||
| 8 | * version 2.1 of the License, or (at your option) any later version. | ||
| 9 | * | ||
| 10 | * This library is distributed in the hope that it will be useful, | ||
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
| 13 | * Lesser General Public License for more details. | ||
| 14 | * | ||
| 15 | * You should have received a copy of the GNU Lesser General Public | ||
| 16 | * License along with this library; if not, write to the Free Software | ||
| 17 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA | ||
| 18 | */ | ||
| 19 | |||
| 20 | /** | ||
| 21 | * ShumateCoordinate: | ||
| 22 | * | ||
| 23 | * A simple object implementing [iface@Location]. | ||
| 24 | */ | ||
| 25 | |||
| 26 | #include "shumate-coordinate.h" | ||
| 27 | |||
| 28 | #include "shumate-marshal.h" | ||
| 29 | #include "shumate-location.h" | ||
| 30 | |||
| 31 | enum | ||
| 32 | { | ||
| 33 | PROP_LONGITUDE = 1, | ||
| 34 | PROP_LATITUDE, | ||
| 35 | }; | ||
| 36 | |||
| 37 | |||
| 38 | static void set_location (ShumateLocation *location, | ||
| 39 | double latitude, | ||
| 40 | double longitude); | ||
| 41 | static double get_latitude (ShumateLocation *location); | ||
| 42 | static double get_longitude (ShumateLocation *location); | ||
| 43 | |||
| 44 | static void location_interface_init (ShumateLocationInterface *iface); | ||
| 45 | |||
| 46 | typedef struct | ||
| 47 | { | ||
| 48 | double longitude; | ||
| 49 | double latitude; | ||
| 50 | } ShumateCoordinatePrivate; | ||
| 51 | |||
| 52 |
4/6shumate_coordinate_class_intern_init:
✓ Branch 3 → 4 taken 3 times.
✗ Branch 3 → 5 not taken.
shumate_coordinate_get_type:
✓ Branch 2 → 3 taken 3 times.
✓ Branch 2 → 7 taken 50 times.
✓ Branch 4 → 5 taken 3 times.
✗ Branch 4 → 7 not taken.
|
119 | G_DEFINE_TYPE_WITH_CODE (ShumateCoordinate, shumate_coordinate, G_TYPE_INITIALLY_UNOWNED, |
| 53 | G_ADD_PRIVATE (ShumateCoordinate) | ||
| 54 | G_IMPLEMENT_INTERFACE (SHUMATE_TYPE_LOCATION, location_interface_init)); | ||
| 55 | |||
| 56 | static void | ||
| 57 | ✗ | shumate_coordinate_get_property (GObject *object, | |
| 58 | guint prop_id, | ||
| 59 | GValue *value, | ||
| 60 | GParamSpec *pspec) | ||
| 61 | { | ||
| 62 | ✗ | ShumateCoordinate *coordinate = SHUMATE_COORDINATE (object); | |
| 63 | ✗ | ShumateCoordinatePrivate *priv = shumate_coordinate_get_instance_private (coordinate); | |
| 64 | |||
| 65 | ✗ | switch (prop_id) | |
| 66 | { | ||
| 67 | ✗ | case PROP_LONGITUDE: | |
| 68 | ✗ | g_value_set_double (value, priv->longitude); | |
| 69 | ✗ | break; | |
| 70 | |||
| 71 | ✗ | case PROP_LATITUDE: | |
| 72 | ✗ | g_value_set_double (value, priv->latitude); | |
| 73 | ✗ | break; | |
| 74 | |||
| 75 | ✗ | default: | |
| 76 | ✗ | G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); | |
| 77 | } | ||
| 78 | ✗ | } | |
| 79 | |||
| 80 | |||
| 81 | static void | ||
| 82 | 8 | shumate_coordinate_set_property (GObject *object, | |
| 83 | guint prop_id, | ||
| 84 | const GValue *value, | ||
| 85 | GParamSpec *pspec) | ||
| 86 | { | ||
| 87 | 8 | ShumateCoordinate *coordinate = SHUMATE_COORDINATE (object); | |
| 88 | 8 | ShumateCoordinatePrivate *priv = shumate_coordinate_get_instance_private (coordinate); | |
| 89 | |||
| 90 |
2/3✓ Branch 2 → 3 taken 4 times.
✓ Branch 2 → 6 taken 4 times.
✗ Branch 2 → 9 not taken.
|
8 | switch (prop_id) |
| 91 | { | ||
| 92 | 4 | case PROP_LONGITUDE: | |
| 93 | { | ||
| 94 | 4 | double longitude = g_value_get_double (value); | |
| 95 | 4 | set_location (SHUMATE_LOCATION (coordinate), priv->latitude, longitude); | |
| 96 | 4 | break; | |
| 97 | } | ||
| 98 | |||
| 99 | 4 | case PROP_LATITUDE: | |
| 100 | { | ||
| 101 | 4 | double latitude = g_value_get_double (value); | |
| 102 | 4 | set_location (SHUMATE_LOCATION (coordinate), latitude, priv->longitude); | |
| 103 | 4 | break; | |
| 104 | } | ||
| 105 | |||
| 106 | ✗ | default: | |
| 107 | ✗ | G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); | |
| 108 | } | ||
| 109 | 8 | } | |
| 110 | |||
| 111 | |||
| 112 | static void | ||
| 113 | 16 | set_location (ShumateLocation *location, | |
| 114 | double latitude, | ||
| 115 | double longitude) | ||
| 116 | { | ||
| 117 | 16 | ShumateCoordinate *coordinate = SHUMATE_COORDINATE (location); | |
| 118 | 16 | ShumateCoordinatePrivate *priv = shumate_coordinate_get_instance_private (coordinate); | |
| 119 | |||
| 120 |
1/2✗ Branch 3 → 4 not taken.
✓ Branch 3 → 5 taken 16 times.
|
16 | g_return_if_fail (SHUMATE_IS_COORDINATE (location)); |
| 121 | |||
| 122 |
1/2✓ Branch 5 → 6 taken 16 times.
✗ Branch 5 → 7 not taken.
|
16 | priv->longitude = CLAMP (longitude, SHUMATE_MIN_LONGITUDE, SHUMATE_MAX_LONGITUDE); |
| 123 |
1/2✓ Branch 7 → 8 taken 16 times.
✗ Branch 7 → 9 not taken.
|
16 | priv->latitude = CLAMP (latitude, SHUMATE_MIN_LATITUDE, SHUMATE_MAX_LATITUDE); |
| 124 | |||
| 125 | 16 | g_object_notify (G_OBJECT (location), "latitude"); | |
| 126 | 16 | g_object_notify (G_OBJECT (location), "longitude"); | |
| 127 | } | ||
| 128 | |||
| 129 | |||
| 130 | static double | ||
| 131 | 16 | get_latitude (ShumateLocation *location) | |
| 132 | { | ||
| 133 | 16 | ShumateCoordinate *coordinate = SHUMATE_COORDINATE (location); | |
| 134 | 16 | ShumateCoordinatePrivate *priv = shumate_coordinate_get_instance_private (coordinate); | |
| 135 | |||
| 136 |
1/2✗ Branch 3 → 4 not taken.
✓ Branch 3 → 5 taken 16 times.
|
16 | g_return_val_if_fail (SHUMATE_IS_COORDINATE (location), 0.0); |
| 137 | |||
| 138 | 16 | return priv->latitude; | |
| 139 | } | ||
| 140 | |||
| 141 | |||
| 142 | static double | ||
| 143 | 16 | get_longitude (ShumateLocation *location) | |
| 144 | { | ||
| 145 | 16 | ShumateCoordinate *coordinate = SHUMATE_COORDINATE (location); | |
| 146 | 16 | ShumateCoordinatePrivate *priv = shumate_coordinate_get_instance_private (coordinate); | |
| 147 | |||
| 148 |
1/2✗ Branch 3 → 4 not taken.
✓ Branch 3 → 5 taken 16 times.
|
16 | g_return_val_if_fail (SHUMATE_IS_COORDINATE (location), 0.0); |
| 149 | |||
| 150 | 16 | return priv->longitude; | |
| 151 | } | ||
| 152 | |||
| 153 | |||
| 154 | static void | ||
| 155 | 3 | location_interface_init (ShumateLocationInterface *iface) | |
| 156 | { | ||
| 157 | 3 | iface->get_latitude = get_latitude; | |
| 158 | 3 | iface->get_longitude = get_longitude; | |
| 159 | 3 | iface->set_location = set_location; | |
| 160 | 3 | } | |
| 161 | |||
| 162 | static void | ||
| 163 | 3 | shumate_coordinate_class_init (ShumateCoordinateClass *coordinate_class) | |
| 164 | { | ||
| 165 | 3 | GObjectClass *object_class = G_OBJECT_CLASS (coordinate_class); | |
| 166 | 3 | object_class->get_property = shumate_coordinate_get_property; | |
| 167 | 3 | object_class->set_property = shumate_coordinate_set_property; | |
| 168 | |||
| 169 | 3 | g_object_class_override_property (object_class, | |
| 170 | PROP_LONGITUDE, | ||
| 171 | "longitude"); | ||
| 172 | |||
| 173 | 3 | g_object_class_override_property (object_class, | |
| 174 | PROP_LATITUDE, | ||
| 175 | "latitude"); | ||
| 176 | 3 | } | |
| 177 | |||
| 178 | |||
| 179 | static void | ||
| 180 | 4 | shumate_coordinate_init (ShumateCoordinate *coordinate) | |
| 181 | { | ||
| 182 | 4 | ShumateCoordinatePrivate *priv = shumate_coordinate_get_instance_private (coordinate); | |
| 183 | |||
| 184 | 4 | priv->latitude = 0.0; | |
| 185 | 4 | priv->longitude = 0.0; | |
| 186 | 4 | } | |
| 187 | |||
| 188 | |||
| 189 | /** | ||
| 190 | * shumate_coordinate_new: | ||
| 191 | * | ||
| 192 | * Creates a new instance of #ShumateCoordinate. | ||
| 193 | * | ||
| 194 | * Returns: the created instance. | ||
| 195 | */ | ||
| 196 | ShumateCoordinate * | ||
| 197 | ✗ | shumate_coordinate_new () | |
| 198 | { | ||
| 199 | ✗ | return SHUMATE_COORDINATE (g_object_new (SHUMATE_TYPE_COORDINATE, NULL)); | |
| 200 | } | ||
| 201 | |||
| 202 | |||
| 203 | /** | ||
| 204 | * shumate_coordinate_new_full: | ||
| 205 | * @latitude: the latitude coordinate | ||
| 206 | * @longitude: the longitude coordinate | ||
| 207 | * | ||
| 208 | * Creates a new instance of #ShumateCoordinate initialized with the given | ||
| 209 | * coordinates. | ||
| 210 | * | ||
| 211 | * Returns: the created instance. | ||
| 212 | */ | ||
| 213 | ShumateCoordinate * | ||
| 214 | 4 | shumate_coordinate_new_full (double latitude, | |
| 215 | double longitude) | ||
| 216 | { | ||
| 217 | 4 | return SHUMATE_COORDINATE (g_object_new (SHUMATE_TYPE_COORDINATE, | |
| 218 | "latitude", latitude, | ||
| 219 | "longitude", longitude, | ||
| 220 | NULL)); | ||
| 221 | } | ||
| 222 |