3dec8dfc6e14a2ff0ffaa58c812802f46b3d48d1
[platform/core/location/lbs-location.git] / location / manager / location-accuracy.c
1 /*
2  * libslp-location
3  *
4  * Copyright (c) 2010-2013 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */
18
19 #ifdef HAVE_CONFIG_H
20 #include "config.h"
21 #endif
22
23 #include "location-accuracy.h"
24 #include "location-log.h"
25
26 GType
27 location_accuracy_get_type(void)
28 {
29         static volatile gsize type_volatile = 0;
30         if (g_once_init_enter(&type_volatile)) {
31                 GType type = g_boxed_type_register_static(g_intern_static_string("LocationAccuracy"),
32                                         (GBoxedCopyFunc) location_accuracy_copy, (GBoxedFreeFunc) location_accuracy_free);
33
34                 g_once_init_leave(&type_volatile, type);
35         }
36         return type_volatile;
37 }
38
39 EXPORT_API LocationAccuracy *
40 location_accuracy_new(LocationAccuracyLevel level, gdouble horizontal_accuracy, gdouble vertical_accuracy)
41 {
42         LocationAccuracy *accuracy = g_slice_new0(LocationAccuracy);
43         g_return_val_if_fail(accuracy, NULL);
44
45         accuracy->level = level;
46         accuracy->horizontal_accuracy = horizontal_accuracy;
47         accuracy->vertical_accuracy = vertical_accuracy;
48
49         return accuracy;
50 }
51
52 EXPORT_API void
53 location_accuracy_free(LocationAccuracy *accuracy)
54 {
55         g_return_if_fail(accuracy);
56         g_slice_free(LocationAccuracy, accuracy);
57 }
58
59 static int
60 comp_int(int a, int b)
61 {
62         if (a < b) return -1;
63         if (a == b) return 0;
64         return 1;
65 }
66
67 static int
68 comp_double_reverse(double a, double b)
69 {
70         if (a > b) return -1;
71         else if (a == b) return 0;
72         return 1;
73 }
74
75 EXPORT_API int
76 location_accuracy_level_compare(const LocationAccuracy *accuracy1, const LocationAccuracy *accuracy2)
77 {
78         g_return_val_if_fail(accuracy1, -1);
79         g_return_val_if_fail(accuracy2, 1);
80
81         return comp_int(accuracy1->level, accuracy2->level);
82 }
83
84 EXPORT_API int
85 location_accuracy_compare(const LocationAccuracy *accuracy1, const LocationAccuracy *accuracy2)
86 {
87         int ret = 0;
88         ret = location_accuracy_level_compare(accuracy1, accuracy2);
89         if (!ret) {
90                 ret = comp_double_reverse(accuracy1->horizontal_accuracy, accuracy2->horizontal_accuracy);
91                 if (!ret) return comp_double_reverse(accuracy1->vertical_accuracy, accuracy2->vertical_accuracy);
92         }
93         return ret;
94 }
95
96 EXPORT_API LocationAccuracy *
97 location_accuracy_copy(const LocationAccuracy *accuracy)
98 {
99         g_return_val_if_fail(accuracy, NULL);
100         return location_accuracy_new(accuracy->level, accuracy->horizontal_accuracy, accuracy->vertical_accuracy);
101 }