12using namespace MaxOS::processes;
28Resource::~Resource() =
default;
177 resource->
close(flags);
206 if(s_current ==
this)
219 auto registry = s_current->m_registries.find(type);
220 if(registry == s_current->m_registries.end())
223 return registry->second;
235 if(s_current->m_registries.find(type) != s_current->m_registries.end())
238 s_current->m_registries.insert(type, registry);
249 if(s_current->m_registries.find(registry->
type()) == s_current->m_registries.end())
252 s_current->m_registries.erase(registry->
type());
256ResourceManager::ResourceManager() =
default;
265 for (
const auto& kv : m_resources)
266 handles.push_back(kv.first);
269 for (
auto h : handles)
300 m_resources.insert(m_next_handle, resource);
303 resource->open(flags);
304 return m_next_handle++;
315 auto it = m_resources.find(handle);
316 if(it == m_resources.end())
321 m_resources.erase(handle);
335 if(m_resources.find(handle) == m_resources.end())
338 return m_resources[handle];
349 for(
const auto& resource : m_resources)
350 if(resource.second->name() == name)
351 return resource.second;
void insert(Key, Value)
Updates the value of an element, or adds a new element if it does not exist.
void erase(Key)
Removes an element from the map.
Stores the left, top, width and height of a rectangle.
Manages the creation, retention and destruction of resources of a certain type. Should be subclassed ...
resource_type_t m_type
The resource type that this registry manages.
resource_type_t type()
Gets the type of resources this registry handles.
virtual Resource * get_resource(const string &name)
Gets a resource from the registry by name.
virtual void close_resource(Resource *resource, size_t flags)
Close the resource provided, if it exists in this registry.
common::Map< string, uint64_t > m_resource_uses
The map of resource names to how many processes are using them.
common::Map< string, Resource * > m_resources
The map of resource names to resource instances.
~BaseResourceRegistry()
Destructor for BaseResourceRegistry, removes it from the global list.
BaseResourceRegistry(resource_type_t type)
Constructs a new BaseResourceRegistry object and registers it globally.
virtual bool register_resource(Resource *resource)
Registers a resource in the registry.
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.
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.
static BaseResourceRegistry * get_registry(resource_type_t type)
Gets a registry of a type.
~GlobalResourceRegistry()
Destructor for GlobalResourceRegistry, unsets it as the current global registry if it is.
static void remove_registry(BaseResourceRegistry *registry)
Adds a registry to the global list if there is not already one for that type.
GlobalResourceRegistry()
Constructs a new GlobalResourceRegistry and sets it as the current global registry.
Resource * get_resource(uint64_t handle)
Gets a resource based on the handle id.
void close_resource(uint64_t handle, size_t flags)
Un registers the resource and then closes it.
~ResourceManager()
Destructor for ResourceManager, closes all open resources.
common::Map< uint64_t, Resource * > resources()
Get the resources currently open.
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.
Represents a generic resource that can be opened, closed, read from and written to.
virtual void open(size_t flags)
Opens the resource.
virtual void close(size_t flags)
Closes the resource.
string name()
Gets the name of this resource.
resource_type_t type()
Gets the type of this resource.
virtual int write(const void *buffer, size_t size, size_t flags)
write a certain amount of bytes to a resource
Resource(const string &name, size_t flags, resource_type_t type)
Constructs a new Resource object.
virtual int read(void *buffer, size_t size, size_t flags)
read a certain amount of bytes from a resource
Defines classes for managing process resources such as files and devices.
::syscore::ResourceType resource_type_t
Alias to make the libsyscore ResourceType accessible here.