Merge branch 'tizen_3.0' into devel/tizen
[platform/core/system/sensord.git] / src / client / sensor_info_manager.cpp
1 /*
2  * sensord
3  *
4  * Copyright (c) 2013 Samsung Electronics Co., Ltd.
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
20 #include <sensor_info_manager.h>
21 #include <utility>
22 #include <vector>
23
24 using std::pair;
25 using std::make_pair;
26 using std::vector;
27
28 sensor_info_manager::sensor_info_manager()
29 {
30 }
31
32 sensor_info_manager::~sensor_info_manager()
33 {
34         auto it_info = m_sensor_infos.begin();
35
36         while (it_info != m_sensor_infos.end()) {
37                 delete it_info->second;
38                 ++it_info;
39         }
40 }
41
42 sensor_info_manager& sensor_info_manager::get_instance(void)
43 {
44         static sensor_info_manager inst;
45         return inst;
46 }
47
48 const sensor_info* sensor_info_manager::get_info(sensor_type_t type)
49 {
50         auto it_info = m_sensor_infos.find(type);
51
52         if (it_info == m_sensor_infos.end())
53                 return NULL;
54
55         return it_info->second;
56 }
57
58 void sensor_info_manager::add_info(sensor_info* info)
59 {
60         m_sensor_infos.insert(make_pair(info->get_type(), info));
61         m_id_to_info_map.insert(make_pair(info->get_id(), info));
62         m_info_set.insert(info);
63 }
64
65 vector<sensor_info *> sensor_info_manager::get_infos(sensor_type_t type)
66 {
67         vector<sensor_info *> sensor_infos;
68
69         pair<sensor_infos::iterator, sensor_infos::iterator> ret;
70
71         if (type == ALL_SENSOR)
72                 ret = std::make_pair(m_sensor_infos.begin(), m_sensor_infos.end());
73         else
74                 ret = m_sensor_infos.equal_range(type);
75
76         for (auto it_info = ret.first; it_info != ret.second; ++it_info)
77                 sensor_infos.push_back(it_info->second);
78
79         return sensor_infos;
80 }
81
82 const sensor_info* sensor_info_manager::get_info(sensor_id_t id)
83 {
84         auto it_info = m_id_to_info_map.find(id);
85
86         if (it_info == m_id_to_info_map.end())
87                 return NULL;
88
89         return it_info->second;
90 }
91
92 bool sensor_info_manager::is_valid(sensor_info* info)
93 {
94         auto it_info = m_info_set.find(info);
95
96         if (it_info == m_info_set.end())
97                 return false;
98
99         return true;
100 }