source: anotherchoice/tags/jsp-1.4.4-full-UTF8/cfg/base/directorymap.h@ 26

Last change on this file since 26 was 26, checked in by ykominami, 12 years ago

initial

File size: 12.4 KB
RevLine 
[26]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 * 上記著作権者
10は,以下の (1)〜(4) の条件か,Free Software Foundation
11 * によってå…
12¬è¡¨ã•ã‚Œã¦ã„ã‚‹ GNU General Public License の Version 2 に記
13 * 述されている条件を満たす場合に限り,本ソフトウェア(本ソフトウェア
14 * を改変したものを含む.以下同じ)を使用・複製・改変・再é…
15å¸ƒï¼ˆä»¥ä¸‹ï¼Œ
16 * 利用と呼ぶ)することを無償で許諾する.
17 * (1) 本ソフトウェアをソースコードの形で利用する場合には,上記の著作
18 * 権表示,この利用条件および下記の無保証規定が,そのままの形でソー
19 * スコード中に含まれていること.
20 * (2) 本ソフトウェアを,ライブラリ形式など,他のソフトウェア開発に使
21 * 用できる形で再é…
22å¸ƒã™ã‚‹å ´åˆã«ã¯ï¼Œå†é…
23å¸ƒã«ä¼´ã†ãƒ‰ã‚­ãƒ¥ãƒ¡ãƒ³ãƒˆï¼ˆåˆ©ç”¨
24 * 者
25マニュアルなど)に,上記の著作権表示,この利用条件および下記
26 * の無保証規定を掲載すること.
27 * (3) 本ソフトウェアを,機器に組み込むなど,他のソフトウェア開発に使
28 * 用できない形で再é…
29å¸ƒã™ã‚‹å ´åˆã«ã¯ï¼Œæ¬¡ã®ã„ずれかの条件を満たすこ
30 * と.
31 * (a) 再é…
32å¸ƒã«ä¼´ã†ãƒ‰ã‚­ãƒ¥ãƒ¡ãƒ³ãƒˆï¼ˆåˆ©ç”¨è€…
33マニュアルなど)に,上記の著
34 * 作権表示,この利用条件および下記の無保証規定を掲載すること.
35 * (b) 再é…
36å¸ƒã®å½¢æ…
37‹ã‚’,別に定める方法によって,TOPPERSプロジェクトに
38 * 報告すること.
39 * (4) 本ソフトウェアの利用により直接的または間接的に生じるいかなる損
40 * 害からも,上記著作権者
41およびTOPPERSプロジェクトをå…
42è²¬ã™ã‚‹ã“と.
43 *
44 * 本ソフトウェアは,無保証で提供されているものである.上記著作権者
45お
46 * よびTOPPERSプロジェクトは,本ソフトウェアに関して,その適用可能性も
47 * 含めて,いかなる保証も行わない.また,本ソフトウェアの利用により直
48 * 接的または間接的に生じたいかなる損害に関しても,その責任を負わない.
49 *
50 * @(#) $Id: directorymap.h,v 1.9 2003/12/15 07:32:13 takayuki Exp $
51 */
52
53// $Header: /home/CVS/configurator/base/directorymap.h,v 1.9 2003/12/15 07:32:13 takayuki Exp $
54
55#ifndef DIRECTORYMAP_H
56#define DIRECTORYMAP_H
57
58#ifdef _MSC_VER
59 #pragma warning(disable:4786)
60#endif
61
62#include "base/message.h"
63#include "base/garbage.h"
64#include "base/singleton.h"
65
66 //MSVC6.0が <cstdio> してもstdにå…
67¥ã‚Œã¦ãã‚Œãªã„ので
68#include <stdio.h>
69
70#include <string>
71#include <map>
72#include <iostream>
73#include <cstring>
74
75class Directory : public std::map<std::string, Directory *>
76{
77private:
78 /*
79 * Directoryクラス : 設計メモ
80 *
81 * ・親子ノード間の関係
82 * 生成 : 親が 子を直接いじってリンクを張る
83 * 破棄 : 子が 親からの独立を依頼する (親から一方的に勘当しない)
84 *
85 * ・NULLセーフ実装
86
87 * 一部の関数は this != NULL を仮定しないで実装
88する (自分への利便性向上)
89 * - addChild
90 * - findChild, openChild (findNode()
91 * - erase(void)
92 * - getFirstChild, getLastChild, getNext, getPrev
93 */
94
95public:
96 enum tagtype
97 {
98 UNKNOWN,
99 POINTER,
100 INTEGER,
101 LITERAL,
102 CONSTLITERAL,
103 OBJECT
104 };
105
106 enum tagflag
107 {
108 NOTHING = 0,
109 DESTRUCT = 1,
110 UNSIGNED = 2
111 };
112
113 enum tagmode
114 {
115 PTRPREFIX = 1
116 };
117
118protected:
119 static int defaultflag;
120
121 enum tagtype type;
122 int flag;
123
124 Directory * parent;
125 std::map<std::string, Directory *>::iterator myself;
126
127 union
128 {
129 void * pointer;
130 long value;
131 const char * const_literal;
132 std::string * literal;
133 class Garbage * instance;
134 } content;
135
136 Directory(const Directory &);
137 void initialize(void);
138 void clearContent(void);
139
140 Directory * findNode(bool, const std::string &);
141 Directory * findNode(bool automatic_creation, const char * key, va_list vl);
142
143public:
144 SINGLETON_CONSTRUCTOR(Directory) { initialize(); }
145
146 Directory(void);
147 Directory(int);
148 Directory(long);
149 Directory(const std::string &);
150 Directory(void *);
151 explicit Directory(const char *);
152 explicit Directory(Garbage *);
153 ~Directory(void);
154
155 Directory & operator =(void *);
156 Directory & operator =(long);
157 Directory & operator =(const char *);
158 Directory & operator =(const std::string &);
159 Directory & operator =(Garbage *);
160
161 bool operator == (enum tagtype cmptype)
162 { return type == cmptype; };
163
164 Directory * operator ()(const std::string & src)
165 { return findChild(src); };
166 Directory * operator ()(const char * src)
167 { return findChild(src); };
168
169 Directory & operator [](const std::string & src)
170 { return *openChild(src); };
171 Directory & operator [](const char * src)
172 { return *openChild(std::string(src)); };
173
174 enum tagtype getType(void) const { return type; };
175 Directory * getParent(void) const { return parent; };
176 Directory * getParent(int) const;
177 Directory * getNext(void) const;
178 Directory * getPrev(void) const;
179 Directory * getFirstChild(void) const;
180 Directory * getLastChild(void) const;
181 Directory * findChild(const std::string &);
182 Directory * findChild(const char *);
183 Directory * findChild(const char *, const char * , ... );
184 Directory * findDescandant(const std::string &, unsigned int = 0xffffffff) const;
185 Directory * openChild(const std::string &);
186 Directory * openChild(const char *);
187 Directory * openChild(const char *, const char *, ... );
188
189 void * operator new(size_t);
190 void * operator new(size_t, std::nothrow_t);
191 void * operator * (void) const;
192
193 operator const long (void) const;
194 operator const unsigned long (void) const;
195 operator const int (void) const;
196 operator const unsigned int (void) const;
197 operator const char (void) const;
198 operator const unsigned char (void) const;
199 operator const char * (void);
200 operator const std::string & (void) const;
201 operator const Garbage * (void) const;
202 operator const void * (void) const;
203
204 void * toPointer(const void * default_value = 0) const;
205 long toInteger(const long default_value = 0) const;
206 std::string toString(const std::string & default_value = "") const;
207
208 bool operator == (int) const;
209 bool operator == (const std::string &) const;
210 bool operator == (const char *) const;
211
212 template<class T>
213 bool operator != (T src) const
214 { return !(this->operator ==(src)); };
215
216
217 Directory * addChild(const std::string &, Directory * = 0);
218 Directory * addChild(const char *, Directory * = 0);
219 Directory * addChild(const std::string &, Directory &);
220 Directory * addChild(const char *, Directory &);
221 Directory * addChild(Directory &);
222 Directory * addChild(Directory * = 0);
223
224 void erase(void);
225 iterator erase(iterator);
226 void disconnect(void);
227 void copyTo(Directory *, int = 0x7fffffff);
228 void dropValue(void);
229 std::map<std::string, Directory *>::size_type size(std::map<std::string, Directory *>::size_type = 0) const;
230
231 const std::string getKey(void) const;
232 bool changeKey(const std::string &);
233 bool changeKey(const char *);
234
235 void Load(std::istream *);
236 void Store(std::ostream *);
237
238 void drawTree(std::ostream * = &std::cerr, int = 0, std::string * = 0);
239 void drawTree_byXML(std::ostream * = &std::cerr, int = 0);
240
241 std::string format(const char *, int mode = 0);
242 std::string format(const std::string &, int mode = 0);
243};
244
245
246//------
247
248inline void Directory::initialize(void)
249{
250 parent = 0;
251 type = UNKNOWN;
252 flag = defaultflag;
253 content.pointer = 0;
254 defaultflag &= ~DESTRUCT;
255}
256
257inline Directory::Directory(void)
258{ initialize(); }
259
260inline Directory::Directory(long val)
261{
262 initialize();
263 *this = val;
264}
265
266inline Directory::Directory(int val)
267{
268 initialize();
269 *this = (long)val;
270}
271
272inline Directory::Directory(void * val)
273{
274 initialize();
275 *this = val;
276}
277
278inline Directory::Directory(const std::string & val)
279{
280 initialize();
281 *this = val;
282}
283
284inline Directory::Directory(const char * val)
285{
286 initialize();
287 *this = val;
288}
289
290inline Directory::Directory(Garbage * val)
291{
292 initialize();
293 *this = val;
294}
295
296inline Directory * Directory::addChild(const char * key, Directory * node)
297{ return addChild(std::string(key), node); }
298
299inline Directory * Directory::addChild(const std::string & key, Directory & node)
300{ return addChild(key, &node); }
301
302inline Directory * Directory::addChild(const char * key, Directory & node)
303{ return addChild(std::string(key), &node); }
304
305inline Directory * Directory::addChild(Directory & node)
306{ return addChild(&node); }
307
308 //このaddChildで追加した要素は、一括削除するか、消してはいけない
309inline Directory * Directory::addChild(Directory * node)
310{
311 char buffer[32];
312 sprintf(buffer,"%03d", (int)size());
313 return addChild(buffer, node);
314}
315
316inline bool Directory::changeKey(const char * key)
317{ return changeKey(std::string(key)); }
318
319inline const std::string Directory::getKey(void) const
320{
321 if(parent == 0)
322 return std::string("/");
323 return (*myself).first;
324}
325
326inline Directory::operator const unsigned long (void) const
327{ return static_cast<const unsigned long>(this->operator const long()); }
328
329inline Directory::operator const int (void) const
330{ return static_cast<const int>(this->operator const long()); }
331
332inline Directory::operator const unsigned int (void) const
333{ return static_cast<const unsigned int>(this->operator const long()); }
334
335inline Directory::operator const char (void) const
336{ return static_cast<const char>(this->operator const long()); }
337
338inline Directory::operator const unsigned char (void) const
339{ return static_cast<const unsigned char>(this->operator const long()); }
340
341inline Directory::operator const char * (void)
342{
343 if(type == CONSTLITERAL)
344 return content.const_literal;
345 if(type == LITERAL)
346 return content.literal->c_str();
347 ExceptionMessage("Bad cast exception raised","不正キャスト例外").throwException();
348 return 0;
349}
350
351inline Directory::operator const std::string &(void) const
352{
353 if(type == LITERAL)
354 return *content.literal;
355 ExceptionMessage("Bad cast exception raised","不正キャスト例外").throwException();
356 return *(std::string *)0;
357}
358
359inline Directory::operator const Garbage * (void) const
360{
361 if(type == OBJECT)
362 return content.instance;
363 ExceptionMessage("Bad cast exception raised","不正キャスト例外").throwException();
364 return 0;
365}
366
367inline Directory::operator const void * (void) const
368{ return **this; }
369
370inline Directory * Directory::findChild(const std::string & path)
371{ return findNode(false, path); }
372
373inline Directory * Directory::openChild(const std::string & path)
374{ return findNode(true, path); }
375
376inline Directory * Directory::getParent(int level) const
377{
378 const Directory * node = this;
379 while(level-- > 0 && node != 0)
380 node = node->parent;
381 return const_cast<Directory *>(node);
382}
383
384inline Directory * Directory::getFirstChild(void) const
385{
386 if(this == 0 || size()== 0)
387 return 0;
388 return (*begin()).second;
389}
390
391inline Directory * Directory::getLastChild(void) const
392{
393 if(this == 0 || size()== 0)
394 return 0;
395 return (*rbegin()).second;
396}
397
398inline bool Directory::operator == (int src) const
399{ return type == INTEGER && content.value == src; }
400
401inline bool Directory::operator == (const std::string & src) const
402{ return (type == LITERAL && src.compare(*content.literal) == 0) || (type == CONSTLITERAL && src.compare(content.const_literal) == 0); }
403
404inline bool Directory::operator == (const char * src) const
405{ return (type == LITERAL && content.literal->compare(src) == 0) || (type == CONSTLITERAL && strcmp(content.const_literal, src)== 0); }
406
407inline std::string Directory::format(const char * src, int mode)
408{ return format(std::string(src), mode); }
409
410inline void Directory::dropValue(void)
411{ clearContent(); }
412
413#endif
414
Note: See TracBrowser for help on using the repository browser.