3 * Toyohashi Open Platform for Embedded Real-Time Systems/
4 * Just Standard Profile Kernel
6 * Copyright (C) 2003 by Embedded and Real-Time Systems Laboratory
7 * Toyohashi Univ. of Technology, JAPAN
9 * 上記著作権者は,以下の (1)〜(4) の条件か,Free Software Foundation
10 * によって公表されている GNU General Public License の Version 2 に記
11 * 述されている条件を満たす場合に限り,本ソフトウェア(本ソフトウェア
12 * を改変したものを含む.以下同じ)を使用・複製・改変・再配布(以下,
14 * (1) 本ソフトウェアをソースコードの形で利用する場合には,上記の著作
15 * 権表示,この利用条件および下記の無保証規定が,そのままの形でソー
17 * (2) 本ソフトウェアを,ライブラリ形式など,他のソフトウェア開発に使
18 * 用できる形で再配布する場合には,再配布に伴うドキュメント(利用
19 * 者マニュアルなど)に,上記の著作権表示,この利用条件および下記
21 * (3) 本ソフトウェアを,機器に組み込むなど,他のソフトウェア開発に使
22 * 用できない形で再配布する場合には,次のいずれかの条件を満たすこ
24 * (a) 再配布に伴うドキュメント(利用者マニュアルなど)に,上記の著
25 * 作権表示,この利用条件および下記の無保証規定を掲載すること.
26 * (b) 再配布の形態を,別に定める方法によって,TOPPERSプロジェクトに
28 * (4) 本ソフトウェアの利用により直接的または間接的に生じるいかなる損
29 * 害からも,上記著作権者およびTOPPERSプロジェクトを免責すること.
31 * 本ソフトウェアは,無保証で提供されているものである.上記著作権者お
32 * よびTOPPERSプロジェクトは,本ソフトウェアに関して,その適用可能性も
33 * 含めて,いかなる保証も行わない.また,本ソフトウェアの利用により直
34 * 接的または間接的に生じたいかなる損害に関しても,その責任を負わない.
36 * @(#) $Id: option.h,v 1.1 2009/01/31 05:27:37 suikan Exp $
39 // $Header: /cvsroot/toppersjsp4bf/jsp/cfg/base/option.h,v 1.1 2009/01/31 05:27:37 suikan Exp $
44 #include "base/testsuite.h"
45 #include "base/singleton.h"
51 #define DEFAULT_PARAMETER "#default"
61 class OptionItem : public std::string
64 bool checked; //使用したかどうか
65 std::vector<std::string> param; //オプション
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) {}
73 //オプションパラメータを解析してOptionItemを生成する
74 static bool createItem(OptionItem & item, string & argv);
76 //別のOptionItemが持つパラメータを自分に追加する
77 void mergeItem(const OptionItem & src);
80 inline void addParameter(const std::string & src) throw()
81 { param.push_back(src); }
84 inline size_t countParameter(void) const throw()
85 { return param.size(); }
88 inline bool hasParameter(void) const throw()
89 { return countParameter() != 0; }
91 //位置を指定してパラメータを参照 (無効な位置だと空文字)
92 inline std::string operator[](std::vector<std::string>::size_type offset) const throw()
96 if(offset < countParameter())
97 result = param[offset];
102 //このオプションを使用したことを記す (OptionParameterを介しての利用のみなので "_" をつけておく)
103 inline void _check(void) throw()
107 inline bool isChecked(void) const throw()
111 inline bool isValid(void) const throw()
115 static OptionItem createInvalidItem(void) throw();
118 std::string getParameters(std::string punctuator = " ") const throw();
121 bool checkParameterCount(size_t count, bool allow_zero = false) const throw();
123 TESTSUITE_PROTOTYPE(main)
128 std::map<std::string, OptionItem> container; //set::begin()はconst_iteratorしか返さないので仕方なくmapに変える (実装を考えれば当然か...)
131 std::string program_name;
136 //デフォルトコンストラクタ (テスト用)
137 OptionParameter(void) throw() {}
140 std::map<std::string, OptionItem>::iterator _find(const std::string & name, bool check = true) throw();
145 SINGLETON_CONSTRUCTOR(OptionParameter) throw() {}
148 bool parseOption(int _argc, char const * const * _argv, char const * _default = NULL) throw();
150 //全てのオプションが使用されたかどうかの確認
151 bool validateOption(void) const throw();
154 std::string getInvalidOptions(void) const throw();
157 inline size_t countItem(void) const throw()
158 { return container.size(); }
161 OptionItem get(const std::string & name, bool check = true) throw();
163 inline OptionItem operator [] (const std::string & name) throw()
164 { return get(name); }
166 inline OptionItem operator [] (const char * name) throw()
167 { return get(string(name)); }
170 inline bool find(std::string name) throw()
171 { return get(name).isValid(); }
175 inline bool check(const std::string & name) throw()
176 { return _find(name) != container.end(); }
178 inline bool check(const char * name) throw()
179 { return check(string(name)); }
182 inline const std::string & getProgramName(void) const throw()
183 { return program_name; }
185 //引数列をそのまま取得する (プログラム名は含まず)
186 inline const std::string & getCommandLine(void) const throw()
189 TESTSUITE_PROTOTYPE(main)
192 /* システム内で単一のOptionParameterを取るための関数 */
193 inline OptionParameter & getOptionParameter(void) throw()
194 { return *Singleton<OptionParameter>::getInstance(); }