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
17#include "ksComponent.h"
18
19#if APP_LOG_ENABLED
20typedef std::function<void(std::string&)> AppLogProviderFunc_t;
21typedef std::function<void(std::string&&)> AppLogCallbackFunc_t;
22#endif
23
24namespace ksf
25{
26 class ksComponent;
27
44 {
45 protected:
46 std::list<std::shared_ptr<ksComponent>> components;
47
48#if APP_LOG_ENABLED
49 AppLogCallbackFunc_t appLogCallback;
50#endif
51 public:
55 virtual ~ksApplication();
56
68 template <class TComponentType, class... TParams>
69 std::weak_ptr<TComponentType> addComponent(TParams... arg)
70 {
71 static_assert(!std::is_same_v<decltype(&ksComponent::getInstanceType), decltype(&TComponentType::getInstanceType)>,
72 "You're calling addComponent, but provided type lacks RTTI implementation. Did you miss KSF_RTTI_DECLARATIONS?"
73 );
74
75 auto componentSp{std::make_shared<TComponentType>(arg...)};
76 auto componentWp{std::weak_ptr<TComponentType>(componentSp)};
77 components.push_front(std::move(componentSp));
78 return componentWp;
79 }
80
86 template <class TComponentType>
87 void findComponents(std::vector<std::weak_ptr<TComponentType>>& outComponents)
88 {
89 static_assert(!std::is_same_v<decltype(&ksComponent::getInstanceType), decltype(&TComponentType::getInstanceType)>,
90 "You're calling findComponents, but provided type lacks RTTI implementation. Did you miss KSF_RTTI_DECLARATIONS?"
91 );
92
93 outComponents.clear();
94
95 for (const auto& comp : components)
96 {
97 if (comp->isA(TComponentType::getClassType()))
98 {
99 std::weak_ptr<TComponentType> castedCompWp{std::static_pointer_cast<TComponentType>(comp)};
100 if (!castedCompWp.expired())
101 outComponents.push_back(std::move(castedCompWp));
102 }
103 }
104 }
105
111 template <class TComponentType>
112 std::weak_ptr<TComponentType> findComponent()
113 {
114 static_assert(!std::is_same_v<decltype(&ksComponent::getInstanceType), decltype(&TComponentType::getInstanceType)>,
115 "You're calling findComponent, but provided type lacks RTTI implementation. Did you miss KSF_RTTI_DECLARATIONS?"
116 );
117
118 for (const auto& comp : components)
119 if (comp->isA(TComponentType::getClassType()))
120 return std::static_pointer_cast<TComponentType>(comp);
121
122 return std::weak_ptr<TComponentType>();
123 }
124
129 virtual bool init() = 0;
130
135 virtual bool loop();
136
137#if APP_LOG_ENABLED
142 void log(AppLogProviderFunc_t provideLogFn) const;
143
148 void setLogCallback(AppLogCallbackFunc_t logCallback);
149#endif
150 };
151}
A class that serves as the base for user-defined applications.
Definition ksApplication.h:44
std::list< std::shared_ptr< ksComponent > > components
An array with shared_ptr of components (holding main reference).
Definition ksApplication.h:46
virtual bool loop()
Executes application logic loop.
Definition ksApplication.cpp:18
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:112
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:87
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:69
virtual bool init()=0
Initializes application.
virtual const std::size_t getInstanceType() const
Retrieves type ID of the object.
Definition ksComponent.h:37