CrossFramework Library

XBase/FixedString.hpp

説明を見る。
00001 
00006 #if defined(XBASE_INCLUDED_FIXEDSTRING_HPP)
00007 #else
00008 #define XBASE_INCLUDED_FIXEDSTRING_HPP
00009 
00010 //------------------------------------------------------------
00011 #include <cstdio>
00012 #include <XBase/BuiltInTypes.hpp>
00013 #include <XBase/StaticAssert.hpp>
00014 #include <XBase/StringTraits.hpp>
00015 #include <XBase/TypeTraits.hpp>
00016 
00017 //------------------------------------------------------------
00018 namespace XBase {
00020 
00021 
00028     template< typename CHAR_TYPE , size_t BUFFER_LENGTH >
00029     struct FixedStringPOD
00030     {
00032         typedef CHAR_TYPE CharType;
00033 
00035         typedef FixedStringPOD< CHAR_TYPE , BUFFER_LENGTH > PODType;
00036 
00038         static const size_t BufferLength = BUFFER_LENGTH;
00039      
00047         static const PODType FromFormat( const CharType* aFormat , ... )
00048         {
00049             PODType str;
00050             va_list arg;
00051             va_start( arg, aFormat );
00052             StringTraits< CharType >::VSNPrintf( str.buffer_ , BufferLength , aFormat , arg );
00053             va_end(arg);
00054             return str;
00055         }        
00056         
00061         const CharType* readPtr()const { return buffer_; }
00062         CharType*       ptr()          { return buffer_; } 
00063         const CharType* ptr()const     { return buffer_; } 
00064 
00071         void set( const CharType* aStr )
00072         {
00073             StringTraits< CharType >::NCopy( buffer_ , BufferLength , aStr );
00074         }
00075 
00080         size_t length()const 
00081         {
00082             return StringTraits< CharType >::Length( buffer_ );
00083         }
00084         
00085         // バッファ。直接アクセスすることは想定していない。
00086         CharType buffer_[ BufferLength ];
00087     };
00088 
00090     template< typename CHAR_TYPE , size_t BUFFER_LENGTH >
00091     class FixedString : public FixedStringPOD< CHAR_TYPE , BUFFER_LENGTH >
00092     {
00093     public:
00095         FixedString()
00096           : SuperType() 
00097         {
00098             SuperType::buffer_[0] = '\0'; 
00099         }
00100 
00102         FixedString( const CHAR_TYPE* aStr ) 
00103             : SuperType() 
00104         {
00105             set( aStr );
00106         }
00107 
00109         FixedString( 
00110             const FixedStringPOD< CHAR_TYPE , BUFFER_LENGTH >& aObj 
00111             )
00112             : SuperType::PODType()
00113         {
00114             static_cast< SuperType& >( *this ) = aObj;
00115         }
00116 
00117     private:
00118         typedef FixedStringPOD< CHAR_TYPE , BUFFER_LENGTH > SuperType;
00119         XBASE_STATIC_ASSERT( TypeTraits::IsPOD< PODType >::Value );
00120     };
00122 }
00123 //------------------------------------------------------------
00124 #endif
00125 // EOF
 全て クラス ネームスペース ファイル 関数 変数 型定義 列挙型 列挙型の値 マクロ定義