AnCH Framework 0.2
Another C++ Hack: C++23 utility libraries
Loading...
Searching...
No Matches
resourcePool.hpp
1/*
2 ANCH Framework: ANother C++ Hack is a C++ framework based on C++23 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 <https://www.gnu.org/licenses/>.
19*/
20#pragma once
21
22#include <chrono>
23#include <concepts>
24#include <condition_variable>
25#include <cstddef>
26#include <deque>
27#include <exception>
28#include <memory>
29#include <mutex>
30#include <stdexcept>
31#include <string>
32
33
34namespace anch {
35
45 template<typename T>
46 concept Validable = requires(const T& object) {
47 {object.isValid()} -> std::convertible_to<bool>;
48 };
49
57 template<typename T>
58 concept Releasable = requires(T& object) {
59 object.release();
60 };
61
71 class TimeoutException: public std::exception {
72
73 // Attributes +
74 private:
76 std::chrono::milliseconds _timeout;
77
79 std::string _msg;
80 // Attributes -
81
82 // Constructors +
83 public:
90 TimeoutException(const std::chrono::milliseconds& timeout, std::string msg) noexcept: std::exception(),
91 _timeout(timeout),
92 _msg(std::move(msg)) {
93 }
94 // Constructors -
95
96 // Accessors +
97 public:
103 const char* what() const noexcept override {
104 return _msg.data();
105 }
106
112 inline const std::chrono::milliseconds& getTimeout() const noexcept {
113 return _timeout;
114 }
115 // Accessors -
116
117 };
118
130 template<anch::Validable T>
132
133 // Attributes +
135 std::mutex _mutex;
136
141 std::deque<std::condition_variable*> _waiters;
142
144 std::deque<std::shared_ptr<T>> _availables;
145
147 std::size_t _used = 0;
148
150 bool _closed = false;
151 // Attributes -
152
153 // Methods +
162 void release(std::shared_ptr<T> res, bool valid) noexcept;
163
167 void notifyFirstWaiter() noexcept;
168 // Methods -
169
170 };
171
182 template<anch::Validable T>
184
185 // Attributes +
186 private:
188 std::shared_ptr<anch::ResourcePoolState<T>> _state;
189
191 std::shared_ptr<T> _ptr;
192 // Attributes -
193
194 // Constructors +
195 public:
202 PoolableResource(std::shared_ptr<anch::ResourcePoolState<T>> state, std::shared_ptr<T> ptr) noexcept;
203
208
214 PoolableResource(PoolableResource&& other) noexcept = default;
215 // Constructors -
216
217 // Destructor +
218 public:
224 // Destructor -
225
226 // Operators +
227 public:
231 PoolableResource& operator=(const PoolableResource&) = delete;
232
236 PoolableResource& operator=(PoolableResource&&) = delete;
237 // Operators -
238
239 // Accessors +
240 public:
246 T& get() noexcept;
247 // Accessors -
248
249 };
250
272 template<anch::Validable T, typename C, std::shared_ptr<T>(*make_ptr)(const C&) = std::make_shared<T> >
274
275 // Attributes +
276 private:
278 std::shared_ptr<anch::ResourcePoolState<T>> _state;
279
281 std::size_t _maxSize;
282
284 C _config;
285
287 std::chrono::milliseconds _timeout;
288 // Attributes -
289
290 // Constructors +
291 public:
303 ResourcePool(const C& config,
304 std::size_t maxSize,
305 std::size_t initialSize = 0,
306 std::chrono::milliseconds timeout = std::chrono::milliseconds(100));
307
311 ResourcePool(const ResourcePool&) = delete;
312
317 // Constructors -
318
319 // Destructor +
320 public:
326 // Destructor -
327
328 // Operators +
329 public:
334
339 // Operators -
340
341 // Methods +
342 public:
353 // Methods -
354
355 // Accessors +
356 public:
362 void setTimeout(std::chrono::milliseconds timeout);
363 // Accessors -
364
365 };
366
367}
368
369#include "impl/resourcePool.hpp"
Generic poolable resource.
Definition resourcePool.hpp:183
~PoolableResource() noexcept
PoolableResource(const PoolableResource &)=delete
PoolableResource(PoolableResource &&other) noexcept=default
T & get() noexcept
PoolableResource(std::shared_ptr< anch::ResourcePoolState< T > > state, std::shared_ptr< T > ptr) noexcept
ResourcePool & operator=(ResourcePool &&)=delete
anch::PoolableResource< T > borrowResource()
ResourcePool & operator=(const ResourcePool &)=delete
void setTimeout(std::chrono::milliseconds timeout)
ResourcePool(const C &config, std::size_t maxSize, std::size_t initialSize=0, std::chrono::milliseconds timeout=std::chrono::milliseconds(100))
ResourcePool(const ResourcePool &)=delete
ResourcePool(ResourcePool &&)=delete
TimeoutException(const std::chrono::milliseconds &timeout, std::string msg) noexcept
Definition resourcePool.hpp:90
const char * what() const noexcept override
Definition resourcePool.hpp:103
const std::chrono::milliseconds & getTimeout() const noexcept
Definition resourcePool.hpp:112
Definition resourcePool.hpp:58
Definition resourcePool.hpp:46
AnCH framework base namespace.
Definition app.hpp:30
Resource pool shared state (internal usage).
Definition resourcePool.hpp:131
std::size_t _used
Definition resourcePool.hpp:147
std::mutex _mutex
Definition resourcePool.hpp:135
std::deque< std::shared_ptr< T > > _availables
Definition resourcePool.hpp:144
void release(std::shared_ptr< T > res, bool valid) noexcept
std::deque< std::condition_variable * > _waiters
Definition resourcePool.hpp:141
void notifyFirstWaiter() noexcept
bool _closed
Definition resourcePool.hpp:150