Logo Pico-Framework A web-first embedded framework for C++
Loading...
Searching...
No Matches
AppContext Class Reference

#include <AppContext.h>

+ Collaboration diagram for AppContext:

Public Member Functions

template<typename T >
void registerService (T *service)
 
template<typename T >
TgetService () const
 
void initFrameworkServices ()
 

Static Public Member Functions

static AppContextgetInstance ()
 
template<typename T >
static Tget ()
 Get a service of type T from the application context.
 
template<typename T >
static bool has ()
 

Private Member Functions

 AppContext ()=default
 

Static Private Member Functions

template<typename T >
static constexpr std::uintptr_t getTypeKey ()
 

Private Attributes

std::unordered_map< std::uintptr_t, void * > services
 

Static Private Attributes

static SemaphoreHandle_t mutex = xSemaphoreCreateMutex()
 

Detailed Description

Constructor & Destructor Documentation

◆ AppContext()

Member Function Documentation

◆ get()

template<typename T >
static T * AppContext::get ( )
inlinestatic

This is a convenience method that allows you to get a service without needing to call getService<T>() explicitly.

Template Parameters
TThe type of the service to retrieve. Must be a complete type.
Returns
Pointer to the service instance if found, nullptr otherwise.
Note
If the service is not registered, this will return nullptr.
Examples
/home/runner/work/pico-framework-docs/pico-framework-docs/pico-framework/framework/include/framework/AppContext.h.

Definition at line 63 of file AppContext.h.

63 {
64 return getInstance().getService<T>();
65 }
static AppContext & getInstance()
T * getService() const
Definition AppContext.h:45
static constexpr std::uintptr_t getTypeKey()
Definition AppContext.h:91

References getInstance(), getService(), and getTypeKey().

+ Here is the call graph for this function:

◆ getInstance()

AppContext & AppContext::getInstance ( )
static
Examples
/home/runner/work/pico-framework-docs/pico-framework-docs/pico-framework/framework/include/framework/AppContext.h.

Definition at line 22 of file AppContext.cpp.

22 {
23 static AppContext instance;
24 return instance;
25}

Referenced by get(), getFormattedTimestamp(), FrameworkManager::onStart(), and traceLog().

+ Here is the caller graph for this function:

◆ getService()

template<typename T >
T * AppContext::getService ( ) const
inline
Examples
/home/runner/work/pico-framework-docs/pico-framework-docs/pico-framework/framework/include/framework/AppContext.h.

Definition at line 45 of file AppContext.h.

45 {
47 auto it = services.find(getTypeKey<T>());
49 if (it != services.end()) {
50 return reinterpret_cast<T*>(it->second);
51 }
52 return nullptr;
53 }
static SemaphoreHandle_t mutex
Definition AppContext.h:88
std::unordered_map< std::uintptr_t, void * > services
Definition AppContext.h:86

References getTypeKey(), mutex, and services.

Referenced by get(), getFormattedTimestamp(), and traceLog().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ getTypeKey()

template<typename T >
static constexpr std::uintptr_t AppContext::getTypeKey ( )
inlinestaticconstexprprivate
Examples
/home/runner/work/pico-framework-docs/pico-framework-docs/pico-framework/framework/include/framework/AppContext.h.

Definition at line 91 of file AppContext.h.

91 {
92 return reinterpret_cast<std::uintptr_t>(&getTypeKey<T>); // unique per T
93 }

References getTypeKey().

Referenced by TimeManager::applyFixedTimezoneOffset(), DashboardController::deleteFile(), MultipartParser::extractFilename(), MultipartParser::file_exists(), Logger::forEachLine(), get(), getService(), getTypeKey(), FrameworkModel::getValue(), GpioEventManager::gpio_event_handler(), has(), FileHandler::init(), initFrameworkServices(), HttpServer::initListener(), App::initRoutes(), FileHandler::listDirectory(), FrameworkModel::load(), Logger::log(), App::onEvent(), FrameworkManager::onEvent(), FrameworkManager::onStart(), App::onStart(), FrameworkManager::poll(), App::poll(), MultipartParser::processFileData(), registerService(), HtmlTemplateView::render(), HttpServer::run(), FrameworkModel::save(), FrameworkModel::saveAll(), TimerService::scheduleAt(), TimerService::scheduleDailyAt(), TimerService::scheduleDuration(), TimerService::scheduleEvery(), FileHandler::serveFile(), TimeManager::setTime(), TimeManager::setTimeFromEpoch(), FrameworkManager::setupTraceFromConfig(), FrameworkModel::setValue(), sntp_set_system_time(), TimeManager::start(), and TimeManager::syncTimeWithNtp().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ has()

template<typename T >
static bool AppContext::has ( )
inlinestatic
Examples
/home/runner/work/pico-framework-docs/pico-framework-docs/pico-framework/framework/include/framework/AppContext.h.

Definition at line 68 of file AppContext.h.

68 {
69 return get<T>() != nullptr;
70 }

References getTypeKey().

+ Here is the call graph for this function:

◆ initFrameworkServices()

void AppContext::initFrameworkServices ( )
Examples
/home/runner/work/pico-framework-docs/pico-framework-docs/pico-framework/framework/include/framework/AppContext.h.

Definition at line 27 of file AppContext.cpp.

27 {
28 printf("[AppContext] Initializing framework services...\n");
29 #if PICO_HTTP_ENABLE_LITTLEFS
30 TRACE("[AppContext] Initializing LittleFS storage manager.\n");
33 TRACE("[AppContext] LittleFS storage manager registered.\n");
34 static JsonService jsonService(&littlefs);
35 registerService<JsonService>(&jsonService);
36 TRACE("[AppContext] Registered JsonService.\n");
37
38 #else
41 static JsonService jsonService(&fatfs);
42 registerService<JsonService>(&jsonService);
43 TRACE("[AppContext] Registered FatFsStorageManager.\n");
44 #endif
45 // Time manager (always present)
46 static TimeManager timeMgr;
48 TRACE("[AppContext] Registered TimeManager.\n");
49 // Event manager (always present)
52 TRACE("[AppContext] Registered EventManager.\n");
53 // Timer service (always present)
56 TRACE("[AppContext] Registered TimerService.\n");
57 static Logger logger;
59 TRACE("[AppContext] Registered Logger.\n");
60 #if defined(ENABLE_GPIO_EVENTS)
63 TRACE("[AppContext] Registered GpioEventManager.\n");
64 #endif
65
66 #if PICO_HTTP_ENABLE_JWT
67 static JwtAuthenticator jwt;
69 TRACE("[AppContext] Registered JwtAuthenticator.\n");
70 #endif
71 }
#define TRACE(...)
Default trace (INFO level).
Definition DebugTrace.h:187
Manages the system-wide event queue and subscriptions.
Concrete implementation of StorageManager using FatFs.
GpioEventManager registers interrupts and posts GpioChange events to multiple listeners per pin.
static GpioEventManager & getInstance()
Get the singleton instance of GpioEventManager.
Manages loading and saving of a single JSON document using StorageManager.
Definition JsonService.h:28
JWT JwtAuthenticator for embedded applications.
A LittleFS-based implementation of StorageManager, storing files in flash memory.
Basic timestamped logger with optional SD file logging.
Definition Logger.h:50
Central service for time-driven scheduling of framework events.

References GpioEventManager::getInstance(), getTypeKey(), and TRACE.

Referenced by FrameworkManager::onStart().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ registerService()

template<typename T >
void AppContext::registerService ( T service)
inline
Examples
/home/runner/work/pico-framework-docs/pico-framework-docs/pico-framework/framework/include/framework/AppContext.h.

Definition at line 27 of file AppContext.h.

27 {
29 services[getTypeKey<T>()] = reinterpret_cast<void*>(service);
31 }

References getTypeKey(), mutex, and services.

+ Here is the call graph for this function:

Member Data Documentation

◆ mutex

◆ services

std::unordered_map<std::uintptr_t, void*> AppContext::services
private

The documentation for this class was generated from the following files: