CrossFramework Library
|
00001 00006 #if defined(XDATA_INCLUDED_XDATA_HPP) 00007 #else 00008 #define XDATA_INCLUDED_XDATA_HPP 00009 00010 //------------------------------------------------------------ 00011 00016 #if defined(XDATA_OPTION_IS_BIG_ENDIAN) 00017 #else 00018 00019 #if defined(__BIG_ENDIAN__) 00020 #define XDATA_OPTION_IS_BIG_ENDIAN 00021 #endif 00022 00023 #endif 00024 00025 #if defined(XDATA_OPTION_IS_BIG_ENDIAN) 00026 #define XDATA_IS_BIG_ENDIAN 00027 #else 00028 #define XDATA_IS_LITTLE_ENDIAN 00029 #endif 00030 00031 00032 //------------------------------------------------------------ 00034 namespace XData { 00036 00037 00038 // TypeDef 00039 typedef signed char SInt8; 00040 typedef signed short SInt16; 00041 typedef signed int SInt32; 00042 typedef unsigned char UInt8; 00043 typedef unsigned short UInt16; 00044 typedef unsigned int UInt32; 00045 typedef float Float32; 00046 typedef double Float64; 00047 typedef UInt32 Reference; 00048 00050 struct String 00051 { 00053 UInt32 byteLength; 00054 00059 const void* ptr()const 00060 { 00061 return &(&byteLength)[1]; 00062 } 00063 00068 const char* toCStr()const 00069 { 00070 return reinterpret_cast< const char* >( ptr() ); 00071 } 00072 }; 00073 00074 // 定数群。 00075 class Constant 00076 { 00077 public: 00078 // XBIN(Xdata BINary)を示す4文字。 00079 static const UInt32 SIGNATURE = 00080 #if defined(XDATA_IS_BIG_ENDIAN) 00081 0x5842494E; 00082 #else 00083 0x4E494258; 00084 #endif 00085 // エンディアンを示す値。 00086 static const UInt16 ENDIAN = 0x1234; 00087 // メジャーバージョン 00088 static const UInt8 VERSION_MAJOR = 2; 00089 // マイナーバージョン 00090 static const UInt8 VERSION_MINOR = 0; 00091 }; 00092 00093 // XDataのヘッダ。 00094 struct XDataHeader 00095 { 00096 UInt32 signature; // シグネチャ。Constant::SIGNATUREと等しい。 00097 UInt16 endian; // エンディアンチェッカー。Constant::ENDIANと等しい。 00098 UInt8 versionMajor; // メジャーバージョンを示す値。VERSION_MAJORに等しい。 00099 UInt8 versionMinor; // マイナーバージョンを示す値。VERSION_MINOR以下。 00100 UInt32 datasize; // ヘッダを含むデータサイズ。バイト数。 00101 UInt32 pageCode; // 文字列のページコード。.netframeworkのページコード番号が入る。 00102 }; 00103 00105 class XData 00106 { 00107 public: 00109 XData() : mPtr(0){} 00111 XData( const void* aPtr ) 00112 : mPtr( static_cast< const XDataHeader* >( aPtr ) ) 00113 { 00114 } 00115 00120 const void* ptr()const 00121 { 00122 return mPtr; 00123 } 00124 00132 bool isValidData()const 00133 { 00134 return mPtr != 0 00135 && mPtr->signature == Constant::SIGNATURE 00136 && mPtr->endian == Constant::ENDIAN 00137 && mPtr->versionMajor == Constant::VERSION_MAJOR 00138 && mPtr->versionMinor <= Constant::VERSION_MINOR 00139 && sizeof(XDataHeader) <= mPtr->datasize 00140 ; 00141 }; 00142 00147 const void* dataHeadAddress()const 00148 { 00149 if ( !isValidData() ) 00150 {// 無効なデータなら0を。 00151 return 0; 00152 } 00153 return &mPtr[1]; 00154 } 00155 00160 const void* labelAddressWithReference( const Reference aReferenceValue )const 00161 { 00162 if ( !isValidData() ) 00163 {// 無効なデータならNULLポインタを。 00164 return 0; 00165 } 00166 if ( aReferenceValue < sizeof(XDataHeader) // ヘッダの中を指している 00167 || mPtr->datasize < aReferenceValue // データの外を指している 00168 ) 00169 {// 範囲外 00170 return 0; 00171 } 00172 return &reinterpret_cast< const UInt8* >( mPtr )[ aReferenceValue ]; 00173 } 00174 00175 private: 00176 const XDataHeader* mPtr; 00177 }; 00178 00180 } 00181 //------------------------------------------------------------ 00182 #endif 00183 // EOF