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