OSDN Git Service

43d5982b421362f5ad5d3b43c019fc34ad13ced4
[trx-305dsp/dsp.git] / trx305 / kernel / cfg / base / filecontainer.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: filecontainer.h,v 1.1 2009/01/31 05:27:37 suikan Exp $
37  */
38
39 // $Header: /cvsroot/toppersjsp4bf/jsp/cfg/base/filecontainer.h,v 1.1 2009/01/31 05:27:37 suikan Exp $
40
41 #ifndef FILECONTAINER_H
42 #define FILECONTAINER_H
43
44 #ifdef _MSC_VER
45 #pragma warning(disable:4786) //デバッグ情報を255文字に切り詰めました
46 #endif
47
48 #include "testsuite.h"
49
50 #include <string>
51 #include <map>
52
53 #include "base/except.h"
54 #include "base/message.h"
55 #include "base/collection.h"
56
57 class FileContainer : public RuntimeObject
58 {
59 public:
60     typedef unsigned long address_t;
61
62     struct tagVariableInfo {
63         address_t  address;
64         union {
65             size_t     size;
66             int        value;
67         };
68     };
69
70     enum tagByteOrder { LITTLE, BIG, HOSTORDER=LITTLE, UNKNOWN };   /* UNKNOWNはテスト用 */
71
72 protected:
73     enum tagByteOrder byteorder;
74
75     std::map<std::string, struct tagVariableInfo> variableinfo;
76
77     FileContainer(void) throw() : byteorder(HOSTORDER) {}
78     virtual ~FileContainer(void) throw() {}
79
80 public:
81         /* インタフェース部 */
82     virtual void                    attachModule(const std::string & filename) throw(Exception) = 0;
83     virtual void                    loadContents(void * dest, address_t address, size_t size) throw(Exception) = 0;
84     virtual address_t               getSymbolAddress(const std::string & symbol) throw(Exception) = 0;
85     virtual std::string             getArchitecture(void) throw(Exception) = 0;
86
87     virtual struct tagVariableInfo  getVariableInfo(const std::string & name) throw(Exception);
88     virtual void                    attachInfo(const std::string & filename) throw(Exception);
89
90         /* 登録されているコンテナの呼び出し */
91     static inline FileContainer * getInstance(void) throw(Exception)
92     {
93         FileContainer * result;
94         RuntimeObjectTable::getInstance(&result);
95         if(result == 0)
96             ExceptionMessage("[Internal error] Filecontainer has no instance.","[内部エラー] FileContainerのインスタンスがありません").throwException();
97         return result;
98     }
99
100     enum tagByteOrder getByteOrder(void) const
101     {   return byteorder;   }
102 };
103
104 class TargetVariableBase
105 {
106 protected:
107     FileContainer::address_t address;               //変数のアドレス
108     size_t                   size;                  //変数のサイズ
109     size_t                   offset;                //属する構造体の先頭からのオフセット
110     size_t                   structure_size;        //属する構造体の大きさ
111     bool                     loaded;                //値が読出し済みであることを保持するフラグ
112
113     inline TargetVariableBase(FileContainer::address_t _address, size_t _size) throw()
114         : address(_address), size(_size), offset(0), structure_size(_size), loaded(false)
115     {}
116
117     inline TargetVariableBase(FileContainer::address_t addr, size_t sz, size_t ofs, size_t ssz) throw()
118         : address(addr), size(sz), offset(ofs), structure_size(ssz), loaded(false)
119     {}
120     
121     TargetVariableBase(const std::string & sym) throw();
122     TargetVariableBase(const std::string & sym, size_t _size) throw();
123     TargetVariableBase(const std::string & sym, const std::string & sz) throw();
124     TargetVariableBase(const TargetVariableBase & src) throw();
125
126     virtual ~TargetVariableBase(void) throw() {}
127
128         /* 内容の取得 */
129     void loadContent(void * dest, size_t dest_size) throw(Exception);
130
131         /* エンディアン変換 */
132     void changeEndian(char * buffer, size_t size) throw();
133
134         /* アドレスの移動 (ついでにloadedを下げる) */
135     inline void setAddress(int offset) throw()
136     {
137         if(isValid()) {
138             address += offset;
139             loaded   = false;
140         }
141     }
142
143 public:
144     inline bool isValid(void) const throw()
145     {   return this != 0 && address != 0 && size != 0 && structure_size != 0;   }
146
147     inline size_t getSize(void) const throw()
148     {   return this != 0 ? size : 0;   }
149
150     inline size_t getOffset(void) const throw()
151     {   return this != 0 ? offset : 0;   }
152
153     inline size_t getStructureSize(void) const throw()
154     {   return this != 0 ? structure_size : 0;   }
155
156     inline FileContainer::address_t getAddress(void) const throw()
157     {   return this != 0 ? address : 0;   }
158
159     inline bool isLoaded(void) const throw()
160     {   return this != 0 ? loaded : false;   }
161
162         /* 基本的な操作 */
163     inline bool operator == (const TargetVariableBase & right) const throw()
164     {   return isValid() && right.isValid() && address == right.address;   }
165
166     inline bool operator != (const TargetVariableBase & right) const throw()
167     {   return ! operator ==(right);   }
168
169     inline FileContainer::address_t operator & (void) const throw()
170     {   return isValid() ? address + offset : 0;   }
171
172     inline size_t sizeOf(void) const throw()
173     {   return isValid() ? size : 0;   }
174 };
175
176 template<class T>
177 class TargetVariable : public TargetVariableBase
178 {
179 protected:
180     T    entity;
181
182 public:
183         /*
184          * コンストラクタ (TargetVariableBaseに回送)
185          */
186     inline TargetVariable(FileContainer::address_t addr) throw()
187         : TargetVariableBase(addr, sizeof(T)), entity() 
188     {}
189
190     inline TargetVariable(FileContainer::address_t addr, size_t sz) throw()
191         : TargetVariableBase(addr, sz), entity() 
192     {}
193
194     inline TargetVariable(FileContainer::address_t addr, size_t sz, size_t ofs, size_t ssz) throw()
195         : TargetVariableBase(addr, sz, ofs, ssz), entity() 
196     {}
197
198     inline TargetVariable(const std::string & sym) throw()
199         : TargetVariableBase(sym), entity()
200     {}
201
202     inline TargetVariable(const std::string & sym, const std::string & sz) throw()
203         : TargetVariableBase(sym, sz), entity() 
204     {}
205
206     inline TargetVariable(const TargetVariable<T> & src) throw()
207         : TargetVariableBase(src), entity()
208     {
209         if(isValid() && src.isValid()) {
210             loaded = src.loaded;
211             entity = src.entity;
212         }
213     }
214
215         /* デストラクタ (特に何もしない) */
216     virtual ~TargetVariable(void) throw() 
217     {}
218
219         /* 正当判定に格納に十分なサイズがあるかどうかを追加しておく */
220     inline bool isValid(void) const
221     {   return TargetVariableBase::isValid() && (sizeof(T) >= getSize());   }
222
223         /*
224          * ポインタ風の動作をするオペレータ群
225          */
226     inline TargetVariable<T> offsetInBytes(int offset) const throw()
227     {   return TargetVariable<T>(getAddress() + offset, getSize(), getOffset(), getStructureSize());   }
228
229     inline TargetVariable<T> operator + (int index) const throw()
230     {   return offsetInBytes(index * static_cast<int>(getStructureSize()));   }
231
232     inline TargetVariable<T> operator - (int index) const throw()
233     {   return offsetInBytes(- index * static_cast<int>(getStructureSize()));   }
234
235     inline TargetVariable<T> operator ++ (int) throw()
236     {
237         TargetVariable<T> result(*this);
238         ++ (*this);
239         return result;
240     }
241
242     inline TargetVariable<T> operator -- (int) throw()
243     {
244         TargetVariable<T> result(*this);
245         -- (*this);
246         return result;
247     }
248
249     inline TargetVariable<T> & operator ++ (void) throw()
250     {
251         setAddress(static_cast<int>(getStructureSize()));
252         return *this;
253     }
254
255     inline TargetVariable<T> & operator -- (void) throw()
256     {  
257         setAddress(-static_cast<int>(getStructureSize()));   
258         return *this;
259     }
260
261     inline TargetVariable<T> & operator += (int sz) throw()
262     {   
263         setAddress(static_cast<int>(getStructureSize()) * sz);   
264         return *this;
265     }
266
267     inline TargetVariable<T> & operator -= (int sz) throw()
268     {   
269         setAddress(-static_cast<int>(getStructureSize()) * sz);   
270         return *this;
271     }
272
273     inline const T & operator * (void) throw(Exception)
274     {
275         if(!isLoaded())
276             loadContent(&entity, sizeof(T));
277         return entity;
278     }
279
280     inline T operator [] (int index) const throw(Exception)
281     {   return * TargetVariable<T>(getAddress() + index * getStructureSize(), getSize(), getOffset(), getStructureSize());   }
282 };
283
284 #endif
285