File: application.h

package info (click to toggle)
gringo 4.4.0-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 8,260 kB
  • ctags: 10,755
  • sloc: cpp: 55,049; python: 629; yacc: 569; sh: 124; makefile: 23
file content (132 lines) | stat: -rw-r--r-- 5,453 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
//
//  Copyright (c) Benjamin Kaufmann 2004
//
//  This is free software; you can redistribute it and/or modify
//  it under the terms of the GNU General Public License as published by
//  the Free Software Foundation; either version 2 of the License, or
//  (at your option) any later version. 
// 
//  This file is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//  GNU General Public License for more details.
//
//  You should have received a copy of the GNU General Public License
//  along with this program. If not, see <http://www.gnu.org/licenses/>.
//
//
// NOTE: ProgramOptions is inspired by Boost.Program_options
//       see: www.boost.org/libs/program_options
//
#ifndef APP_OPTIONS_H_INCLUDED
#define APP_OPTIONS_H_INCLUDED

#ifdef _MSC_VER
#pragma warning (disable : 4200) // nonstandard extension used : zero-sized array
#pragma once
#endif

#include <string>
#include <utility>
#include "program_options.h"
#include <stdio.h>
#include <signal.h>

namespace ProgramOptions {
typedef std::vector<std::string> StringSeq;
/////////////////////////////////////////////////////////////////////////////////////////
// Application base class
/////////////////////////////////////////////////////////////////////////////////////////
#define WRITE_STDERR(type,sys,msg) ( fprintf(stderr, "*** %-5s: (%s): %s\n", (type),(sys),(msg)), fflush(stderr) )
class Application {
public:
	//! Description of and max value for help option.
	typedef std::pair<const char*, unsigned> HelpOpt;
	/*!
	 * \name Basic functions.
	 */
	//@{
	//! Returns the name of this application.
	virtual const char* getName()       const   = 0;
	//! Returns the version number of this application.
	virtual const char* getVersion()    const   = 0;
	//! Returns a null-terminated array of signals that this application handles.
	virtual const int*  getSignals()    const { return 0; }
	//! Returns the usage information of this application.
	virtual const char* getUsage()      const { return "[options]"; }
	//! Returns the application's help option and its description.
	virtual HelpOpt     getHelpOption() const { return HelpOpt("Print help information and exit", 1); }
	//! Returns the parser function for handling positional options.
	virtual PosOption   getPositional() const { return 0; }
	//! Prints the given error message to stderr.
	virtual void        error(const char* msg) const { WRITE_STDERR("ERROR", getName(), msg); }
	//! Prints the given info message to stderr.
	virtual void        info(const char*  msg) const { WRITE_STDERR("Info",  getName(), msg); }
	//! Prints the given warning message to stderr.
	virtual void        warn(const char*  msg) const { WRITE_STDERR("Warn",  getName(), msg); }
	//@}
	
	/*!
	 * \name Main functions.
	 */
	//@{
	//! Runs this application with the given command-line arguments.
	int main(int argc, char** argv);
	//! Sets the value that should be returned as the application's exit code. 
	void setExitCode(int n);
	//! Returns the application's exit code.
	int  getExitCode() const;
	//! Returns the application object that is running.
	static Application* getInstance();
	//! Prints the application's help information (called if options contain '--help').
	virtual void        printHelp(const OptionContext& root);
	//! Prints the application's version message (called if options contain '--version').
	virtual void        printVersion();
	//! Prints the application's usage message (default is: "usage: getName() getUsage()").
	virtual void        printUsage();
	//@}
protected:
	/*!
	 * \name Life cycle and option handling
	 */
	//@{
	//! Adds all application options to the given context.
	virtual void        initOptions(OptionContext& root) = 0;
	//! Validates parsed options. Shall throw to signal error.
	virtual void        validateOptions(const OptionContext& root, const ParsedOptions& parsed, const ParsedValues& values) = 0;
	//! Called once after option processing is done.
	virtual void        setup() = 0;
	//! Shall run the application. Called after setup and option processing.
	virtual void        run()   = 0;
	//! Called after run returned. The default is a noop.
	virtual void        shutdown();
	//! Called on an exception from run(). The default terminates the application.
	virtual void        onUnhandledException();
	//! Called when a signal is received. Tthe default terminates the application.
	virtual bool        onSignal(int);
	//@}
protected:
	Application();
	virtual ~Application();
	void     shutdown(bool hasError);
	void     exit(int exitCode) const;
	unsigned verbose() const;
	void     setVerbose(unsigned v);
	void     killAlarm();
	int      blockSignals();
	void     unblockSignals(bool deliverPending);
	void     processSignal(int sigNum);
private:
	bool getOptions(int argc, char** argv);
	int                   exitCode_;  // application's exit code
	unsigned              timeout_;   // active time limit or 0 for no limit
	unsigned              verbose_;   // active verbosity level
	bool                  fastExit_;  // force fast exit?
	volatile long         blocked_;   // temporarily block signals?
	volatile long         pending_;   // pending signal or 0 if no pending signal
	static Application*   instance_s; // running instance (only valid during run()).
	static void sigHandler(int sig);  // signal/timeout handler
};

}
#endif