Logo Pico-Framework A web-first embedded framework for C++
Loading...
Searching...
No Matches
AppContext.h
Go to the documentation of this file.
1#pragma once
2#include <unordered_map>
3#include <cstdint>
4#include <type_traits>
5#include <string_view>
6#include <FreeRTOS.h>
7#include <semphr.h>
8
9class AppContext {
10public:
11 static AppContext& getInstance();
12
26 template<typename T>
29 services[getTypeKey<T>()] = reinterpret_cast<void*>(service);
31 }
32
44 template<typename T>
45 T* getService() const {
47 auto it = services.find(getTypeKey<T>());
49 if (it != services.end()) {
50 return reinterpret_cast<T*>(it->second);
51 }
52 return nullptr;
53 }
54
62 template<typename T>
63 static T* get() {
64 return getInstance().getService<T>();
65 }
66
67 template<typename T>
68 static bool has() {
69 return get<T>() != nullptr;
70 }
71
83
84private:
85 AppContext() = default;
86 std::unordered_map<std::uintptr_t, void*> services;
87
89
90 template<typename T>
91 static constexpr std::uintptr_t getTypeKey() {
92 return reinterpret_cast<std::uintptr_t>(&getTypeKey<T>); // unique per T
93 }
94};
static SemaphoreHandle_t mutex
Definition AppContext.h:88
std::unordered_map< std::uintptr_t, void * > services
Definition AppContext.h:86
static T * get()
Get a service of type T from the application context.
Definition AppContext.h:63
static AppContext & getInstance()
static bool has()
Definition AppContext.h:68
AppContext()=default
T * getService() const
Definition AppContext.h:45
void initFrameworkServices()
void registerService(T *service)
Definition AppContext.h:27
static constexpr std::uintptr_t getTypeKey()
Definition AppContext.h:91