f206a913cc26b0d1535c6e0f400277a826bfbf24
[iec.git] / src / type3_AndroidCloud / anbox-master / src / anbox / network / connections.h
1 /*
2  * Copyright © 2012-2014 Canonical Ltd.
3  *
4  * This program is free software: you can redistribute it and/or modify it
5  * under the terms of the GNU General Public License version 3,
6  * as published by the Free Software Foundation.
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11  * GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
15  *
16  * Authored by: Alan Griffiths <alan@octopull.co.uk>
17  */
18
19 #ifndef ANBOX_NETWORK_CONNECTIONS_H_
20 #define ANBOX_NETWORK_CONNECTIONS_H_
21
22 #include <map>
23 #include <memory>
24 #include <mutex>
25
26 namespace anbox {
27 namespace network {
28 template <class Connection>
29 class Connections {
30  public:
31   Connections() {}
32   ~Connections() { clear(); }
33
34   void add(std::shared_ptr<Connection> const& connection) {
35     std::unique_lock<std::mutex> lock(mutex);
36     connections.insert({connection->id(), connection});
37   }
38
39   void remove(int id) {
40     std::unique_lock<std::mutex> lock(mutex);
41     connections.erase(id);
42   }
43
44   bool includes(int id) const {
45     std::unique_lock<std::mutex> lock(mutex);
46     return connections.find(id) != connections.end();
47   }
48
49   void clear() {
50     std::unique_lock<std::mutex> lock(mutex);
51     connections.clear();
52   }
53
54   size_t size() { return connections.size(); }
55
56   std::shared_ptr<Connection> at(size_t n) { return connections.at(n); }
57
58  private:
59   Connections(Connections const&) = delete;
60   Connections& operator=(Connections const&) = delete;
61
62   std::mutex mutex;
63   std::map<int, std::shared_ptr<Connection>> connections;
64 };
65 }  // namespace anbox
66 }  // namespace network
67
68 #endif