-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathApplication.cpp
More file actions
65 lines (51 loc) · 1.93 KB
/
Application.cpp
File metadata and controls
65 lines (51 loc) · 1.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
// Copyright Ciriaco Garcia de Celis 2016-2017.
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
#include <MyLib/Printer.h>
#include "Application.h"
int main(int argc, char** argv)
{
return Application::run(argc, argv);
}
void Application::onStart()
{
library->basicSubscriptions(weak_from_this()); // all except ReplyA and ReplyB
library->connect(getChannel<std::shared_ptr<ReplyA>>());
library->connect<std::shared_ptr<ReplyB>>(weak_from_this()); // (alternative syntax)
library->send(WantPrinter{});
}
template <> void Application::onMessage(Printer::ptr& msg)
{
printer = msg;
}
template <> void Application::onMessage(std::unique_ptr<Info>& msg)
{
printer->send(LINE("<MyApp> received " << msg->data));
// Programmers not very seasoned managing the objects lifecycle may be concerned
// about the risk of 'library' being potentially deleted at the moment they
// need to invoke its send() method. In the following example, a safeLibrary()
// functor is used instead, which wouldn't crash even in such situation:
if (msg->data.find("fast") != std::string::npos)
safeLibrary(std::make_shared<RequestA>("RequestA")); // equivalent to library->send()
else
safeLibrary(std::make_shared<RequestB>("RequestB"));
}
template <> void Application::onMessage(std::shared_ptr<ReplyA>& msg)
{
printer->send(LINE("<MyApp> received " << msg->data));
}
template <> void Application::onMessage(std::shared_ptr<ReplyB>& msg)
{
printer->send(LINE("<MyApp> received " << msg->data));
}
template <> void Application::onMessage(std::shared_ptr<Billing>& msg)
{
printer->send(LINE("<MyApp> owes " << msg->count << " bills"));
}
template <> void Application::onMessage(LibraryIsTired&)
{
printer->send(LINE("<MyApp> shutting down"));
printer->waitIdle();
stop();
}