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 
54 class PT_SYSTEM_API Application : public Pt::Connectable
55 {
56  public:
60 
63  explicit Application(int& argc, char** argv = 0);
64 
67  explicit Application(EventLoop* loop);
68 
71  Application(EventLoop* loop, int& argc, char** argv = 0);
72 
76 
79  static Application& instance();
80 
84  { return *_loop; }
85 
88  void run()
89  { _loop->run(); }
90 
93  void exit()
94  { _loop->exit(); }
95 
98  bool ignoreSystemSignal(int sig);
99 
102  bool catchSystemSignal(int sig);
103 
106  bool raiseSystemSignal(int sig);
107 
111  { return _systemSignal; }
112 
115  int& argc() const
116  { return *_argc; }
117 
120  char** argv() const
121  { return _argv; }
122 
125  void setArgs(int& argc, char** argv);
126 
129  template <typename T>
130  Arg<T> getArg(const char* name)
131  {
132  return Arg<T>(*_argc, _argv, name);
133  }
134 
137  template <typename T>
138  Arg<T> getArg(const char* name, const T& def)
139  {
140  return Arg<T>(*_argc, _argv, name, def);
141  }
142 
145  template <typename T>
146  Arg<T> getArg(const char name)
147  {
148  return Arg<T>(*_argc, _argv, name);
149  }
150 
153  template <typename T>
154  Arg<T> getArg(const char name, const T& def)
155  {
156  return Arg<T>(*_argc, _argv, name, def);
157  }
158 
159  public:
161  static void chdir(const Path& path);
162 
164  static Path cwd();
165 
170  static Path rootdir();
171 
178  static Path tmpdir();
179 
184  static void setEnvVar(const std::string& name, const std::string& value);
185 
190  static void unsetEnvVar(const std::string& name);
191 
196  static std::string getEnvVar(const std::string& name);
197 
198  static unsigned long usedMemory();
199 
201  ApplicationImpl& impl()
202  { return *_impl; }
203 
204  protected:
206  void init(EventLoop& loop);
207 
208  private:
209  int _noArgs;
210  int* _argc;
211  char** _argv;
212  ApplicationImpl* _impl;
213  EventLoop* _loop;
214  MainLoop* _owner;
215  Signal<int> _systemSignal;
216 };
217 
218 } // namespace System
219 
220 } // namespace Pt
221 
222 #endif // PT_SYSTEM_APPLICATION_H
Core module.
Definition: pt-gfx-images.dox:14
Application(EventLoop *loop, int &argc, char **argv=0)
Construct with custom event loop.
static void unsetEnvVar(const std::string &name)
Unset environment variable.
static void setEnvVar(const std::string &name, const std::string &value)
Set environment variable.
Arg< T > getArg(const char *name)
Returns the value of a long option.
Definition: Application.h:130
Arg< T > getArg(const char *name, const T &def)
Returns the value of a long option.
Definition: Application.h:138
void exit()
Exits from the contained event loop.
Definition: Application.h:93
EventLoop & loop()
Returns the event loop.
Definition: Application.h:83
Read and extract command-line options.
Definition: Arg.h:200
Arg< T > getArg(const char name, const T &def)
Returns the value of a short option.
Definition: Application.h:154
bool raiseSystemSignal(int sig)
Raise a system signal.
Arg< T > getArg(const char name)
Returns the value of a short option.
Definition: Application.h:146
char ** argv() const
Command line arguments.
Definition: Application.h:120
int & argc() const
Number of command line arguments.
Definition: Application.h:115
static Path rootdir()
Returns the system root path.
Connection Management for Signal and Slot Objects.
Definition: Connectable.h:50
Application()
default Constructor.
static Application & instance()
Returns an instance to the application.
bool ignoreSystemSignal(int sig)
Ignores a system signal.
Application(int &argc, char **argv=0)
Construct with command line arguments.
static std::string getEnvVar(const std::string &name)
Get environment variable.
void run()
Starts the contained event loop.
Definition: Application.h:88
Represents a path in the file-system.
Definition: Path.h:48
Thread-safe event loop supporting I/O multiplexing and Timers.
Definition: EventLoop.h:74
static Path tmpdir()
Returns the systems tmp directory.
Signal< int > & systemSignal()
Notifies when a system signal was caught.
Definition: Application.h:110
static void chdir(const Path &path)
Changes the current directory.
void setArgs(int &argc, char **argv)
Set command line arguments.
~Application()
Destructor.
Console applications without a GUI.
Definition: Application.h:55
Thread-safe event loop supporting I/O multiplexing and Timers.
Definition: MainLoop.h:67
Application(EventLoop *loop)
Default construct with custom event loop.
static Path cwd()
Returns the current directory.
bool catchSystemSignal(int sig)
Catch a system signal.