Cogs.Core
Services.cpp
1#include "Services.h"
2
3#include "Foundation/Logging/Logger.h"
4
5namespace
6{
8}
9
10Cogs::Core::Services::Services(Context * context) :
11 context(context)
12{
13
14}
15
16Cogs::Core::Services::~Services()
17{
18 clear();
19}
20
21void Cogs::Core::Services::clear()
22{
23 for (auto & s : services) {
24 if (s.destroy) {
25 s.destroy(s.service);
26 assert(s.service);
27 context->memory->baseAllocator->deallocate(s.service, s.size);
28 }
29 s.service = nullptr;
30 }
31 services.clear();
32}
33
34void Cogs::Core::Services::addFrameCallback(std::function<void(void)> callback)
35{
36 callbacks.push_back(callback);
37}
38
39void Cogs::Core::Services::dispatchFrameCallbacks()
40{
41 for (auto & c : callbacks) {
42 c();
43 }
44}
45
46bool Cogs::Core::Services::ensureUniqueService(Reflection::TypeId typeId)
47{
48 if (locateService(typeId)) {
49 auto & type = Reflection::TypeDatabase::getType(typeId);
50 LOG_WARNING(logger, "Service %s has already been registered. Returning previously created service.", type.getName().c_str());
51 return false;
52 }
53
54 return true;
55}
56
57void * Cogs::Core::Services::locateService(Reflection::TypeId typeId)
58{
59 for (auto & s : services) {
60 if (s.id == typeId) {
61 return s.service;
62 }
63 }
64
65 return nullptr;
66}
Log implementation class.
Definition: LogManager.h:139
static const Type & getType()
Get the Type of the given template argument.
Definition: TypeDatabase.h:168
constexpr Log getLogger(const char(&name)[LEN]) noexcept
Definition: LogManager.h:180
uint16_t TypeId
Built in type used to uniquely identify a single type instance.
Definition: Name.h:48