misc.hpp

説明を見る。
00001 /*
00002  *  TOPPERS/FDMP Kernel
00003  *      Toyohashi Open Platform for Embedded Real-Time Systems/
00004  *      Function Distributed Multiprocessor Kernel
00005  *
00006  *  Copyright (C) 2005 by Takagi Nobuhisa
00007  * 
00008  *  上記著作権者は,以下の (1)〜(4) の条件か,Free Software Foundation 
00009  *  によって公表されている GNU General Public License の Version 2 に記
00010  *  述されている条件を満たす場合に限り,本ソフトウェア(本ソフトウェア
00011  *  を改変したものを含む.以下同じ)を使用・複製・改変・再配布(以下,
00012  *  利用と呼ぶ)することを無償で許諾する.
00013  *  (1) 本ソフトウェアをソースコードの形で利用する場合には,上記の著作
00014  *      権表示,この利用条件および下記の無保証規定が,そのままの形でソー
00015  *      スコード中に含まれていること.
00016  *  (2) 本ソフトウェアを,ライブラリ形式など,他のソフトウェア開発に使
00017  *      用できる形で再配布する場合には,再配布に伴うドキュメント(利用
00018  *      者マニュアルなど)に,上記の著作権表示,この利用条件および下記
00019  *      の無保証規定を掲載すること.
00020  *  (3) 本ソフトウェアを,機器に組み込むなど,他のソフトウェア開発に使
00021  *      用できない形で再配布する場合には,次のいずれかの条件を満たすこ
00022  *      と.
00023  *    (a) 再配布に伴うドキュメント(利用者マニュアルなど)に,上記の著
00024  *        作権表示,この利用条件および下記の無保証規定を掲載すること.
00025  *    (b) 再配布の形態を,別に定める方法によって,TOPPERSプロジェクトに
00026  *        報告すること.
00027  *  (4) 本ソフトウェアの利用により直接的または間接的に生じるいかなる損
00028  *      害からも,上記著作権者およびTOPPERSプロジェクトを免責すること.
00029  * 
00030  *  本ソフトウェアは,無保証で提供されているものである.上記著作権者お
00031  *  よびTOPPERSプロジェクトは,本ソフトウェアに関して,その適用可能性も
00032  *  含めて,いかなる保証も行わない.また,本ソフトウェアの利用により直
00033  *  接的または間接的に生じたいかなる損害に関しても,その責任を負わない.
00034  * 
00035  */
00036 
00041 #ifndef TOPPERS_MISC_HPP_
00042 #define TOPPERS_MISC_HPP_
00043 
00044 #include "toppers/config.hpp"
00045 #include <stdexcept>
00046 #include <locale>
00047 #include <iosfwd>
00048 #include <string>
00049 #include <algorithm>
00050 #include <cctype>
00051 #include <cwchar>
00052 #include <cstdlib>
00053 #include <boost/scoped_array.hpp>
00054 
00055 namespace toppers
00056 {
00057 
00062   class conversion_error : public std::runtime_error
00063   {
00064   public:
00069     explicit conversion_error( const std::string& what ) : std::runtime_error( what ) {}
00070   };
00071 
00077   template <typename CharT>
00078   inline CharT widen( char ch )
00079   {
00080     return std::use_facet<std::ctype<CharT> >( std::locale() ).widen( ch );
00081   }
00082 
00083   template <>
00084   inline char widen<char>( char ch )
00085   {
00086     return ch;
00087   }
00088 
00089   template <>
00090   inline unsigned char widen<unsigned char>( char ch )
00091   {
00092     return static_cast<unsigned char>( ch );
00093   }
00094 
00095   template <>
00096   inline wchar_t widen<wchar_t>( char ch )
00097   {
00098 #if defined( __MINGW32__ )
00099     wchar_t wc = wchar_t( -1 );
00100     std::mbtowc( &wc, &ch, 1 ) < 0 )
00101     return wc;
00102 #elif defined( __CYGWIN__ )
00103     return static_cast<wchar_t>( static_cast<unsigned char>( ch ) );
00104 #else
00105     return static_cast<wchar_t>( std::btowc( static_cast<unsigned char>( ch ) ) );
00106 #endif
00107   }
00108 
00114   template <typename CharT>
00115   const std::basic_string<CharT> widen( const std::string& str );
00116 
00117   template <>
00118   inline const std::basic_string<char> widen( const std::string& str )
00119   {
00120     return str;
00121   }
00122 
00123   template <>
00124   inline const std::basic_string<wchar_t> widen( const std::string& str )
00125   {
00126     boost::scoped_array<wchar_t> t( new wchar_t[str.size()+1] );
00127     if ( std::mbstowcs( t.get(), str.c_str(), str.size() ) == size_t( -1 ) )
00128     {
00129       static conversion_error x( "in function widen" );
00130       throw x;
00131     }
00132     return t.get();
00133   }
00134 
00135 #undef  tolower
00136 
00142   inline char tolower( char ch )
00143   {
00144     return static_cast<char>( std::tolower( static_cast<unsigned char>( ch ) ) );
00145   }
00146 
00152   inline const std::string tolower( std::string str )
00153   {
00154     char ( *f )( char ch ) = &tolower;
00155     std::transform( str.begin(), str.end(), str.begin(), f );
00156     return str;
00157   }
00158 
00164   template <class Traits, class Allocator>
00165   const std::basic_string<wchar_t, Traits, Allocator> tolower( std::basic_string<wchar_t, Traits, Allocator> str )
00166   {
00167     std::transform( str.begin(), str.end(), str.begin(), &std::towlower );
00168     return str;
00169   }
00170 
00171 #undef  toupper
00172 
00178   inline char toupper( char ch )
00179   {
00180     return static_cast<char>( std::toupper( static_cast<unsigned char>( ch ) ) );
00181   }
00182 
00188   inline const std::string toupper( std::string str )
00189   {
00190     char ( *f )( char ) = &toupper;
00191     std::transform( str.begin(), str.end(), str.begin(), f );
00192     return str;
00193   }
00194 
00200   template <class Traits, class Allocator>
00201   const std::basic_string<wchar_t, Traits, Allocator> toupper( std::basic_string<wchar_t, Traits, Allocator> str )
00202   {
00203     std::transform( str.begin(), str.end(), str.begin(), &std::towupper );
00204     return str;
00205   }
00206 
00220   template <class InputIterator, typename CharT, class Traits, class Pred>
00221   void output_list( InputIterator first, InputIterator last, std::basic_ostream<CharT, Traits>& ostr, Pred pred, const CharT* delim = 0 )
00222   {
00223     if ( delim == 0 )
00224     {
00225       static const CharT null_delim[] = { 0 };
00226       delim = null_delim;
00227     }
00228 
00229     for ( bool f = false; first != last; ++first )
00230     {
00231       if ( f )
00232       {
00233         ostr << delim;
00234       }
00235       ostr << pred( *first );
00236       f = true;
00237     }
00238   }
00239 
00240 }
00241 
00242 #endif  // ! TOPPERS_MISC_HPP_

Copyright © 2006 by TAKAGI Nobuhisa.
このページは Wed Apr 12 16:31:57 2006 に Doxygen によって生成されました。