Application.h
1 /*
2  * Copyright (C) 2006-2008 Marc Boris Duerner
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * As a special exception, you may use this file as part of a free
10  * software library without restriction. Specifically, if other files
11  * instantiate templates or use macros or inline functions from this
12  * file, or you compile this file and link it with other files to
13  * produce an executable, this file does not by itself cause the
14  * resulting executable to be covered by the GNU General Public
15  * License. This exception does not however invalidate any other
16  * reasons why the executable file might be covered by the GNU Library
17  * General Public License.
18  *
19  * This library is distributed in the hope that it will be useful,
20  * but WITHOUT ANY WARRANTY; without even the implied warranty of
21  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22  * Lesser General Public License for more details.
23  *
24  * You should have received a copy of the GNU Lesser General Public
25  * License along with this library; if not, write to the Free Software
26  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
27  */
28 
29 #ifndef PT_SYSTEM_APPLICATION_H
30 #define PT_SYSTEM_APPLICATION_H
31 
32 #include <Pt/System/Api.h>
33 #include <Pt/System/MainLoop.h>
34 #include <Pt/Arg.h>
35 #include <Pt/Signal.h>
36 #include <Pt/Connectable.h>
37 
38 namespace Pt {
39 
40 namespace System {
41 
42 class ApplicationImpl;
43 
56 class PT_SYSTEM_API Application : public Pt::Connectable
57 {
58  public:
61  explicit Application(int argc = 0, char** argv = 0);
62 
65  explicit Application(EventLoop* loop, int argc = 0, char** argv = 0);
66 
69  ~Application();
70 
73  static Application& instance();
74 
78  { return *_loop; }
79 
82  void run()
83  { _loop->run(); }
84 
87  void exit()
88  { _loop->exit(); }
89 
92  bool ignoreSystemSignal(int sig);
93 
96  bool catchSystemSignal(int sig);
97 
100  bool raiseSystemSignal(int sig);
101 
105  { return _systemSignal; }
106 
109  int argc() const
110  { return _argc; }
111 
114  char** argv() const
115  { return _argv; }
116 
119  template <typename T>
120  Arg<T> getArg(const char* name)
121  {
122  return Arg<T>(_argc, _argv, name);
123  }
124 
127  template <typename T>
128  Arg<T> getArg(const char* name, const T& def)
129  {
130  return Arg<T>(_argc, _argv, name, def);
131  }
132 
135  template <typename T>
136  Arg<T> getArg(const char name)
137  {
138  return Arg<T>(_argc, _argv, name);
139  }
140 
143  template <typename T>
144  Arg<T> getArg(const char name, const T& def)
145  {
146  return Arg<T>(_argc, _argv, name, def);
147  }
148 
149  public:
151  static void chdir(const Path& path);
152 
154  static Path cwd();
155 
160  static Path rootdir();
161 
168  static Path tmpdir();
169 
174  static void setEnvVar(const std::string& name, const std::string& value);
175 
180  static void unsetEnvVar(const std::string& name);
181 
186  static std::string getEnvVar(const std::string& name);
187 
188  static unsigned long usedMemory();
189 
191  ApplicationImpl& impl()
192  { return *_impl; }
193 
194  protected:
196  void init(EventLoop& loop);
197 
198  private:
199  int _argc;
200  char** _argv;
201  ApplicationImpl* _impl;
202  EventLoop* _loop;
203  MainLoop* _owner;
204  Signal<int> _systemSignal;
205 };
206 
207 } // namespace System
208 
209 } // namespace Pt
210 
211 #endif // PT_SYSTEM_APPLICATION_H
Console applications without a GUI.
Definition: Application.h:56
Connection Management for Signal and Slot Objects.
Definition: Connectable.h:49
Represents a path in the file-system.
Definition: Path.h:47
void run()
Starts the contained event loop.
Definition: Application.h:82
char ** argv() const
Command line arguments.
Definition: Application.h:114
Read and extract command-line options.
Definition: Arg.h:199
Signal< int > & systemSignal()
Notifies when a system signal was caught.
Definition: Application.h:104
EventLoop & loop()
Returns the event loop.
Definition: Application.h:77
Thread-safe event loop supporting I/O multiplexing and Timers.
Definition: EventLoop.h:72
int argc() const
Number of command line arguments.
Definition: Application.h:109
Arg< T > getArg(const char *name)
Returns the value of a long option.
Definition: Application.h:120
Arg< T > getArg(const char name)
Returns the value of a short option.
Definition: Application.h:136
void exit()
Exits from the contained event loop.
Definition: Application.h:87
Arg< T > getArg(const char name, const T &def)
Returns the value of a short option.
Definition: Application.h:144
Arg< T > getArg(const char *name, const T &def)
Returns the value of a long option.
Definition: Application.h:128