Unravel Engine C++ Reference
Loading...
Searching...
No Matches
service.cpp
Go to the documentation of this file.
1#include "service.h"
2#include "entt/core/fwd.hpp"
3#include "entt/meta/meta.hpp"
4#include <chrono>
5#include <csignal>
6#include <iostream>
7
8#include <entt/meta/resolve.hpp>
9#include <entt/meta/utility.hpp>
10#include <entt/core/hashed_string.hpp>
11
12using namespace std::chrono_literals;
13
14service::service(int argc, char* argv[]) : parser_(argc, argv)
15{
16}
17
18auto service::load(const module_desc& desc) -> bool
19{
20 std::cout << "service::" << __func__ << " module " << desc.lib_name << std::endl;
21 module_data module;
22 module.desc = desc;
23
24 using namespace entt::literals;
25
26 auto type = entt::resolve(entt::hashed_string{module.desc.type_name.c_str()});
27
28 if(!type.invoke("create"_hs, {}, entt::forward_as_meta(ctx_), entt::forward_as_meta(parser_)).cast<bool>())
29 {
30 return false;
31 }
32
33 modules_.emplace_back(std::move(module));
34
35 return true;
36}
37
38auto service::unload(const module_data& module) -> bool
39{
40 std::cout << "service::" << __func__ << " module " << module.desc.lib_name << std::endl;
41
42 using namespace entt::literals;
43
44 auto type = entt::resolve(entt::hashed_string{module.desc.type_name.c_str()});
45
46 if(!type.invoke("deinit"_hs, {}).cast<bool>())
47 {
48 return false;
49 }
50
51 if(!type.invoke("destroy"_hs, {}).cast<bool>())
52 {
53 return false;
54 }
55
56 return true;
57}
58
59auto service::load(const std::vector<module_desc>& descs) -> bool
60{
61 bool batch = true;
62 for(const auto& desc : descs)
63 {
64 batch &= load(desc);
65 }
66
67 if(batch)
68 {
69 batch &= init();
70 }
71
72 if(!batch)
73 {
74 unload();
75 }
76
77 return batch;
78}
79
80auto service::unload() -> bool
81{
82 bool batch = true;
83 for(auto it = std::rbegin(modules_); it != std::rend(modules_); ++it)
84 {
85 auto& module = *it;
86 batch &= unload(module);
87 }
88
89 modules_.clear();
90 return batch;
91}
92
93auto service::init() -> bool
94{
95 if(!parser_.run())
96 {
97 return false;
98 }
99
100 for(const auto& module : modules_)
101 {
102 using namespace entt::literals;
103
104 auto type = entt::resolve(entt::hashed_string{module.desc.type_name.c_str()});
105
106 if(!type.invoke("init"_hs, {}, entt::forward_as_meta(parser_)).cast<bool>())
107 {
108 return false;
109 }
110 }
111
112 parser_.reset();
113
114 return true;
115}
116
117auto service::interrupt() -> bool
118{
119 // std::cout << "service::" << __func__ << std::endl;
120 bool processed = false;
121 for(const auto& module : modules_)
122 {
123 using namespace entt::literals;
124
125 auto type = entt::resolve(entt::hashed_string{module.desc.type_name.c_str()});
126
127 if(!type.invoke("interrupt"_hs, {}).cast<bool>())
128 {
129 return false;
130 }
131
132 processed = true;
133 }
134
135 return processed;
136}
137
138auto service::process() -> int
139{
140 // std::cout << "service::" << __func__ << std::endl;
141 int processed = SERVICE_RESULT_EXIT;
142 for(const auto& module : modules_)
143 {
144 using namespace entt::literals;
145
146 auto type = entt::resolve(entt::hashed_string{module.desc.type_name.c_str()});
147
148 auto proc_result = type.invoke("process"_hs, {}).cast<int>();
149
150 if(proc_result == SERVICE_RESULT_EXIT)
151 {
152 return SERVICE_RESULT_EXIT;
153 }
154
155 processed = std::max(processed, proc_result);
156 }
157
158 return processed;
159}
160
162{
163 return parser_;
164}
165
166auto service_main(const char* name, int argc, char* argv[]) -> int
167{
168 std::vector<module_desc> modules{{name, name}};
169
170 int run = SERVICE_RESULT_RUN;
171 while(run != SERVICE_RESULT_EXIT)
172 {
173 service app(argc, argv);
174
175 if(!app.load(modules))
176 {
177 return -1;
178 }
179
180 while(run == SERVICE_RESULT_RUN)
181 {
182 run = app.process();
183 }
184
185 if(!app.unload())
186 {
187 return -1;
188 }
189 }
190
191 return 0;
192}
void unload(void *_ptr)
void * load(bx::FileReaderI *_reader, bx::AllocatorI *_allocator, const char *_filePath, uint32_t *_size)
manifold_type type
std::string name
Definition hub.cpp:27
auto service_main(const char *name, int argc, char *argv[]) -> int
Definition service.cpp:166
#define SERVICE_RESULT_EXIT
Definition service.h:18
#define SERVICE_RESULT_RUN
Definition service.h:19
module_desc desc
Definition service.h:15
std::string lib_name
Definition service.h:9
auto interrupt() -> bool
Definition service.cpp:117
auto get_cmd_line_parser() -> cmd_line::parser &
Definition service.cpp:161
service(int argc, char *argv[])
Definition service.cpp:14
auto process() -> int
Definition service.cpp:138
auto unload() -> bool
Definition service.cpp:80
auto load(const module_desc &desc) -> bool
Definition service.cpp:18
auto unload(const module_data &module) -> bool
Definition service.cpp:38
auto init() -> bool
Definition service.cpp:93