Debian Codegraph @

src/core/SessionPool.hpp

raw
1 /*
2 * This File is part of Davix, The IO library for HTTP based protocols
3 * Copyright (C) CERN 2019
4 * Author: Georgios Bitzes <[email protected]>
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 *
20 */
21
22 #ifndef DAVIX_CORE_SESSION_POOL_HPP
23 #define DAVIX_CORE_SESSION_POOL_HPP
24
25 #include <map>
26 #include <mutex>
27
28 //------------------------------------------------------------------------------
29 // Utility class to juggle sessions based on URI and parameters.
30 //------------------------------------------------------------------------------
31 template<typename T>
32 class SessionPool {
33 public:
34 //----------------------------------------------------------------------------
35 // Constructor
36 //----------------------------------------------------------------------------
37 SessionPool() {}
38
39 //----------------------------------------------------------------------------
40 // Destructor
41 //----------------------------------------------------------------------------
42 virtual ~SessionPool() {
43 clear();
44 }
45
46 //----------------------------------------------------------------------------
47 // Insert
48 //----------------------------------------------------------------------------
49 void insert(const std::string &key, T item) {
50 std::lock_guard<std::mutex> lock(_mutex);
51 _map.insert(std::pair<std::string, T>(key, std::move(item)));
52 }
53
54 //----------------------------------------------------------------------------
55 // Clear
56 //----------------------------------------------------------------------------
57 void clear() {
58 std::lock_guard<std::mutex> lock(_mutex);
59 _map.clear();
60 }
61
62 //----------------------------------------------------------------------------
63 // Retrieve: Remove from the map and copy value onto caller variable.
64 // Return true if value was found, returned and erased, and false otherwise.
65 //----------------------------------------------------------------------------
66 bool retrieve(const std::string &key, T& item) {
67 std::lock_guard<std::mutex> lock(_mutex);
68 auto it = _map.find(key);
69
70 if(it == _map.end()) {
71 return false;
72 }
73
74 item = it->second;
75 _map.erase(it);
76 return true;
77 }
78
79 private:
80 std::multimap<std::string, T> _map;
81 std::mutex _mutex;
82
83 };
84
85 #endif
86