メインページ | ネームスペース一覧 | クラス階層 | 構成 | ファイル一覧 | ネームスペースメンバ | 構成メンバ | ファイルメンバ | 関連ページ

string.h

説明を見る。
00001 #ifndef STATIC_CONTAINER_STRING_H
00002 
00003 #define STATIC_CONTAINER_STRING_H
00004 
00005 /*
00006 zlib/libpng license
00007 -------------------
00008 
00009 Copyright (C) 2004 &o
00010 
00011 This software is provided 'as-is', without any express or implied warranty. In n
00012 o event will the authors be held liable for any damages arising from the use of 
00013 this software.
00014 
00015 Permission is granted to anyone to use this software for any purpose, including 
00016 commercial applications, and to alter it and redistribute it freely, subject to 
00017 the following restrictions:
00018 
00019 The origin of this software must not be misrepresented; you must not claim that 
00020 you wrote the original software. If you use this software in a product, an ackno
00021 wledgment in the product documentation would be appreciated but is not required.
00022 
00023 Altered source versions must be plainly marked as such, and must not be misrepre
00024 sented as being the original software.
00025 This notice may not be removed or altered from any source distribution.
00026 
00027 project site : https://sourceforge.jp/projects/gslib/
00028 my site : http://www.game-syokunin.com/
00029 --------------------------------------------------------------------------------
00030 
00031 法的には、上記の原文のほうが有効なので、より厳密には日本語訳よりも原文を参考にし
00032 てください。日本語訳は、http://opensource.jp/licenses/zlib-license.html から頂い
00033 てきました。
00034 
00035 zlib/libpngライセンス ( 日本語訳 )
00036 
00037 Copyright (C) 2004 &o
00038 
00039 本ソフトウェアは「現状のまま」で、明示であるか暗黙であるかを問わず、何らの保証も
00040 なく提供されます。本ソフトウェアの使用によって生じるいかなる損害についても、作者
00041 は一切の責任を負わないものとします。 以下の制限に従う限り、商用アプリケーション
00042 を含めて、本ソフトウェアを任意の目的に使用し、自由に改変して再頒布することをすべ
00043 ての人に許可します。
00044 
00045 本ソフトウェアの出自について虚偽の表示をしてはなりません。あなたがオリジナルのソ
00046 フトウェアを作成したと主張してはなりません。あなたが本ソフトウェアを製品内で使用
00047 する場合、製品の文書に謝辞をれていただければ幸いですが、必須ではありません。
00048 ソースを変更した場合は、そのことを明示しなければなりません。オリジナルのソフトウ
00049 ェアであるという虚偽の表示をしてはなりません。
00050 ソースの頒布物から、この表示を削除したり、表示の内容を変更したりしてはなりません
00051 
00052 
00053 project site : https://sourceforge.jp/projects/gslib/
00054 my site : http://www.game-syokunin.com/
00055 */
00056 
00057 #include <iosfwd>
00058 #include <boost/assert.hpp>
00059 #include <gslib/static_container/STATIC_CONTAINER_MEMBERTYPEDEF.h>
00060 #include <gslib/numeric.h>
00061 #include <algorithm>
00062 
00063 namespace gslib {
00064     namespace static_container {
00066 
00072         template< size_t MaxStrLen, typename Ch = char, typename ChTraits = std::char_traits< Ch > >
00073         class string {
00074         public:
00075             STATIC_CONTAINER_MEMBERTYPEDEF( Ch )
00076             typedef Ch              char_type;
00077             typedef ChTraits        traits_type;
00078             typedef pointer         iterator;
00079             typedef const_pointer   const_iterator;
00080         private:
00081             Ch  buffer_[ MaxStrLen + 1 ];
00082         public:
00084             BOOST_STATIC_CONSTANT( size_type, npos = -1 );
00085             BOOST_STATIC_CONSTANT( size_type, const_max = MaxStrLen );
00086 
00088             static size_type max_size() { return const_max; }
00089 
00091             static size_type capaciry() { return const_max; }
00092         
00094             size_type size() const { return traits_type::length( buffer_ ); }
00095 
00097             size_type length() const { return size(); }
00098 
00099             iterator begin() { return buffer_; }
00100             const_iterator begin() const { return buffer_; }
00101             iterator end() { return begin() + size(); }
00102             const_iterator end() const { return begin() + size(); }
00103 
00104             char_type& operator [] ( size_type i ) {
00105                 PYD_ASSERT( i < size() );
00106                 return *( begin() + i );
00107             }
00108             const char_type& operator [] ( size_type i ) const {
00109                 PYD_ASSERT( i < size() );
00110                 return *( begin() + i );
00111             }
00112             bool empty() const {
00113                 return 0 == size();
00114             }
00115             reference at( size_type i ) { return operator [] ( i ); }
00116             char_type at( size_type i ) const { return operator [] ( i ); }
00117 
00118             reference front() { return *begin(); }
00119             char_type front() const { return *begin(); }
00120             reference back() { return *( end() - 1 ); }
00121             char_type back() const { return *( end() - 1 ); }
00122         
00124             void push_back( char_type ch ) {
00125                 //BOOST_ASSERT;
00126                 if ( size() < MaxStrLen ) {
00127                     iterator last = end();
00128                     *last = ch;
00129                     ++last;
00130                     *last = char_type();
00131                 }
00132             }
00133 
00135             void append( char_type ch ) {
00136                 push_back( ch );
00137             }
00138 
00140             void append( const char* str ) {
00141                 if ( 0 == str ) {
00142                     return;
00143                 }
00144                 if ( size() + traits_type::length( str ) <= max_size() ) {
00145                     traits_type::copy( end(), str, traits_type::length( str ) + 1 );
00146                 } else {
00147                     traits_type::copy( end(), str, max_size() );
00148                     buffer_[ MaxStrLen ] = char_type();
00149                 }
00150             }
00151 
00153             template < size_type OtherMaxStrLen >
00154             void append( const string< OtherMaxStrLen, Ch, ChTraits >& other ) {
00155                 append( other.c_str() );
00156             }
00157 
00159             void pop_back() {
00160                 BOOST_ASSERT( false == empty() );
00161                 iterator last = end();
00162                 --last;
00163                 *last = char_type();
00164             }
00165 
00167             string() {
00168                 clear();
00169             }
00170             
00172             template < size_type OtherMaxStrLen >
00173             string( const string< OtherMaxStrLen, Ch, ChTraits >& other ) {
00174                 if ( buffer_ != other.begin() ) {
00175                     size_type otherLen = other.size();
00176                     if ( otherLen <= max_size() ) {
00177                         traits_type::copy( buffer_, other.begin(), otherLen + 1 );
00178                     } else {
00179                         traits_type::copy( buffer_, other.begin(), max_size() + 1 );
00180                     }
00181                 }
00182             }
00183 
00184             string( const char_type* s ) {
00185                 if ( s != buffer_ ) {
00186                     size_type otherLen = traits_type::length( s );
00187                     if ( otherLen <= max_size() ) {
00188                         traits_type::copy( buffer_, s, otherLen + 1 );
00189                     } else {
00190                         traits_type::copy( buffer_, s, max_size() + 1 );
00191                     }
00192                 }
00193             }
00194 
00195             // デストラクタ
00196             ~string() {
00197             }
00198 
00200             string& operator = ( const string& other )  {
00201                 return operator = ( other.c_str() );
00202             }
00203             string& operator = ( const char_type* s ) {
00204                 if ( s != buffer_ ) {
00205                     clear();
00206                     append( s );
00207                 }
00208                 return *this;
00209             }
00210             
00212             pointer data() { return begin(); }
00213             const_pointer data() const { return begin(); }
00214             const_pointer c_str() const { return begin(); }
00215 
00217             string operator += ( const char* other ) {
00218                 append( other );
00219                 return *this;
00220             }
00221 
00223             template < size_type OtherMaxStrLen >
00224             string operator += ( const string< OtherMaxStrLen, Ch, ChTraits >& other ) {
00225                 append( other );
00226                 return *this;
00227             }
00228             
00230             void clear() {
00231                 buffer_[ 0 ] = char_type();
00232             }
00233 
00235             template < size_type OtherMaxStrLen >
00236             friend bool operator == ( const string& a, const string< OtherMaxStrLen, Ch, ChTraits >& b ) {
00237                 return operator == ( a, b.c_str() );
00238             }
00239 
00241             friend bool operator == ( const string& a, const char* b ) {
00242                 size_type size = a.size();
00243                 return char_type() == b[ size ] &&
00244                     std::equal( a.begin(), a.begin() + size, b, traits_type::eq );
00245             }
00246 
00248             friend bool operator == ( const char* a, const string& b ) {
00249                 return operator == ( b, a );
00250             }
00251             
00253             template < size_type OtherMaxStrLen >
00254             friend bool operator != ( const string& a, const string< OtherMaxStrLen, Ch, ChTraits >& b ) {
00255                 return !operator == ( a, b );
00256             }
00257 
00259             friend bool operator != ( const string& a, const char* b ) {
00260                 return !operator == ( a, b );
00261             }
00262 
00264             friend bool operator != ( const char* a, const string& b ) {
00265                 return !operator == ( a, b );
00266             }
00267 
00269             template < size_type OtherMaxStrLen >
00270             friend bool operator < ( const string& a, const string< OtherMaxStrLen, Ch, ChTraits >& b ) {
00271                 return operator < ( a, b.c_str() );
00272             }
00273 
00275             friend bool operator < ( const string& a, const char* b ) {
00276                 return std::lexicographical_compare(
00277                     a.begin(), a.end(),
00278                     b, b + traits_type::length( b ),
00279                     traits_type::eq_int_type );
00280             }
00281 
00283             friend bool operator < ( const char* a, const string& b ) {
00284                 return operator < ( b, a );
00285             }
00286         };
00287     }
00288 }
00289 
00290 #endif

static_containerに対してSat Nov 27 15:03:13 2004に生成されました。 doxygen 1.3.6