libzypp: zypp-tui/application.cc Source File

libzypp 17.38.14

application.cc
Go to the documentation of this file.
1/*---------------------------------------------------------------------\
2| ____ _ __ __ ___ |
3| |__ / \ / / . \ . \ |
4| / / \ V /| _/ _/ |
5| / /__ | | | | | | |
6| /_____||_| |_| |_| |
7| |
8----------------------------------------------------------------------*/
9#include "application.h"
10#include <zypp-tui/output/OutNormal.h>
11#include <zypp-core/base/Exception.h>
12
13 namespace ztui {
14
15 namespace {
16 Application **getApplicationInstance () {
17 thread_local Application *app = nullptr;
18 return &app;
19 }
20 }
21
22 Application::Application()
23 : _exitCode(ZTUI_EXIT_OK)
24 , _config( new Config() )
25 { init(); }
26
27 Application::Application(std::shared_ptr<Config> &&cfg)
28 : _exitCode(ZTUI_EXIT_OK)
29 , _config( std::move(cfg) )
30 { init(); }
31
32 void Application::init()
33 {
34 // ALWAYS do this FIRST!
35 *getApplicationInstance() = this;
36 _out = std::make_shared<OutNormal>();
37 }
38
39 Application::~Application()
40 {
41 // set the thread local global to null again
42 *getApplicationInstance() = nullptr;
43 }
44
45 Application &Application::instance()
46 {
47 auto instPtr = *getApplicationInstance ();
48 if ( !instPtr )
49 ZYPP_THROW( zypp::Exception("No ztui::Application intance registered, its required to create one manually before using ztui.") );
50 return *instPtr;
51 }
52
53 const Config &Application::config() const
54 {
55 return *_config;
56 }
57
58 Config &Application::mutableConfig()
59 {
60 return *_config;
61 }
62
63 Out &Application::out()
64 {
65 if ( not _out ) {
66 _out.reset(new OutNormal( Out::QUIET ));
67 }
68 return *_out;
69 }
70
71 void Application::setOutputWriter(Out *out)
72 {
73 if ( out == _out.get() )
74 return;
75 _out.reset ( out );
76 }
77
78 void Application::setExitCode(int exit) {
79 WAR << "setExitCode " << exit << std::endl;
80 _exitCode = exit;
81 }
82
83}
Exception.h
ZYPP_THROW
#define ZYPP_THROW(EXCPT)
Drops a logline and throws the Exception.
Definition Exception.h:459
WAR
#define WAR
Definition Logger.h:104
OutNormal.h
application.h
ztui::Application::setOutputWriter
virtual void setOutputWriter(Out *out)
Definition application.cc:71
ztui::Application::_out
std::shared_ptr< Out > _out
Definition application.h:54
ztui::Application::_config
std::shared_ptr< Config > _config
Definition application.h:53
ztui::Application::Application
Application()
Definition application.cc:22
ztui::Application::instance
static Application & instance()
Definition application.cc:45
ztui::Application::out
virtual Out & out()
Definition application.cc:63
ztui::Application::setExitCode
void setExitCode(int exit)
Definition application.cc:78
ztui::Application::config
const Config & config() const
Definition application.cc:53
ztui::Application::mutableConfig
Config & mutableConfig()
Definition application.cc:58
ztui::Application::_exitCode
int _exitCode
Definition application.h:52
ztui::Application::init
void init()
Definition application.cc:32
ztui::Config
Definition config.h:21
ztui::OutNormal
Definition OutNormal.h:24
ztui::Out
Base class for producing common (for now) zypper output.
Definition Out.h:424
ztui::Out::QUIET
@ QUIET
Only important messages (no progress or status, only the result).
Definition Out.h:429
zypp::Application
Class representing an application (appdata.xml).
Definition Application.h:28
zypp::Application::~Application
~Application() override
Dtor.
Definition Application.cc:27
zypp::Exception
Base class for Exception.
Definition Exception.h:153
std
Definition ansi.h:855
ztui
Definition application.cc:13