OSDN Git Service

Change the directry name to hirado
[trx-305dsp/dsp.git] / hirado / kernel / cfg / base / option.h
1 /*
2  *  TOPPERS/JSP Kernel
3  *      Toyohashi Open Platform for Embedded Real-Time Systems/
4  *      Just Standard Profile Kernel
5  * 
6  *  Copyright (C) 2003 by Embedded and Real-Time Systems Laboratory
7  *                              Toyohashi Univ. of Technology, JAPAN
8  * 
9  *  上記著作権者は,以下の (1)〜(4) の条件か,Free Software Foundation 
10  *  によって公表されている GNU General Public License の Version 2 に記
11  *  述されている条件を満たす場合に限り,本ソフトウェア(本ソフトウェア
12  *  を改変したものを含む.以下同じ)を使用・複製・改変・再配布(以下,
13  *  利用と呼ぶ)することを無償で許諾する.
14  *  (1) 本ソフトウェアをソースコードの形で利用する場合には,上記の著作
15  *      権表示,この利用条件および下記の無保証規定が,そのままの形でソー
16  *      スコード中に含まれていること.
17  *  (2) 本ソフトウェアを,ライブラリ形式など,他のソフトウェア開発に使
18  *      用できる形で再配布する場合には,再配布に伴うドキュメント(利用
19  *      者マニュアルなど)に,上記の著作権表示,この利用条件および下記
20  *      の無保証規定を掲載すること.
21  *  (3) 本ソフトウェアを,機器に組み込むなど,他のソフトウェア開発に使
22  *      用できない形で再配布する場合には,次のいずれかの条件を満たすこ
23  *      と.
24  *    (a) 再配布に伴うドキュメント(利用者マニュアルなど)に,上記の著
25  *        作権表示,この利用条件および下記の無保証規定を掲載すること.
26  *    (b) 再配布の形態を,別に定める方法によって,TOPPERSプロジェクトに
27  *        報告すること.
28  *  (4) 本ソフトウェアの利用により直接的または間接的に生じるいかなる損
29  *      害からも,上記著作権者およびTOPPERSプロジェクトを免責すること.
30  * 
31  *  本ソフトウェアは,無保証で提供されているものである.上記著作権者お
32  *  よびTOPPERSプロジェクトは,本ソフトウェアに関して,その適用可能性も
33  *  含めて,いかなる保証も行わない.また,本ソフトウェアの利用により直
34  *  接的または間接的に生じたいかなる損害に関しても,その責任を負わない.
35  * 
36  *  @(#) $Id: option.h,v 1.1 2009/01/31 05:27:37 suikan Exp $
37  */
38
39 // $Header: /cvsroot/toppersjsp4bf/jsp/cfg/base/option.h,v 1.1 2009/01/31 05:27:37 suikan Exp $
40
41 #ifndef OPTION_H
42 #define OPTION_H
43
44 #include "base/testsuite.h"
45 #include "base/singleton.h"
46
47 #include <string>
48 #include <vector>
49 #include <map>
50
51 #define DEFAULT_PARAMETER "#default"
52
53 using namespace std;
54
55 /*
56  *   オプションパラメータを管理するクラス
57  */
58 class OptionParameter
59 {
60 public:
61     class OptionItem : public std::string
62     {
63     protected:
64         bool                      checked;  //使用したかどうか
65         std::vector<std::string>  param;    //オプション
66
67     public:
68             //コンストラクタ
69         OptionItem(void) throw() : string(DEFAULT_PARAMETER), checked(false) {}
70         OptionItem(const std::string & name) throw() : string(name), checked(false) {}
71         OptionItem(const OptionItem & src) throw() : string(src), checked(src.checked), param(src.param) {}
72
73             //オプションパラメータを解析してOptionItemを生成する
74         static bool createItem(OptionItem & item, string & argv);
75
76             //別のOptionItemが持つパラメータを自分に追加する
77         void mergeItem(const OptionItem & src);
78
79             //パラメータの追加
80         inline void addParameter(const std::string & src) throw()
81         {   param.push_back(src);   }
82
83             //パラメータ数の取得
84         inline size_t countParameter(void) const throw()
85         {   return param.size();    }
86
87             //パラメータを持っているかどうか
88         inline bool hasParameter(void) const throw()
89         {   return countParameter() != 0;   }
90
91             //位置を指定してパラメータを参照 (無効な位置だと空文字)
92         inline std::string operator[](std::vector<std::string>::size_type offset) const throw()
93         {
94             string result;
95
96             if(offset < countParameter())
97                 result = param[offset];
98
99             return result;
100         }
101
102             //このオプションを使用したことを記す (OptionParameterを介しての利用のみなので "_" をつけておく)
103         inline void _check(void) throw()
104         {   checked = true;   }
105
106             //このオプションに触ったかどうか
107         inline bool isChecked(void) const throw()
108         {   return checked;   }
109
110             //有効かどうか (ななしは無効)
111         inline bool isValid(void) const throw()
112         {   return !empty();   }
113
114             //無効なオプションアイテムの生成
115         static OptionItem createInvalidItem(void) throw();
116
117             //全てのパラメータ結合したものを取得
118         std::string getParameters(std::string punctuator = " ") const throw();
119
120             //パラメータの個数をチェック
121         bool checkParameterCount(size_t count, bool allow_zero = false) const throw();
122
123         TESTSUITE_PROTOTYPE(main)
124     };
125
126 protected:
127         //オプションを格納する変数
128     std::map<std::string, OptionItem> container;    //set::begin()はconst_iteratorしか返さないので仕方なくmapに変える (実装を考えれば当然か...)
129
130         //プログラム名称
131     std::string program_name;
132
133         //コマンドラインに渡された引数そのもの
134     std::string cmdline;    
135
136         //デフォルトコンストラクタ (テスト用)
137     OptionParameter(void) throw() {}
138
139         //要素の検出
140     std::map<std::string, OptionItem>::iterator _find(const std::string & name, bool check = true) throw();
141
142 public:
143
144         //シングルトンコンストラクタ
145     SINGLETON_CONSTRUCTOR(OptionParameter) throw() {}
146
147         //オプションパラメータのパース
148     bool parseOption(int _argc, char const * const * _argv, char const * _default = NULL) throw();
149
150         //全てのオプションが使用されたかどうかの確認
151     bool validateOption(void) const throw();
152
153         //使用されなかったオプション列を取得
154     std::string getInvalidOptions(void) const throw();
155
156         //アイテムの数を数える
157     inline size_t countItem(void) const throw()
158     {   return container.size();   }
159
160         //オプションアイテムの参照
161     OptionItem get(const std::string & name, bool check = true) throw();
162     
163     inline OptionItem operator [] (const std::string & name) throw()
164     {   return get(name);   }
165     
166     inline OptionItem operator [] (const char * name) throw()
167     {   return get(string(name));   }
168
169         //オプションの有無の検出
170     inline bool find(std::string name) throw()
171     {   return get(name).isValid(); }
172
173
174         //参照マークをつける
175     inline bool check(const std::string & name) throw()
176     {   return _find(name) != container.end();   }
177
178     inline bool check(const char * name) throw()
179     {   return check(string(name));   }
180
181         //プログラム名を取得する
182     inline const std::string & getProgramName(void) const throw()
183     {   return program_name;    }
184
185         //引数列をそのまま取得する (プログラム名は含まず)
186     inline const std::string & getCommandLine(void) const throw()
187     {   return cmdline;   }
188
189     TESTSUITE_PROTOTYPE(main)
190 };
191
192     /* システム内で単一のOptionParameterを取るための関数 */
193 inline OptionParameter & getOptionParameter(void) throw()
194 {   return *Singleton<OptionParameter>::getInstance();   }
195
196 #endif
197
198