aaa14728357632d0c39b3e3c2a40df85428051ca
[framework/location/maps-service.git] / test / src / util / maps_object.cpp
1 /* Copyright (c) 2010-2014 Samsung Electronics Co., Ltd. All rights reserved.
2  *
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #include "maps_object.h"
18 #include "maps_address.h"
19 #include "maps_error.h"
20 #include <glib.h>
21
22 bool __equal_double(const double &d1, const double &d2)
23 {
24         static double delta = 0.0000001;
25         if (d1 >= (d2 + delta))
26                 return false;
27         if (d1 <= (d2 - delta))
28                 return false;
29         return true;
30 }
31
32 /*----------------------------------------------------------------------------*/
33 /* Simple type library, helpful for operating with Maps Objects */
34
35 /* Coordinates */
36  maps::coordinates::coordinates():object(NULL)
37 {
38 }
39
40  maps::coordinates::coordinates(const double &lat, const double &lon):object(NULL)
41 {
42         maps_coordinates_create(lat, lon, &handle);
43 }
44
45 maps::coordinates::coordinates(const maps_coordinates_s & c):object(NULL)
46 {
47         maps_coordinates_create(c.latitude, c.longitude, &handle);
48 }
49
50 maps::coordinates::coordinates(const coordinates & c):object(NULL)
51 {
52         *this = c;
53 }
54
55 maps::coordinates & maps::coordinates::operator=(const coordinates & c)
56 {
57         if (this != &c) {
58                 maps_coordinates_destroy(handle);
59                 maps_coordinates_clone(c.handle, &handle);
60         }
61         return* this;
62 }
63
64 maps::coordinates::~coordinates()
65 {
66 }
67
68 bool maps::coordinates::operator==(const coordinates & c) const
69 {
70         if (this == &c)
71                 return true;
72         if (!__equal_double(get_latitude(), c.get_latitude()))
73                 return false;
74         if (!__equal_double(get_longitude(), c.get_longitude()))
75                 return false;
76         return true;
77 }
78
79 bool maps::coordinates::operator!=(const coordinates & c) const
80 {
81         return !(*this == c);
82 }
83
84 double maps::coordinates::get_latitude() const
85 {
86         double lat = 0;
87         if (handle)
88                 maps_coordinates_get_latitude(handle, &lat);
89         return lat;
90 }
91
92 double maps::coordinates::get_longitude() const
93 {
94         double lon = 0;
95         if (handle)
96                 maps_coordinates_get_longitude(handle, &lon);
97         return lon;
98 }
99
100 /* Area */
101  maps::area::area():object(NULL)
102 {
103 }
104
105 maps::area::~area()
106 {
107 }
108
109  maps::area::area(const coordinates & lt, const coordinates & rb):object(NULL)
110 {
111         maps_area_create_rectangle(lt, rb, &handle);
112 }
113
114 maps::area::area(const double &l, const double &t, const double &r,
115         const double &b):object(NULL)
116 {
117         maps_area_create_rectangle(coordinates(l, t), coordinates(r, b),
118                 &handle);
119 }
120
121 maps::area::area(const coordinates & c, const double &r):object(NULL)
122 {
123         maps_area_create_circle(c, r, &handle);
124 }
125
126 maps::area::area(const double &x, const double &y, const double &r):object(NULL)
127 {
128         maps_area_create_circle(coordinates(x, y), r, &handle);
129 }
130
131 maps::area::area(const area & a):object(NULL)
132 {
133         *this = a;
134 }
135
136 maps::area & maps::area::operator=(const area & a)
137 {
138         if (this != &a) {
139                 maps_area_destroy(handle);
140                 maps_area_clone(a.handle, &handle);
141         }
142         return* this;
143 }
144
145 maps_area_type_e maps::area::get_type() const
146 {
147         if (!handle)
148                 return MAPS_AREA_NONE;
149         maps_area_s* a = (maps_area_s*) handle;
150         return a->type;
151 }
152
153 maps::coordinates maps::area::get_top_left() const
154 {
155         if (!handle)
156                 return coordinates(.0, .0);
157         maps_area_s* a = (maps_area_s*) handle;
158         return coordinates(a->rect.top_left);
159 }
160
161 maps::coordinates maps::area::get_bottom_right() const
162 {
163         if (!handle)
164                 return coordinates(.0, .0);
165         maps_area_s* a = (maps_area_s*) handle;
166         return coordinates(a->rect.bottom_right);
167 }
168
169 maps::coordinates maps::area::get_center() const
170 {
171         if (!handle)
172                 return coordinates(.0, .0);
173         maps_area_s* a = (maps_area_s*) handle;
174         return coordinates(a->circle.center);
175 }
176
177 double maps::area::get_radius() const
178 {
179         if (!handle)
180                 return .0;
181         maps_area_s* a = (maps_area_s*) handle;
182         return a->circle.radius;
183 }
184
185 bool maps::area::operator==(const area & a) const
186 {
187         if (this == &a)
188                 return true;
189         if (get_type() != a.get_type())
190                 return false;
191         switch (get_type()) {
192         case MAPS_AREA_RECTANGLE:
193                 if (get_top_left() != a.get_top_left())
194                         return false;
195                 if (get_bottom_right() != a.get_bottom_right())
196                         return false;
197                 break;
198         case MAPS_AREA_CIRCLE:
199                 if (get_center() != a.get_center())
200                         return false;
201                 if (!__equal_double(get_radius(), a.get_radius()))
202                         return false;
203                 break;
204         case MAPS_AREA_NONE:
205         default:
206                 break;
207         }
208         return true;
209 }
210
211 bool maps::area::operator!=(const area & a) const
212 {
213         return !(*this == a);
214 }
215
216 /*----------------------------------------------------------------------------*/
217
218 maps::string_holder::~string_holder()
219 {
220         g_free(str);
221 }
222