Max OS 0.3
Loading...
Searching...
No Matches
resource.h
Go to the documentation of this file.
1
9#ifndef MAXOS_PROCESSES_RESOURCE_H
10#define MAXOS_PROCESSES_RESOURCE_H
11
12#include <cstddef>
13#include <common/map.h>
14#include <common/vector.h>
15#include <common/string.h>
16#include <common/logger.h>
17#include <syscalls.h>
18
19
20namespace MaxOS::processes {
21
22 typedef ::syscore::ResourceType resource_type_t;
23 typedef ::syscore::ResourceErrorBase resource_error_base_t;
24
29 class Resource {
30
31 private:
32 string m_name;
33 resource_type_t m_type;
34
35 public:
36
37 Resource(const string& name, size_t flags, resource_type_t type);
38 virtual ~Resource();
39
40 string name();
42
43 virtual void open(size_t flags);
44 virtual void close(size_t flags);
45
46 virtual int read(void* buffer, size_t size, size_t flags);
47 virtual int write(const void* buffer, size_t size, size_t flags);
48 };
49
55
56 protected:
59
61
62 public:
65
67
68 virtual Resource* get_resource(const string& name);
69 virtual bool register_resource(Resource* resource);
70
71 virtual void close_resource(Resource* resource, size_t flags);
72 virtual Resource* create_resource(const string& name, size_t flags);
73 };
74
81 template<class Type> class ResourceRegistry : public BaseResourceRegistry {
82
83 public:
85 ~ResourceRegistry() = default;
86
94 Resource* create_resource(const string& name, size_t flags) override {
95
96 auto resource = new Type(name, flags, type());
97
98 // Creation failed
99 if(!register_resource(resource)) {
100 delete resource;
101 return nullptr;
102 }
103
104 return resource;
105 }
106 };
107
116
122
123 private:
125 inline static GlobalResourceRegistry* s_current;
126
127 public:
130
132
133 static void add_registry(resource_type_t type, BaseResourceRegistry* registry);
134 static void remove_registry(BaseResourceRegistry* registry);
135 };
136
142
143 private:
145
146 uint64_t m_next_handle = 1;
147
148 public:
151
153
154 uint64_t open_resource(resource_type_t type, const string& name, size_t flags);
155 void close_resource(uint64_t handle, size_t flags);
156
157 Resource* get_resource(uint64_t handle);
158 Resource* get_resource(const string& name);
159 };
160}
161
162
163#endif //MAXOS_PROCESSES_RESOURCE_H
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
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
Manages all the resource registries for each resource type.
Definition resource.h:121
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
Manages the open resources for a process.
Definition resource.h:141
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
A resource registry for a specific resource type.
Definition resource.h:81
Resource * create_resource(const string &name, size_t flags) override
Create a new resource of the specific type.
Definition resource.h:94
ResourceRegistry(resource_type_t type)
Constructor for ResourceRegistry of a specific type.
Definition resource.h:114
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
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 a Logger class for logging messages with different severity levels to multiple output streams...
Defines a Map class for storing key-value pairs.
::syscore::ResourceErrorBase resource_error_base_t
Alias to make the libsyscore ResourceErrorBase accessible here.
Definition resource.h:23
::syscore::ResourceType resource_type_t
Alias to make the libsyscore ResourceType accessible here.
Definition resource.h:22
Defines a String class for dynamically sized strings with various operations.
Defines the SyscallManager class for handling system calls from userspace applications.
Defines a Vector class for dynamically storing an array of elements.