libzypp  17.36.3
application.cc
Go to the documentation of this file.
1 /*---------------------------------------------------------------------\
2 | ____ _ __ __ ___ |
3 | |__ / \ / / . \ . \ |
4 | / / \ V /| _/ _/ |
5 | / /__ | | | | | | |
6 | /_____||_| |_| |_| |
7 | |
8 ----------------------------------------------------------------------*/
9 #include "application.h"
12 
13 namespace ztui {
14 
15  namespace {
16  Application **getApplicationInstance () {
17  thread_local Application *app = nullptr;
18  return &app;
19  }
20  }
21 
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 
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 }
#define ZYPP_THROW(EXCPT)
Drops a logline and throws the Exception.
Definition: Exception.h:424
Base class for producing common (for now) zypper output.
Definition: Out.h:423
Application(const sat::Solvable &solvable_r)
Ctor.
Definition: Application.cc:23
Definition: Arch.h:363
~Application() override
Dtor.
Definition: Application.cc:27
#define WAR
Definition: Logger.h:101
Base class for Exception.
Definition: Exception.h:146
Class representing an application (appdata.xml)
Definition: Application.h:27
static constexpr int ZTUI_EXIT_OK
Definition: application.h:24