OSDN Git Service

Change the directry name to hirado
[trx-305dsp/dsp.git] / hirado / kernel / cfg / base / message.h
1 /*
2  *  TOPPERS/JSP Kernel
3  *      Toyohashi Open Platform for Embedded Real-Time Systems/
4  *      Just Standard Profile Kernel
5  * 
6  *  Copyright (C) 2000-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: message.h,v 1.1 2009/01/31 05:27:37 suikan Exp $
37  */
38
39 // $Header: /cvsroot/toppersjsp4bf/jsp/cfg/base/message.h,v 1.1 2009/01/31 05:27:37 suikan Exp $
40
41 #ifndef MESSAGE_H
42 #define MESSAGE_H
43
44 #ifdef _MSC_VER
45 #  pragma warning(disable:4290) //C++ の例外の指定は無視されます。関数が __declspec(nothrow) でないことのみ表示されます。
46 #  pragma warning(disable:4786) //デバッグ情報で識別子が255文字に切り捨てられました。
47 #endif
48
49 #include <string>
50 #include <sstream>
51 #include <stdarg.h>
52 #include <stdexcept>
53
54 //#include "base/testsuite.h"
55 #include "base/singleton.h"
56 #include "base/except.h"
57
58     /*
59      *   文字列整形ヘルパークラス
60      */
61 class Formatter
62 {
63 protected:
64     std::string       templatestring;    //テンプレート文字列
65     std::stringstream content;           //文字列整形と書式済み文字列可能場所用ストリーム
66     bool              accept;            //引数を受入可能かどうか
67
68         //次の引数の挿入先へと移動
69     void shift(void) throw();
70
71         //未設定の引数を (null) で置換
72     void shift_all(void) throw();
73
74 public:
75         //デフォルトコンストラクタ
76     Formatter(void) throw();
77
78         //コンストラクタ
79     Formatter(std::string src) throw();
80
81         //コピーコンストラクタ
82     Formatter(const Formatter & src) throw();
83
84         //テンプレート文字列の設定 (中身は消える)
85     void assign(std::string src) throw();
86
87         //内容のクリア
88     void clear(void) throw();
89
90         //代入演算子
91     Formatter & operator =(const Formatter & src) throw();
92
93         //引数の設定
94     template<class T>
95     Formatter & operator << (const T & src) throw()
96     {
97         std::string::size_type pos;
98
99             //引数を受入可能なら
100         if(accept) {
101             pos = content.str().size();
102             content << src;
103
104                 /* 空文字でなければ次の挿入場所に移動 */
105             if(pos != content.str().size())
106                 shift();
107         }
108         return *this;
109     }
110
111         //内部メッセージの取り出し
112     std::string str(void) throw();
113
114         //標準出力ストリームへの出力用 operator << 定義
115     friend inline std::ostream & operator << (std::ostream & out, Formatter msg) throw()
116     {
117         out << msg.content.str();
118         return out;
119     }
120
121 //    TESTSUITE_PROTOTYPE(main)
122 };
123
124     /*
125      *   メッセージ出力用に2言語対応を行った文字列整形ヘルパークラス
126      */
127 class Message : public Formatter
128 {
129 public:
130     enum tagLanguage
131     {
132         ENGLISH  = 0,
133         JAPANESE = 1,
134
135         LAST_LANG,              //最後の位置
136         NEUTRAL  = ENGLISH,     //標準言語
137     };
138
139         /* 出力メッセージ言語制御クラス */
140     class MessageControl
141     {
142     protected:
143         int language;   //どの言語を使用するのか
144
145     public:
146             //コンストラクタ
147         SINGLETON_CONSTRUCTOR(MessageControl) throw() : language(NEUTRAL) {};
148
149             //アクセサ
150         inline void setLanguage(int _language) throw()  { language = _language; }
151         inline int  getLanguage(void) const    throw()  { return language; }
152
153         const char * select(const char * msg1, const char * msg2, va_list vl ) throw();
154     };
155
156 protected:
157
158         //派生クラス用コンストラクタ
159     void _initialize(const char * src1, const char * src2, va_list vl) throw();
160
161 public:
162         //デフォルトコンストラクタ
163     Message(void) throw();
164
165         //コンストラクタ (単一言語)
166     Message(std::string src) throw();
167
168         //コンストラクタ (複数言語)
169     Message(const char * src1, const char * src2, ... ) throw();
170
171         //コピーコンストラクタ
172     Message(const Message & src) throw();
173
174         //テンプレート文字列の設定 (中身は消える)
175     void assign(std::string src) throw() { Formatter::assign(src); }
176     void assign(const char * src1, const char * src2, ... ) throw();
177
178         //言語選択
179     static void selectLanguage(enum tagLanguage lang = NEUTRAL) throw();
180         
181         //言語の取得
182     static enum tagLanguage getCurrentLanguage(void) throw()
183     {   return static_cast<enum tagLanguage>(Singleton<MessageControl>::getInstance()->getLanguage());   }
184
185     //TESTSUITE_PROTOTYPE(main)
186 };
187
188
189     /*
190      *   冗長出力用文字列整形ヘルパークラス
191      */
192 class VerboseMessage : public Message
193 {
194 public:
195         //共通の冗長出力情報を持つクラス
196     class VerboseControl
197     {
198     public:
199         bool           verbose;     //冗長出力を行うかどうか
200         std::ostream * out;         //出力先
201
202             //コンストラクタ
203         SINGLETON_CONSTRUCTOR(VerboseControl) throw() : verbose(false), out(0)
204         {}
205
206             //冗長出力制御
207         template<class T>
208         inline const VerboseControl & operator << (const T & src) const throw()
209         {
210             if(verbose && out != 0)
211                 (*out) << src;
212             return *this;
213         }
214
215             //verboseアクセサ
216         void setVerbose(bool _verbose) throw()
217         {   verbose = _verbose;   }
218         bool getVerbose(void) const throw()
219         {   return verbose;   }
220
221             //outアクセサ
222         void setStream(std::ostream * _out) throw()
223         {   out = _out;   }
224         std::ostream * getStream(void) throw()
225         {   return out;   }
226     };
227
228         //冗長出力ストリーム (単純な抑止機構つきラッパークラス)
229     class VerboseStream
230     {
231     protected:
232         std::ostream * out;
233
234     public:
235         VerboseStream(std::ostream * _out = 0) throw() : out(_out) {};
236
237         template<class T>
238         VerboseStream & operator << (const T & src) throw()
239         {
240             if(out != 0)
241                 (*out) << src;
242             return *this;
243         }
244     };
245
246 protected:
247         //デフォルトコンストラクタ (テスト用)
248     VerboseMessage(void) throw();
249
250 public:
251
252         //コンストラクタ
253     VerboseMessage(const char * src) throw();
254     VerboseMessage(const std::string & src) throw();
255     VerboseMessage(const char * src1, const char * src2, ... ) throw();
256
257         //デストラクタ
258     ~VerboseMessage(void) throw();
259
260         //冗長出力制御 (アクセサ回送)
261     inline static void setVerbose(bool _verbose) throw()
262     {   Singleton<VerboseControl>::getInstance()->setVerbose(_verbose);   }
263
264         //冗長出力先ストリーム設定 (アクセサ回送)
265     inline static void setStream(std::ostream * _stream) throw()
266     {   Singleton<VerboseControl>::getInstance()->setStream(_stream);   }
267
268         //冗長出力制御の状態取得
269     inline static bool getVerbose(void) throw()
270     {   return Singleton<VerboseControl>::getInstance()->getVerbose();   }
271
272         //冗長出力先ストリームの取得 (設定したものと同じものは出てこない)
273     inline static VerboseStream getStream(void) throw()
274     {   return VerboseStream(getVerbose() ? Singleton<VerboseControl>::getInstance()->getStream() : 0);   }
275
276     //TESTSUITE_PROTOTYPE(main)
277 };
278
279     /*
280      *   例外メッセージ用文字列整形ヘルパークラス
281      */
282 class ExceptionMessage : public Message
283 {
284 public:
285         //例外の危険度レベル
286     enum tagLevel {
287         DEFAULT = 0,
288         FATAL   = 1,
289         WARNING = 2
290     };
291
292 #ifdef EXCEPT_H
293     class ExceptionMessageException : public Exception
294     {
295     friend class ExceptionMessage;
296     protected:
297         ExceptionMessageException(int _code, std::string _details) throw() : Exception("ExceptionMessage", _code, _details) {}
298     };
299 #endif
300
301 protected:
302     enum tagLevel level;
303
304 public:
305         //コンストラクタ
306     ExceptionMessage(const char * src) throw();
307     ExceptionMessage(const std::string & src) throw();
308     ExceptionMessage(const char * src1, const char * src2, ...) throw();
309
310     ExceptionMessage(enum tagLevel level = DEFAULT) throw();
311     ExceptionMessage(enum tagLevel level, const char * src) throw();
312     ExceptionMessage(enum tagLevel level, const std::string & src) throw();
313     ExceptionMessage(enum tagLevel level, const char * src1, const char * src2, ...) throw();
314
315     ExceptionMessage(const ExceptionMessage & src) throw();
316
317
318         //危険度レベル参照
319     inline bool operator == (enum tagLevel _level) const throw()
320     {   return level == _level;   }
321
322         //危険度レベル参照
323     inline bool operator != (enum tagLevel _level) const throw()
324     {   return !(operator ==(_level));   }
325
326         //引数の設定 (返却値の型をMessage & から ExceptionMessage & にするための小細工)
327     template<class T>
328     inline ExceptionMessage & operator << (const T & src) throw()
329     {
330         Message::operator << ( src );
331         return *this;
332     }
333
334 #ifdef EXCEPT_H     //Exceptionクラスを使用する場合
335         //例外の発生
336     bool throwException(void) throw(Exception)
337     {
338         ExceptionMessageException exc(level, str());
339         return exc.throwException();
340     }
341         //マニピュレータ
342     inline ExceptionMessage & operator << ( ExceptionMessage & (* func)(ExceptionMessage &) ) throw(Exception)
343     {   return (*func)(*this);   }
344
345 #else               //stdexceptを使用する場合
346         //例外の発生
347     template<class T>
348     bool throwException(void) throw(T)
349     {
350         throw T(str());
351         return true;
352     }
353         //マニピュレータ
354     inline ExceptionMessage & operator << ( ExceptionMessage & (* func)(ExceptionMessage &) ) throw(T)
355     {   return (*func)(*this);   }
356 #endif
357
358     //TESTSUITE_PROTOTYPE(main)
359 };
360
361 #ifdef EXCEPT_H     //Exceptionクラスを使用する場合
362
363         //例外をスローするためのマニピュレータ
364     inline ExceptionMessage & throwException(ExceptionMessage & excmsg) throw(Exception)
365     {
366         excmsg.throwException();
367         return excmsg;
368     }
369
370 #else
371         //例外をスローするためのマニピュレータ
372     template<class T>
373     inline ExceptionMessage & throwException(ExceptionMessage & excmsg) throw(T)
374     {
375         excmsg.throwException<T>();
376         return excmsg;
377     }
378
379 #endif
380
381
382
383     /*
384      *   デバッグ情報出力用文字列整形ヘルパークラス
385      */
386 class DebugMessage : public Formatter
387 {
388 public:
389         //共通の冗長出力情報を持つクラス (シングルトンにするのでVerboseControlを同じものをクラス化する)
390     class DebugControl : public VerboseMessage::VerboseControl
391     { public: SINGLETON_CONSTRUCTOR_(DebugControl) throw() : VerboseMessage::VerboseControl(_singleton) {} };
392
393
394 protected:
395         //デフォルトコンストラクタ (テスト用)
396     DebugMessage(void) throw();
397
398 public:
399
400         //コンストラクタ
401     DebugMessage(std::string src) throw();
402
403         //デストラクタ
404     ~DebugMessage(void) throw();
405
406         //冗長出力制御 (アクセサ回送)
407     inline static void setVerbose(bool _verbose) throw()
408     {   Singleton<DebugControl>::getInstance()->setVerbose(_verbose);   }
409
410         //冗長出力先ストリーム設定 (アクセサ回送)
411     inline static void setStream(std::ostream * _stream) throw()
412     {   Singleton<DebugControl>::getInstance()->setStream(_stream);   }
413
414         //冗長出力制御の状態取得
415     inline static bool getVerbose(void) throw()
416     {   return Singleton<DebugControl>::getInstance()->getVerbose();   }
417
418         //冗長出力先ストリームの取得 (設定したものと同じものは出てこない)
419     inline static VerboseMessage::VerboseStream getStream(void) throw()
420     {   return VerboseMessage::VerboseStream(getVerbose() ? Singleton<DebugControl>::getInstance()->getStream() : 0);   }
421
422 //  TESTSUITE_PROTOTYPE(main)
423 };
424
425
426 #endif  //MESSAGE_H
427
428