Max OS 0.3
Loading...
Searching...
No Matches
resource.cpp
Go to the documentation of this file.
1
10
11using namespace MaxOS;
12using namespace MaxOS::processes;
13
21Resource::Resource(const string& name, size_t flags, resource_type_t type)
22: m_name(name),
23 m_type(type)
24{
25
26}
27
28Resource::~Resource() = default;
29
35void Resource::open(size_t flags) {
36
37}
38
39
45void Resource::close(size_t flags) {
46
47}
48
57int Resource::read(void* buffer, size_t size, size_t flags) {
58 return 0;
59}
60
69int Resource::write(void const* buffer, size_t size, size_t flags) {
70
71 return 0;
72}
73
80 return m_name;
81}
82
89
90 return m_type;
91}
92
103
111
121
129
130 // Resource isn't stored in this registry
131 if(m_resources.find(name) == m_resources.end())
132 return nullptr;
133
134 // Increment the use count of the resource and return it
135 m_resource_uses[name]++;
136 return m_resources[name];
137
138}
139
147
148 // Resource name is already stored in this registry
149 if(m_resources.find(resource->name()) != m_resources.end())
150 return false;
151
152 m_resources.insert(resource->name(), resource);
153 m_resource_uses.insert(resource->name(), 0);
154 return true;
155}
156
157
164void BaseResourceRegistry::close_resource(Resource* resource, size_t flags) {
165
166 // Resource isn't stored in this registry
167 if(m_resources.find(resource->name()) == m_resources.end())
168 return;
169
170 m_resource_uses[resource->name()]--;
171
172 // Don't close a resource that has more processes using it
173 if(m_resource_uses[resource->name()])
174 return;
175
176 // Can safely close the resource
177 resource->close(flags);
178 m_resources.erase(resource->name());
179 m_resource_uses.erase(resource->name());
180 delete resource;
181}
182
190Resource* BaseResourceRegistry::create_resource(string const& name, size_t flags) {
191 return nullptr;
192}
193
198 s_current = this;
199
200}
201
206 if(s_current == this)
207 s_current = nullptr;
208
209}
210
218
219 auto registry = s_current->m_registries.find(type);
220 if(registry == s_current->m_registries.end())
221 return nullptr;
222
223 return registry->second;
224}
225
233
234 // Does it already exist?
235 if(s_current->m_registries.find(type) != s_current->m_registries.end())
236 return;
237
238 s_current->m_registries.insert(type, registry);
239}
240
247
248 // Does it already exist?
249 if(s_current->m_registries.find(registry->type()) == s_current->m_registries.end())
250 return;
251
252 s_current->m_registries.erase(registry->type());
253
254}
255
256ResourceManager::ResourceManager() = default;
257
262
263 // Collect all resources (as closing will break iteration)
265 for (const auto& kv : m_resources)
266 handles.push_back(kv.first);
267
268 // Close the resources
269 for (auto h : handles)
270 close_resource(h, 0);
271
272}
273
283
292uint64_t ResourceManager::open_resource(resource_type_t type, string const& name, size_t flags) {
293
294 // Get the resource
295 auto resource = GlobalResourceRegistry::get_registry(type) -> get_resource(name);
296 if(!resource)
297 return 0;
298
299 // Store it
300 m_resources.insert(m_next_handle, resource);
301
302 // Open it
303 resource->open(flags);
304 return m_next_handle++;
305}
306
313void ResourceManager::close_resource(uint64_t handle, size_t flags) {
314
315 auto it = m_resources.find(handle);
316 if(it == m_resources.end())
317 return;
318
319 // Remove it
320 Resource* resource = it->second;
321 m_resources.erase(handle);
322
323 // Close it
324 GlobalResourceRegistry::get_registry(resource->type()) -> close_resource(resource, flags);
325}
326
334
335 if(m_resources.find(handle) == m_resources.end())
336 return nullptr;
337
338 return m_resources[handle];
339}
340
348
349 for(const auto& resource : m_resources)
350 if(resource.second->name() == name)
351 return resource.second;
352
353 return nullptr;
354}
355
356
void insert(Key, Value)
Updates the value of an element, or adds a new element if it does not exist.
Definition map.h:253
void erase(Key)
Removes an element from the map.
Definition map.h:274
Stores the left, top, width and height of a rectangle.
Definition rectangle.h:22
Manages the creation, retention and destruction of resources of a certain type. Should be subclassed ...
Definition resource.h:54
resource_type_t m_type
The resource type that this registry manages.
Definition resource.h:60
resource_type_t type()
Gets the type of resources this registry handles.
Definition resource.cpp:117
virtual Resource * get_resource(const string &name)
Gets a resource from the registry by name.
Definition resource.cpp:128
virtual void close_resource(Resource *resource, size_t flags)
Close the resource provided, if it exists in this registry.
Definition resource.cpp:164
common::Map< string, uint64_t > m_resource_uses
The map of resource names to how many processes are using them.
Definition resource.h:58
common::Map< string, Resource * > m_resources
The map of resource names to resource instances.
Definition resource.h:57
~BaseResourceRegistry()
Destructor for BaseResourceRegistry, removes it from the global list.
Definition resource.cpp:107
BaseResourceRegistry(resource_type_t type)
Constructs a new BaseResourceRegistry object and registers it globally.
Definition resource.cpp:98
virtual bool register_resource(Resource *resource)
Registers a resource in the registry.
Definition resource.cpp:146
virtual Resource * create_resource(const string &name, size_t flags)
Try to create a new resource with the specified name, will fail if the name is in use.
Definition resource.cpp:190
static void add_registry(resource_type_t type, BaseResourceRegistry *registry)
Adds a registry to the global list if there is not already one for that type.
Definition resource.cpp:232
static BaseResourceRegistry * get_registry(resource_type_t type)
Gets a registry of a type.
Definition resource.cpp:217
~GlobalResourceRegistry()
Destructor for GlobalResourceRegistry, unsets it as the current global registry if it is.
Definition resource.cpp:205
static void remove_registry(BaseResourceRegistry *registry)
Adds a registry to the global list if there is not already one for that type.
Definition resource.cpp:246
GlobalResourceRegistry()
Constructs a new GlobalResourceRegistry and sets it as the current global registry.
Definition resource.cpp:197
Resource * get_resource(uint64_t handle)
Gets a resource based on the handle id.
Definition resource.cpp:333
void close_resource(uint64_t handle, size_t flags)
Un registers the resource and then closes it.
Definition resource.cpp:313
~ResourceManager()
Destructor for ResourceManager, closes all open resources.
Definition resource.cpp:261
common::Map< uint64_t, Resource * > resources()
Get the resources currently open.
Definition resource.cpp:279
uint64_t open_resource(resource_type_t type, const string &name, size_t flags)
Registers a resource with the resource manager and then opens it.
Definition resource.cpp:292
Represents a generic resource that can be opened, closed, read from and written to.
Definition resource.h:29
virtual void open(size_t flags)
Opens the resource.
Definition resource.cpp:35
virtual void close(size_t flags)
Closes the resource.
Definition resource.cpp:45
string name()
Gets the name of this resource.
Definition resource.cpp:79
resource_type_t type()
Gets the type of this resource.
Definition resource.cpp:88
virtual int write(const void *buffer, size_t size, size_t flags)
write a certain amount of bytes to a resource
Definition resource.cpp:69
Resource(const string &name, size_t flags, resource_type_t type)
Constructs a new Resource object.
Definition resource.cpp:21
virtual int read(void *buffer, size_t size, size_t flags)
read a certain amount of bytes from a resource
Definition resource.cpp:57
Defines classes for managing process resources such as files and devices.
::syscore::ResourceType resource_type_t
Alias to make the libsyscore ResourceType accessible here.
Definition resource.h:22