kernel_id.cpp

00001 /*
00002  *  TOPPERS/FDMP Kernel
00003  *      Toyohashi Open Platform for Embedded Real-Time Systems/
00004  *      Function Distributed Multiprocessor Kernel
00005  *
00006  *  Copyright (C) 2004 by Witz Corporation, JAPAN  
00007  *  Copyright (C) 2005 by Takagi Nobuhisa
00008  * 
00009  *  上記著作権者は,以下の (1)〜(4) の条件か,Free Software Foundation 
00010  *  によって公表されている GNU General Public License の Version 2 に記
00011  *  述されている条件を満たす場合に限り,本ソフトウェア(本ソフトウェア
00012  *  を改変したものを含む.以下同じ)を使用・複製・改変・再配布(以下,
00013  *  利用と呼ぶ)することを無償で許諾する.
00014  *  (1) 本ソフトウェアをソースコードの形で利用する場合には,上記の著作
00015  *      権表示,この利用条件および下記の無保証規定が,そのままの形でソー
00016  *      スコード中に含まれていること.
00017  *  (2) 本ソフトウェアを,ライブラリ形式など,他のソフトウェア開発に使
00018  *      用できる形で再配布する場合には,再配布に伴うドキュメント(利用
00019  *      者マニュアルなど)に,上記の著作権表示,この利用条件および下記
00020  *      の無保証規定を掲載すること.
00021  *  (3) 本ソフトウェアを,機器に組み込むなど,他のソフトウェア開発に使
00022  *      用できない形で再配布する場合には,次のいずれかの条件を満たすこ
00023  *      と.
00024  *    (a) 再配布に伴うドキュメント(利用者マニュアルなど)に,上記の著
00025  *        作権表示,この利用条件および下記の無保証規定を掲載すること.
00026  *    (b) 再配布の形態を,別に定める方法によって,TOPPERSプロジェクトに
00027  *        報告すること.
00028  *  (4) 本ソフトウェアの利用により直接的または間接的に生じるいかなる損
00029  *      害からも,上記著作権者およびTOPPERSプロジェクトを免責すること.
00030  * 
00031  *  本ソフトウェアは,無保証で提供されているものである.上記著作権者お
00032  *  よびTOPPERSプロジェクトは,本ソフトウェアに関して,その適用可能性も
00033  *  含めて,いかなる保証も行わない.また,本ソフトウェアの利用により直
00034  *  接的または間接的に生じたいかなる損害に関しても,その責任を負わない.
00035  * 
00036  */
00037 
00038 /*
00039  *  toppers/kernel_id.hpp
00040  */
00041 #include "toppers/kernel_id.hpp"
00042 #include "toppers/reporter.hpp"
00043 #include <cctype>
00044 #include <ctime>
00045 #include <cstdio>
00046 #include <algorithm>
00047 #include <fstream>
00048 #include <sstream>
00049 #include <boost/format.hpp>
00050 #include <boost/filesystem/path.hpp>
00051 
00052 namespace toppers
00053 {
00054 
00059   cfg_base::cfg_base( const std::string& file ) : file_( file )
00060   {
00061   }
00062 
00071   void cfg_base::save() const
00072   {
00073     std::ostringstream osstr;
00074     if ( do_save( osstr ) )
00075     {
00076       try
00077       {
00078         std::ofstream ofstr( file_.c_str() );
00079         if ( ofstr.fail() )
00080         {
00081           rout.error( _( "cannot create file `%1%\'" ) % file_ );
00082         }
00083         ofstr << osstr.str() << std::endl;
00084         ofstr.close();
00085         do_depend();
00086       }
00087       catch ( ... )
00088       {
00089         std::remove( file_.c_str() );
00090         throw;
00091       }
00092     }
00093   }
00094 
00099   const std::string cfg_base::file() const
00100   {
00101     boost::filesystem::path path( file_, boost::filesystem::native );
00102     return path.leaf();
00103   }
00104 
00109   void cfg_base::do_file_header( std::ostream& ostr ) const
00110   {
00111     std::time_t t = std::time( 0 );
00112     ostr << boost::format( "/*\n"
00113                            " *  %1%\n"
00114                            " *  %2%"  // ctimeが返す文字列は改行文字付きなので'\n'を省略する
00115                            " */\n" ) % file() % std::ctime( &t );
00116   }
00117 
00124   void cfg_base::do_depend() const
00125   {
00126   }
00127 
00131   kernel_id::id_number::id_number() : id_( -1L )
00132   {
00133   }
00134 
00139   const std::string& kernel_id::id_number::name() const
00140   {
00141     return name_;
00142   }
00143 
00149   long kernel_id::id_number::value() const
00150   {
00151     return id_;
00152   }
00153 
00159   void kernel_id::id_number::set( const std::string& name, long id )
00160   {
00161     name_ = name;
00162     id_ = id;
00163   }
00164 
00174   bool kernel_id::id_number::less_than( const boost::shared_ptr<id_number> lhs, const boost::shared_ptr<id_number> rhs )
00175   {
00176     return lhs->id_ < rhs->id_;
00177   }
00178 
00183   kernel_id::kernel_id( const std::string& file ) : cfg_base( file )
00184   {
00185   }
00186 
00192   const kernel_id::id_array* kernel_id::get_id_array( const std::string& key ) const
00193   {
00194     std::map<std::string, id_array>::const_iterator iter( id_map_.find( key ) );
00195     if ( iter == id_map_.end() )
00196     {
00197       return 0;
00198     }
00199     return &iter->second;
00200   }
00201 
00207   void kernel_id::insert_id( const std::string& key, const boost::shared_ptr<id_number>& value )
00208   {
00209     if ( value )
00210     {
00211       id_map_[key].push_back( value );
00212     }
00213   }
00214 
00219   void kernel_id::clear_id_array( const std::string& key )
00220   {
00221     id_map_[key].clear();
00222   }
00223 
00224   bool kernel_id::do_save( std::ostream& ostr ) const
00225   {
00226     do_file_header( ostr );
00227 
00228     const std::string macro( include_guard_macro( file() ) );
00229     ostr << boost::format( "#ifndef %1%\n"
00230                            "#define %1%\n" ) % macro;
00231     ostr << '\n';
00232     do_body( ostr );
00233     ostr << boost::format( "#endif\t/* ! %1% */\n" ) % macro;
00234     return true;
00235   }
00236 
00260   const std::string kernel_id::include_guard_macro( const std::string& file )
00261   {
00262     std::stringstream sstr;
00263     const std::string::const_iterator last( file.end() );
00264     for ( std::string::const_iterator iter( file.begin() ); iter != last; ++iter )
00265     {
00266       int ch = static_cast<unsigned char>( *iter );
00267       if ( std::isalnum( ch ) )
00268       {
00269         sstr << static_cast<char>( std::toupper( ch ) );
00270       }
00271       else if ( std::ispunct( ch ) || ch == ' ' )
00272       {
00273         sstr << '_';
00274       }
00275       else
00276       {
00277         sstr << 'x' << std::hex << ch;
00278       }
00279     }
00280     return sstr.str();
00281   }
00282 
00283 }

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