AnCH Framework 0.1
Another C++ Hack Framework
 
Loading...
Searching...
No Matches
resourcePool.hpp
1/*
2 ANCH Framework: ANother C++ Hack is a C++ framework based on C++11 standard
3 Copyright (C) 2012 Vincent Lachenal
4
5 This file is part of ANCH Framework.
6
7 ANCH Framework is free software: you can redistribute it and/or modify
8 it under the terms of the GNU Lesser General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
11
12 ANCH Framework is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU Lesser General Public License for more details.
16
17 You should have received a copy of the GNU Lesser General Public License
18 along with ANCH Framework. If not, see <http://www.gnu.org/licenses/>.
19*/
20#pragma once
21
22#include <memory>
23#include <mutex>
24#include <chrono>
25#include <condition_variable>
26#include <deque>
27#include <atomic>
28#include <string>
29
30
31namespace anch {
32
42 template<typename T>
43 concept Validable = requires(const T& object) {
44 {object.isValid()} -> std::convertible_to<bool>;
45 };
46
56 class TimeoutException: std::exception {
57
58 // Attributes +
59 private:
61 std::chrono::milliseconds _timeout;
62
64 std::string _msg;
65 // Attributes -
66
67 // Constructors +
68 public:
75 TimeoutException(const std::chrono::milliseconds& timeout, const std::string msg) noexcept: std::exception(),
76 _timeout(timeout),
77 _msg(msg) {
78 }
79 // Constructors -
80
81 // Destructor +
82 public:
86 virtual ~TimeoutException() {
87 // nothing to do
88 }
89 // Destructor -
90
91 // Accessors +
92 public:
98 virtual const char* what() const noexcept {
99 return _msg.data();
100 }
101
107 inline const std::chrono::milliseconds& getTimeout() const noexcept {
108 return _timeout;
109 }
110 // Accessors -
111
112 };
113
114 // PoolableResource internal usage class declaration ; you can look at implementation but you should use auto keyword
115 template<anch::Validable T, typename C, std::shared_ptr<T>(*make_ptr)(const C&)>
116 class PoolableResource;
117
135 template<anch::Validable T, typename C, std::shared_ptr<T>(*make_ptr)(const C&) = std::make_shared<T> >
136 class ResourcePool {
137
138 friend class PoolableResource<T,C,make_ptr>;
139
140 // Attributes +
141 private:
143 std::mutex _mutex;
144
146 std::condition_variable _wait;
147
149 std::mutex _waitex;
150
152 std::deque<std::shared_ptr<T> > _availables;
153
155 std::size_t _maxSize;
156
158 std::atomic_size_t _used;
159
161 C _config;
162
164 std::chrono::milliseconds _timeout;
165 // Attributes -
166
167 // Constructors +
168 public:
177 ResourcePool(const C& config,
178 std::size_t maxSize,
179 std::size_t initialiSize = 0,
180 std::chrono::milliseconds timeout = std::chrono::milliseconds(100));
181
185 ResourcePool(const ResourcePool&) = delete;
186 // Constructors -
187
188 // Destructor +
189 public:
194 virtual ~ResourcePool();
195 // Destructor -
196
197 // Methods +
198 public:
208 anch::PoolableResource<T,C,make_ptr> borrowResource();
209
210 private:
216 void returnResource(std::shared_ptr<T> res);
217
224 void invalidateResource(std::shared_ptr<T> res);
225 // Methods -
226
227 // Accessors +
228 public:
234 void setTimeout(const std::chrono::milliseconds timeout);
235 // Accessors -
236
237 };
238
239}
240
241#include "impl/resourcePool.hpp"
AnCH framework base namespace.
Definition app.hpp:28