Doxygen with github
Loading...
Searching...
No Matches
ksApplication.h
1/*
2 * Copyright (c) 2021-2023, Krzysztof Strehlau
3 *
4 * This file is a part of the ksIotFramework library.
5 * All licensing information can be found inside LICENSE.md file.
6 *
7 * https://github.com/cziter15/ksIotFrameworkLib/blob/master/LICENSE
8 */
9
10#pragma once
11
12#include <memory>
13#include <list>
14#include <string>
15#include <functional>
16#include "ksComponent.h"
17
18#if APP_LOG_ENABLED
19typedef std::function<void(std::string&)> AppLogProviderFunc_t;
20typedef std::function<void(std::string&&)> AppLogCallbackFunc_t;
21#endif
22
23namespace ksf
24{
25 class ksComponent;
26
43 {
44 protected:
45 std::list<std::shared_ptr<ksComponent>> components;
46
47#if APP_LOG_ENABLED
48 AppLogCallbackFunc_t appLogCallback;
49#endif
50 public:
62 template <class TComponentType, class... TParams>
63 std::weak_ptr<TComponentType> addComponent(TParams... arg)
64 {
65 static_assert(!std::is_same_v<decltype(&ksComponent::getInstanceType), decltype(&TComponentType::getInstanceType)>,
66 "You're calling addComponent, but provided type lacks RTTI implementation. Did you miss KSF_RTTI_DECLARATIONS?"
67 );
68
69 auto componentSp{std::make_shared<TComponentType>(arg...)};
70 auto componentWp{std::weak_ptr<TComponentType>(componentSp)};
71 components.push_front(std::move(componentSp));
72 return componentWp;
73 }
74
80 template <class TComponentType>
81 void findComponents(std::vector<std::weak_ptr<TComponentType>>& outComponents)
82 {
83 static_assert(!std::is_same_v<decltype(&ksComponent::getInstanceType), decltype(&TComponentType::getInstanceType)>,
84 "You're calling findComponents, but provided type lacks RTTI implementation. Did you miss KSF_RTTI_DECLARATIONS?"
85 );
86
87 outComponents.clear();
88
89 for (const auto& comp : components)
90 {
91 if (comp->isA(TComponentType::getClassType()))
92 {
93 std::weak_ptr<TComponentType> castedCompWp{std::static_pointer_cast<TComponentType>(comp)};
94 if (!castedCompWp.expired())
95 outComponents.push_back(std::move(castedCompWp));
96 }
97 }
98 }
99
105 template <class TComponentType>
106 std::weak_ptr<TComponentType> findComponent()
107 {
108 static_assert(!std::is_same_v<decltype(&ksComponent::getInstanceType), decltype(&TComponentType::getInstanceType)>,
109 "You're calling findComponent, but provided type lacks RTTI implementation. Did you miss KSF_RTTI_DECLARATIONS?"
110 );
111
112 for (const auto& comp : components)
113 if (comp->isA(TComponentType::getClassType()))
114 return std::static_pointer_cast<TComponentType>(comp);
115
116 return std::weak_ptr<TComponentType>();
117 }
118
123 virtual bool init() = 0;
124
129 virtual bool loop();
130
131#if APP_LOG_ENABLED
136 void log(AppLogProviderFunc_t provideLogFn) const;
137
142 void setLogCallback(AppLogCallbackFunc_t logCallback);
143#endif
144 };
145}
A class that is a base for user-defined application.
Definition ksApplication.h:43
std::list< std::shared_ptr< ksComponent > > components
An array with shared_ptr of components (holding main reference).
Definition ksApplication.h:45
virtual bool loop()
Executes application logic loop.
Definition ksApplication.cpp:15
std::weak_ptr< TComponentType > findComponent()
Iterates through all components and returns a weak pointer to the first component that matches the ty...
Definition ksApplication.h:106
void findComponents(std::vector< std::weak_ptr< TComponentType > > &outComponents)
Iterates through all components and returns a vector of weak pointers with components that matches th...
Definition ksApplication.h:81
std::weak_ptr< TComponentType > addComponent(TParams... arg)
Instantiates a component of the type defined by the template instance. This function will pass all te...
Definition ksApplication.h:63
virtual bool init()=0
Initializes application.
virtual const size_t getInstanceType() const
Retrieves type ID of the object.
Definition ksComponent.h:52