Imported Upstream version 1.0.1
[platform/upstream/iotivity.git] / service / simulator / src / client / request_list.h
1 /******************************************************************
2  *
3  * Copyright 2015 Samsung Electronics All Rights Reserved.
4  *
5  *
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  *
19  ******************************************************************/
20
21 #ifndef REQUEST_LIST_H_
22 #define REQUEST_LIST_H_
23
24 #include <map>
25 #include <mutex>
26
27 template <typename T>
28 class RequestList
29 {
30     public:
31         RequestList() : m_id(0) {}
32
33         int add(T request)
34         {
35             if (!request)
36                 return -1;
37
38             std::lock_guard<std::recursive_mutex> lock(m_listMutex);
39             m_requestList[m_id++] = request;
40             return m_id - 1;
41         }
42
43         T get(int id)
44         {
45             std::lock_guard<std::recursive_mutex> lock(m_listMutex);
46             if (m_requestList.end() != m_requestList.find(id))
47                 return m_requestList[id];
48
49             return nullptr;
50         }
51
52         T remove(int id)
53         {
54             std::lock_guard<std::recursive_mutex> lock(m_listMutex);
55             if (m_requestList.end() != m_requestList.find(id))
56             {
57                 T request = m_requestList[id];
58                 m_requestList.erase(m_requestList.find(id));
59                 return request;
60             }
61
62             return nullptr;
63         }
64
65         int size()
66         {
67             std::lock_guard<std::recursive_mutex> lock(m_listMutex);
68             return m_requestList.size();
69         }
70
71         void clear()
72         {
73             std::lock_guard<std::recursive_mutex> lock(m_listMutex);
74             m_requestList.clear();
75         }
76
77     private:
78         int m_id;
79         std::recursive_mutex m_listMutex;
80         std::map<int, T> m_requestList;
81 };
82
83 #endif