Doxygen with github
Loading...
Searching...
No Matches
ksApplication.h
1/*
2 * Copyright (c) 2020-2026, Krzysztof Strehlau
3 *
4 * This file is a part of the ksIotFrameworkLib IoT 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 <vector>
17#include <type_traits>
18
19#include "ksComponent.h"
20
21#if APP_LOG_ENABLED
22typedef std::function<void(std::string&)> AppLogProviderFunc_t;
23typedef std::function<void(std::string&&)> AppLogCallbackFunc_t;
24#endif
25
26namespace ksf
27{
28 class ksComponent;
29
46 {
47 protected:
48 std::list<std::shared_ptr<ksComponent>> components;
49
50#if APP_LOG_ENABLED
51 AppLogCallbackFunc_t appLogCallback;
52#endif
53 public:
57 virtual ~ksApplication();
58
70 template <class TComponentType, class... TParams>
71 std::weak_ptr<TComponentType> addComponent(TParams... arg)
72 {
73 static_assert(!std::is_same_v<decltype(&ksComponent::getInstanceType), decltype(&TComponentType::getInstanceType)>,
74 "You're calling addComponent, but provided type lacks RTTI implementation. Did you miss KSF_RTTI_DECLARATIONS?"
75 );
76
77 auto componentSp{std::make_shared<TComponentType>(arg...)};
78 auto componentWp{std::weak_ptr<TComponentType>(componentSp)};
79 components.push_front(std::move(componentSp));
80 return componentWp;
81 }
82
88 template <class TComponentType>
89 void findComponents(std::vector<std::weak_ptr<TComponentType>>& outComponents)
90 {
91 static_assert(!std::is_same_v<decltype(&ksComponent::getInstanceType), decltype(&TComponentType::getInstanceType)>,
92 "You're calling findComponents, but provided type lacks RTTI implementation. Did you miss KSF_RTTI_DECLARATIONS?"
93 );
94
95 outComponents.clear();
96
97 for (const auto& comp : components)
98 {
99 if (comp->isA(TComponentType::getClassType()))
100 {
101 auto castedCompSp{std::static_pointer_cast<TComponentType>(comp)};
102 outComponents.emplace_back(castedCompSp);
103 }
104 }
105 }
106
112 template <class TComponentType>
113 std::weak_ptr<TComponentType> findComponent()
114 {
115 static_assert(!std::is_same_v<decltype(&ksComponent::getInstanceType), decltype(&TComponentType::getInstanceType)>,
116 "You're calling findComponent, but provided type lacks RTTI implementation. Did you miss KSF_RTTI_DECLARATIONS?"
117 );
118
119 for (const auto& comp : components)
120 {
121 if (comp->isA(TComponentType::getClassType()))
122 {
123 auto castedCompSp{std::static_pointer_cast<TComponentType>(comp)};
124 return std::weak_ptr<TComponentType>(castedCompSp);
125 }
126 }
127
128 return std::weak_ptr<TComponentType>();
129 }
130
135 virtual bool init() = 0;
136
141 virtual bool loop();
142
143#if APP_LOG_ENABLED
148 void log(AppLogProviderFunc_t provideLogFn) const;
149
154 void setLogCallback(AppLogCallbackFunc_t logCallback);
155#endif
156 };
157}
A class that serves as the base for user-defined applications.
Definition ksApplication.h:46
std::list< std::shared_ptr< ksComponent > > components
An array with shared_ptr of components (holding main reference).
Definition ksApplication.h:48
virtual bool loop()
Executes application logic loop.
Definition ksApplication.cpp:19
virtual ~ksApplication()
Destructor.
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:113
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:89
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:71
virtual bool init()=0
Initializes application.
virtual std::size_t getInstanceType() const
Retrieves type ID of the object.
Definition ksComponent.h:37