|
CeresEngine 0.2.0
A game development framework
|
A context for function object execution. More...
#include <CeresEngine/Foundation/Threading/ExecutionContext.hpp>
Classes | |
| class | Scope |
| Scope object that sets the current execution context as current. More... | |
| class | Service |
Public Types | |
| using | ID = id |
| using | executor_type = AnyExecutor |
Public Member Functions | |
| virtual | ~ExecutionContext () noexcept=default |
| Virtual destructor for the execution context object. | |
| template<typename T > | |
| bool | hasService () noexcept |
Determine if the ExecutionContext contains the specified service type. | |
| template<typename T > | |
| T & | getService () noexcept |
| Obtain the service object corresponding to the given type. | |
| template<typename T > | |
| T * | getServiceIf () noexcept |
| Obtain the service object corresponding to the given type. | |
| virtual AnyExecutor | getExecutor () noexcept=0 |
| Returns an executor that run execute scheduled commands on the context. | |
| executor_type | get_executor () noexcept |
| Returns an executor that run execute scheduled commands on the context. | |
| bool | isActive () const noexcept |
| Checks if the execution context is currently active. | |
Static Public Member Functions | |
| static ExecutionContext * | getCurrent () noexcept |
| Gets the current execution context, if any is active. | |
| static ExecutionContext & | get () noexcept |
| Gets the current execution context, if any is active. | |
A context for function object execution.
An execution context represents a place where function objects will be executed. An io_context is an example of an execution context.
Class ExecutionContext implements an extensible, type-safe, polymorphic set of services, indexed by service type.
Services exist to manage the resources that are shared across an execution context. For example, timers may be implemented in terms of a single timer queue, and this queue would be stored in a service.
Access to the services of an execution_context is via three function templates, use_service(), add_service() and has_service().
In a call to use_service<Service>(), the type argument chooses a service, making available all members of the named type. If Service is not present in an execution_context, an object of type Service is created and added to the execution_context. A C++ program can check if an execution_context implements a particular service with the function template has_service<Service>().
Service objects may be explicitly added to an execution_context using the function template add_service<Service>(). If the Service is already present, the service_already_exists exception is thrown. If the owner of the service is not the same object as the execution_context parameter, the invalid_service_owner exception is thrown.
Once a service reference is obtained from an execution_context object by calling use_service(), that reference remains usable as long as the owning execution_context object exists.
All service implementations have execution_context::service as a public base class. Custom services may be implemented by deriving from this class and then added to an execution_context using the facilities described above.
Class execution_context may be used only as a base class for concrete execution context types. The io_context is an example of such a derived type.
On destruction, a class that is derived from execution_context must perform execution_context::shutdown() followed by execution_context::destroy().
This destruction sequence permits programs to simplify their resource management by using shared_ptr<>. Where an object's lifetime is tied to the lifetime of a connection (or some other sequence of asynchronous operations), a shared_ptr to the object would be bound into the handlers for all asynchronous operations associated with it. This works as follows:
shared_ptr references to the objects are destroyed.shutdown() and destroy() to destroy all pending handlers, causing all shared_ptr references to all connection objects to be destroyed.
|
virtualdefaultnoexcept |
Virtual destructor for the execution context object.
|
inlinestaticnoexcept |
Gets the current execution context, if any is active.
|
noexcept |
Returns an executor that run execute scheduled commands on the context.
|
staticnoexcept |
Gets the current execution context, if any is active.
|
pure virtualnoexcept |
Returns an executor that run execute scheduled commands on the context.
Implemented in CeresEngine::AppleDispatchQueue, CeresEngine::AppleRunLoop, CeresEngine::EmscriptenRunLoop, CeresEngine::Win32RunLoop, CeresEngine::Win32ThreadPool, and CeresEngine::ThreadPool.
Obtain the service object corresponding to the given type.
This function is used to locate a service object that corresponds to the given service type. If there is no existing implementation of the service, then the ExecutionContext will create a new instance of the service.
Obtain the service object corresponding to the given type.
This function is used to locate a service object that corresponds to the given service type. If there is no existing implementation of the service, nullptr is returned.
Determine if the ExecutionContext contains the specified service type.
This function is used to determine whether the ExecutionContext contains a service object corresponding to the given service type.
ExecutionContext contains the service.
|
noexcept |
Checks if the execution context is currently active.
true if the execution context is active (top most or not); false otherwise.