pars 0.2.1
Loading...
Searching...
No Matches
resources.h
Go to the documentation of this file.
1/*
2Copyright (c) 2025 Giuseppe Roberti.
3All rights reserved.
4
5Redistribution and use in source and binary forms, with or without modification,
6are permitted provided that the following conditions are met:
7
81. Redistributions of source code must retain the above copyright notice, this
9list of conditions and the following disclaimer.
10
112. Redistributions in binary form must reproduce the above copyright notice,
12this list of conditions and the following disclaimer in the documentation and/or
13other materials provided with the distribution.
14
153. Neither the name of the copyright holder nor the names of its contributors
16may be used to endorse or promote products derived from this software without
17specific prior written permission.
18
19THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
20ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
21WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
23ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
26ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29*/
30#pragma once
31
32#include "pars/log.h"
33
34#include <format>
35#include <mutex>
36#include <stdexcept>
37#include <unordered_map>
38
39namespace pars::app
40{
41
42template<typename resource_t, typename mtx_t>
44{
45 using resource_type = resource_t;
46 using mutex_type = mtx_t;
47
50};
51
52template<typename resource_t, typename mutex_t, typename key_t,
53 typename lock_t = std::unique_lock<mutex_t>>
54struct locked
55{
56public:
57 using resource_type = resource_t;
58 using mutex_type = mutex_t;
59 using key_type = key_t;
60 using lock_type = lock_t;
61
63 : lockable_m{l}
64 , key_m{k}
65 , guard_m{lock_type{l.mutex}}
66 {
67 }
68
69 auto& resource() { return lockable_m.resource; }
70
71 auto& guard() { return guard_m; }
72
73 auto& key() { return key_m; }
74
75private:
77 key_type key_m;
78 lock_type guard_m;
79};
80
81template<typename key_t, typename resource_t, typename mutex_t = std::mutex>
83{
84public:
85 using key_type = key_t;
86 using resource_type = resource_t;
87 using mutex_type = mutex_t;
88
90
91 std::size_t count()
92 {
93 auto guard = std::lock_guard{mtx_m};
94
95 return resources_m.size();
96 }
97
98 bool contains(const key_type key)
99 {
100 auto guard = std::lock_guard{mtx_m};
101
102 return resources_m.contains(key);
103 }
104
105 template<class... args_t>
106 auto find_or_emplace(const key_type key, args_t&&... args)
107 {
108 if (resources_m.contains(key))
109 return find_locked(key, resources_m);
110 return emplace(key, std::forward<args>(args)...);
111 }
112
113 template<class... args_t>
114 auto emplace(const key_type key, args_t&&... args)
115 {
116 auto guard = std::lock_guard{mtx_m};
117
118 auto mtx = mtxs_m.try_emplace(key);
119
120 if (!mtx.second)
121 throw std::runtime_error(std::format(
122 "Unable to emplace a new Resource Mutex [key: 0x{:X}]", key));
123
124 auto res = resources_m.try_emplace(key, std::forward<args_t>(args)...);
125
126 if (!res.second)
127 {
128 mtxs_m.erase(mtx.first);
129
130 throw std::runtime_error(
131 std::format("Unable to emplace a new Resource [key: 0x{:X}]", key));
132 }
133
134 pars::debug(SL, lf::app, "Emplaced Resource [key: 0x{:X}]", key);
135
136 return locked{lockable{res.first->second, mtx.first->second}, key};
137 }
138
141 {
142 return find_locked(k, resources_m);
143 }
144
146 {
147 auto guard = std::lock_guard{mtx_m};
148
149 {
150 auto guard = std::lock_guard{mtxs_m.at(k)};
151
152 resources_m.erase(k);
153 }
154
155 mtxs_m.erase(k);
156 }
157
158private:
159 auto find_locked(const key_type key,
160 std::unordered_map<key_type, resource_type>& xs)
162 {
163 auto guard = std::lock_guard{mtx_m};
164
165 if (!xs.contains(key))
166 throw std::out_of_range(
167 std::format("Object not found for Key 0x{:X}", key));
168
169 auto& x = xs.at(key);
170
171 return locked{lockable{x, mtxs_m.at(key)}, key};
172 }
173
174 std::mutex mtx_m;
175 std::unordered_map<key_type, mutex_type>
176 mtxs_m;
177 std::unordered_map<key_type, resource_type> resources_m;
178};
179
180} // namespace pars::app
bool contains(const key_type key)
Definition resources.h:98
std::size_t count()
Definition resources.h:91
resource_t resource_type
Definition resources.h:86
auto emplace(const key_type key, args_t &&... args)
Definition resources.h:114
auto find_or_emplace(const key_type key, args_t &&... args)
Definition resources.h:106
void delete_resource(key_type k)
Definition resources.h:145
auto locked_resource(const key_type k) -> locked< resource_type, mutex_type, key_type >
Definition resources.h:139
#define SL
Definition log.h:58
@ app
Definition flags.h:41
void debug(spdlog::source_loc loc, pars::lf lf, spdlog::format_string_t< args_t... > fmt, args_t &&... args)
Definition log.h:129
resource_t resource_type
Definition resources.h:45
mutex_type & mutex
Definition resources.h:49
resource_type & resource
Definition resources.h:48
auto & resource()
Definition resources.h:69
mutex_t mutex_type
Definition resources.h:58
resource_t resource_type
Definition resources.h:57
locked(lockable< resource_type, mutex_type > l, key_type k)
Definition resources.h:62