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
38namespace Pt {
39
40namespace System {
41
42class ApplicationImpl;
43
73class PT_SYSTEM_API Application : public Pt::Connectable
74{
75 public:
79
82 explicit Application(int& argc, char** argv = 0);
83
87
90 Application(EventLoop* loop, int& argc, char** argv = 0);
91
95
99
103 { return *_loop; }
104
107 void run()
108 { _loop->run(); }
109
112 void exit()
113 { _loop->exit(); }
114
117 bool ignoreSystemSignal(int sig);
118
121 bool catchSystemSignal(int sig);
122
125 bool raiseSystemSignal(int sig);
126
130 { return _systemSignal; }
131
134 int& argc() const
135 { return *_argc; }
136
139 char** argv() const
140 { return _argv; }
141
144 void setArgs(int& argc, char** argv);
145
148 template <typename T>
149 Arg<T> getArg(const char* name)
150 {
151 return Arg<T>(*_argc, _argv, name);
152 }
153
156 template <typename T>
157 Arg<T> getArg(const char* name, const T& def)
158 {
159 return Arg<T>(*_argc, _argv, name, def);
160 }
161
164 template <typename T>
165 Arg<T> getArg(const char name)
166 {
167 return Arg<T>(*_argc, _argv, name);
168 }
169
172 template <typename T>
173 Arg<T> getArg(const char name, const T& def)
174 {
175 return Arg<T>(*_argc, _argv, name, def);
176 }
177
178 public:
180 static void chdir(const Path& path);
181
183 static Path cwd();
184
189 static Path rootdir();
190
197 static Path tmpdir();
198
203 static void setEnvVar(const std::string& name, const std::string& value);
204
209 static void unsetEnvVar(const std::string& name);
210
215 static std::string getEnvVar(const std::string& name);
216
219 static unsigned long usedMemory();
220
222 ApplicationImpl& impl()
223 { return *_impl; }
224
225 protected:
227 void init(EventLoop& loop);
228
229 private:
230 int _noArgs;
231 int* _argc;
232 char** _argv;
233 ApplicationImpl* _impl;
234 EventLoop* _loop;
235 MainLoop* _owner;
236 Signal<int> _systemSignal;
237};
238
239} // namespace System
240
241} // namespace Pt
242
243#endif // PT_SYSTEM_APPLICATION_H
Command-line option extracted from argc and argv.
Definition Arg.h:211
Connection Management for Signal and Slot Objects.
Definition Connectable.h:50
Multicast Signal to call multiple slots.
Definition Signal.h:190
char ** argv() const
Command line arguments.
Definition Application.h:139
Arg< T > getArg(const char name, const T &def)
Returns the value of a short option.
Definition Application.h:173
void run()
Starts the contained event loop.
Definition Application.h:107
Application(EventLoop *loop)
Default construct with custom event loop.
static Path cwd()
Returns the current directory.
bool raiseSystemSignal(int sig)
Raise a system signal.
static void setEnvVar(const std::string &name, const std::string &value)
Set environment variable.
void exit()
Exits from the contained event loop.
Definition Application.h:112
static void chdir(const Path &path)
Changes the current directory.
Application(int &argc, char **argv=0)
Construct with command line arguments.
bool catchSystemSignal(int sig)
Catch a system signal.
bool ignoreSystemSignal(int sig)
Ignores a system signal.
EventLoop & loop()
Returns the event loop.
Definition Application.h:102
Arg< T > getArg(const char *name)
Returns the value of a long option.
Definition Application.h:149
static Path tmpdir()
Returns the systems tmp directory.
static Path rootdir()
Returns the system root path.
static std::string getEnvVar(const std::string &name)
Get environment variable.
~Application()
Destructor.
Arg< T > getArg(const char name)
Returns the value of a short option.
Definition Application.h:165
Signal< int > & systemSignal()
Notifies when a system signal was caught.
Definition Application.h:129
static unsigned long usedMemory()
Returns the amount of used memory.
static Application & instance()
Returns an instance to the application.
Arg< T > getArg(const char *name, const T &def)
Returns the value of a long option.
Definition Application.h:157
void setArgs(int &argc, char **argv)
Set command line arguments.
Application(EventLoop *loop, int &argc, char **argv=0)
Construct with custom event loop.
static void unsetEnvVar(const std::string &name)
Unset environment variable.
Application()
default Constructor.
int & argc() const
Number of command line arguments.
Definition Application.h:134
Event loop of a thread or process.
Definition EventLoop.h:83
Platform event loop.
Definition MainLoop.h:51
Represents a path in the file-system.
Definition Path.h:48
System programming
Definition Connection.h:26
Core module.
Definition Allocator.h:33