aboutsummaryrefslogtreecommitdiff
path: root/src/paramproc.h
blob: d857193eb86c06a95a9b4c46e059f5aba1025b4a (plain)
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
#ifndef PARAM_PROC_H
#define PARAM_PROC_H

#include <stdint.h>
#include <string>
#include <list>
#include "staticstring.h"

class ParamProc
{
public:
	class ParamPtr
	{
	public:
		ParamPtr();
		ParamPtr( std::string *str );
		ParamPtr( uint64_t    *uint64  );
		ParamPtr( uint32_t    *uint32  );
		ParamPtr( uint16_t    *uint16  );
		ParamPtr( uint8_t     *uint8   );
		ParamPtr( int64_t     *int64   );
		ParamPtr( int32_t     *int32   );
		ParamPtr( int16_t     *int16   );
		ParamPtr( int8_t      *int8    );
		ParamPtr( float       *float32 );
		ParamPtr( double      *float64 );
		ParamPtr( long double *float96 );
		ParamPtr( bool        *bln     );

		enum
		{
			vtunset,
			vtstr,
			vtuint64,
			vtuint32,
			vtuint16,
			vtuint8,
			vtint64,
			vtint32,
			vtint16,
			vtint8,
			vtfloat32,
			vtfloat64,
			vtfloat96,
			vtbln,
		};
		ParamPtr &operator=( ParamPtr &ptr );
		ParamPtr &operator=( const char *str );

		bool isSet();

	private:
		int type;
		union
		{
			std::string *str;
			uint64_t *uint64;
			uint32_t *uint32;
			uint16_t *uint16;
			uint8_t *uint8;
			int64_t *int64;
			int32_t *int32;
			int16_t *int16;
			int8_t *int8;
			float *float32;
			double *float64;
			long double *float96;
			bool *bln;
		} val;
	};

	typedef int (ParamProc::*Proc)( int, char *[] );

	typedef struct ArgSpec
	{
		uint8_t nFlags;
		StaticString sWord;
		char cChar;
		Proc proc;
		ParamProc::ParamPtr val;
		StaticString sExtra;
		StaticString sDesc;
		StaticString sValue;
	} ArgSpec;

public:
	ParamProc();
	virtual ~ParamProc();
	
	void addParam( const char *lpWord, char cChar, Proc proc, ParamPtr val,
		const char *lpDesc=NULL, const char *lpExtra=NULL,
		const char *lpValue=NULL
		);
	void addParam( const char *lpWord, char cChar, Proc proc,
		const char *lpDesc=NULL, const char *lpExtra=NULL,
		const char *lpValue=NULL
		);
	void addParam( const char *lpWord, char cChar, ParamPtr val,
		const char *lpDesc=NULL, const char *lpExtra=NULL,
		const char *lpValue=NULL
		);

	void addParam( const char *lpWord, Proc proc, ParamPtr val,
		const char *lpDesc=NULL, const char *lpExtra=NULL,
		const char *lpValue=NULL
		);
	void addParam( const char *lpWord, Proc proc,
		const char *lpDesc=NULL, const char *lpExtra=NULL,
		const char *lpValue=NULL
		);
	void addParam( const char *lpWord, ParamPtr val,
		const char *lpDesc=NULL, const char *lpExtra=NULL,
		const char *lpValue=NULL
		);
	
	void addParam( char cChar, Proc proc, ParamPtr val,
		const char *lpDesc=NULL, const char *lpExtra=NULL,
		const char *lpValue=NULL
		);
	void addParam( char cChar, Proc proc,
		const char *lpDesc=NULL, const char *lpExtra=NULL,
		const char *lpValue=NULL
		);
	void addParam( char cChar, ParamPtr val,
		const char *lpDesc=NULL, const char *lpExtra=NULL,
		const char *lpValue=NULL
		);

	void process( int argc, char *argv[] );
	void addHelpBanner( const char *sHelpBanner );

private:
	ArgSpec *checkWord( const char *arg );
	ArgSpec *checkLetr( const char arg );

public:
	virtual int cmdParam( int argc, char *argv[] );
	virtual int unknownParam( int argc, char *argv[] );
	virtual int help( int argc, char *argv[] );

private:
	typedef struct Banner
	{
		StaticString sBanner;
		ArgSpec *pBefore;
	} Banner;
	std::list<Banner *> lBan;
	std::list<ArgSpec *> lArg;
};

#define mkproc( cls ) static_cast<int (ParamProc::*)( int, char *[])>(&cls)

#endif