メインページ   モジュール   名前空間一覧   クラス階層   アルファベット順一覧   構成   ファイル一覧   構成メンバ   ファイルメンバ   関連ページ    

TDataSection.cc

解説を見る。
00001 // =====================================================================
00002 //  $Id: TDataSection.cc,v 1.8 2004/03/07 10:30:30 goiwai Exp $
00003 //  $Name: CLDAQ-1-14-03 $
00004 //  $Log: TDataSection.cc,v $
00005 //  Revision 1.8  2004/03/07 10:30:30  goiwai
00006 //  ROOTに組みこむためのおまじないマクロを埋めこみました。
00007 //  全てにおいて完全に動作するわけではありません。
00008 //
00009 //  Revision 1.7  2003/12/06 10:59:11  goiwai
00010 //  Serialize(Tvoid*) -> Serialize(const Tvoid*)に変更しました.
00011 //  Deserialize(const Tvoid*)を追加しました.
00012 //  手抜きの実装なので,バッファオーバーフローの可能性があります.
00013 //  Reallocate()とかするような仕組みが必要です.
00014 //
00015 //  Revision 1.6  2003/11/04 12:03:05  goiwai
00016 //  ==演算子と!=演算子を実装しました.具体的には
00017 //  if ( record == "String" ) {
00018 //    hoeghoge;
00019 //  }
00020 //  のような使い方が出来ます.単純に theID メンバとを比較して結果を返します.
00021 //
00022 //  Revision 1.5  2003/10/06 16:42:19  goiwai
00023 //  *** empty log message ***
00024 //
00025 //  Revision 1.4  2003/08/25 09:20:29  goiwai
00026 //  operator[]( const Tstring& id ) を加えました.
00027 //  record[0] とか record["tag"] とかすれば, TDataSegment を得ることが出来
00028 //  ます.
00029 //  size()を越えた範囲にも,チェックしないでアクセスします.
00030 //  マッチしなかった場合は落ちます.
00031 //  同じ名前でタグされた DataSegment がある場合,最初にマッチしたものが返っ
00032 //  てきます.
00033 //
00034 //  Revision 1.3  2003/07/30 16:17:51  goiwai
00035 //  ファイルにコミットログをつけることにしました.
00036 //
00037 // =====================================================================
00038 #include "TDataSection.hh"
00039 #include "TOutputObjectStream.hh"
00040 #include "TOutputObjectFile.hh"
00041 #include "TOutputObjectSocket.hh"
00042 #include "TOutputObjectSharedMemory.hh"
00043 
00044 
00045 TDataSection::TDataSection( const Tstring& id )
00046   : TStreamableObject( tObjectDataSection, id ), TDataSegmentList()
00047 {;}
00048 
00049 TDataSection::TDataSection( const TDataSection& right )
00050   : TStreamableObject( right ), TDataSegmentList( right )
00051 {;}
00052 
00053 TDataSection::~TDataSection()
00054 {;}
00055 
00056 Tint TDataSection::GetRecordSize()
00057 {
00058   Tsize_t nchar = theID.size() + 1;
00059   // record size + object type + object id + number of entries
00060   Tint total = Tsizeof( Tint ) + Tsizeof( Tobject_t ) + Tsizeof( Tsize_t ) + ( Tsizeof( Tchar ) * nchar ) + Tsizeof( Tsize_t );
00061   for ( Tsize_t i = 0; i < size(); i ++ ) {
00062     total += ( (*this)[ i ] ).GetRecordSize();
00063   }
00064   return total;
00065 }
00066 
00067 Tint TDataSection::Record( TOutputObjectStream* output )
00068 {
00069   Tint datasize = 0;
00070   Tstream_t streamtype = output -> GetStreamType();
00071 
00072   switch ( streamtype ) {
00073     case tFileStream:
00074       datasize = record( (TOutputObjectFile*)output );
00075       break;
00076     case tSocketStream:
00077       datasize = record( (TOutputObjectSocket*)output );
00078       break;
00079     case tSharedMemoryStream:
00080       datasize = record( (TOutputObjectSharedMemory*)output );
00081       break;
00082     case tUnknownStream:
00083     default:
00084       Tcerr << "TDataSection::Record: unknown stream type." << Tendl;
00085       return 0;
00086   }
00087 
00088   for ( Tint i = 0; i < (Tint)size(); i ++ )
00089     datasize += ( (*this)[ i ] ).Record( output );
00090 
00091   return datasize;
00092 }
00093 
00094 Tostream& operator<<( Tostream& tos, const TDataSection& right )
00095 {
00096   tos << "Data Section(" << right.theObjectType << "), "
00097       << "ID: " << right.theID
00098       << ", Capacity: " << right.capacity()
00099       << ", Entry: " << right.size();
00100 
00101   if ( right.empty() ) {
00102     return tos;
00103   }
00104   
00105   tos << Tendl;
00106 
00107   for ( Tsize_t i = 0; i < right.size(); i ++ ) {
00108     tos << Ttab << Ttab << "[" << i << "] " << right[ i ];
00109     if ( i != right.size() - 1 ) {
00110       tos << Tendl;
00111     }
00112   }
00113 
00114   return tos;
00115 }
00116 
00117 Tint TDataSection::record( TOutputObjectFile* ofile )
00118 {
00119   Tsize_t datasize = 0;
00120   static const Tsize_t nmemb = 1;
00121   Tsize_t ss = 0;
00122   static const Tstring head = "TDataSection::record";
00123 
00124 
00125   // write record size
00126   Tint recsize = GetRecordSize();
00127   ss = fwrite( &recsize, Tsizeof( Tint ), nmemb, ofile -> GetFileStream() );
00128   if ( ss != nmemb ) {
00129     perror( head.c_str() );
00130   } else {
00131     datasize += ss * Tsizeof( Tint );
00132   }
00133 
00134 
00135   // write object type
00136   ss = fwrite( &theObjectType, Tsizeof( Tobject_t ), nmemb, ofile -> GetFileStream() );
00137   if ( ss != nmemb ) {
00138     perror( head.c_str() );
00139   } else {
00140     datasize += ss * Tsizeof( Tobject_t );
00141   }
00142 
00143 
00144   // write object ID
00145   Tchar charbuf;
00146   Tsize_t nchar = theID.size() + 1;
00147   ss = fwrite( &nchar, Tsizeof( Tsize_t ), nmemb, ofile -> GetFileStream() );
00148   if ( ss != nmemb ) {
00149     perror( head.c_str() );
00150   } else {
00151     datasize += ss * Tsizeof( Tsize_t );
00152   }
00153   for ( Tsize_t i = 0; i < nchar; i ++ ) {
00154     if ( i == ( nchar - 1 ) ) {
00155       charbuf = '\0';
00156     } else {
00157       charbuf = theID[ i ];
00158     }
00159     ss = fwrite( &charbuf, Tsizeof( Tchar ), nmemb, ofile -> GetFileStream() );
00160     if ( ss != nmemb ) {
00161       perror( head.c_str() );
00162     } else {
00163       datasize += ss * Tsizeof( Tchar );
00164     }
00165   }
00166 
00167 
00168   // write number of entries
00169   Tsize_t entry = size();
00170   ss = fwrite( &entry, Tsizeof( Tsize_t ), nmemb, ofile -> GetFileStream() );
00171   if ( ss != nmemb ) {
00172     perror( head.c_str() );
00173   } else {
00174     datasize += ss * Tsizeof( Tsize_t );
00175   }
00176 
00177 
00178   return (Tint)datasize;
00179 }
00180 
00181 Tint TDataSection::record( TOutputObjectSocket* osocket )
00182 {
00183   Tsize_t datasize = 0;
00184   static const Tstring head = "TDataSection::record";
00185 
00186 
00187   // send record size
00188   Tint recsize = GetRecordSize();
00189   if ( send( osocket -> GetServerDescriptor(), &recsize, Tsizeof( Tint ), 0 ) != Tsizeof( Tint ) ) {
00190     perror( head.c_str() );
00191   } else {
00192     datasize += Tsizeof( Tint );
00193   }
00194 
00195 
00196   // send object type
00197   if ( send( osocket -> GetServerDescriptor(), &theObjectType, Tsizeof( Tobject_t ), 0 ) != Tsizeof( Tobject_t ) ) {
00198     perror( head.c_str() );
00199   } else {
00200     datasize += Tsizeof( Tobject_t );
00201   }
00202 
00203 
00204   // send object ID
00205   Tchar charbuf;
00206   Tsize_t nchar = theID.size() + 1;
00207   if ( send( osocket -> GetServerDescriptor(), &nchar, Tsizeof( Tsize_t ), 0 ) != Tsizeof( Tsize_t ) ) {
00208     perror( head.c_str() );
00209   } else {
00210     datasize += Tsizeof( Tsize_t );
00211   }
00212   for ( Tsize_t i = 0; i < nchar; i ++ ) {
00213     if ( i == ( nchar - 1 ) ) {
00214       charbuf = '\0';
00215     } else {
00216       charbuf = theID[ i ];
00217     }
00218     if ( send( osocket -> GetServerDescriptor(), &charbuf, Tsizeof( Tchar ), 0 ) != Tsizeof( Tchar ) ) {
00219       perror( head.c_str() );
00220     } else {
00221       datasize += Tsizeof( Tchar );
00222     }
00223   }
00224 
00225 
00226   // send number of entries
00227   Tsize_t entry = size();
00228   if ( send( osocket -> GetServerDescriptor(), &entry, Tsizeof( Tsize_t ), 0 ) != Tsizeof( Tsize_t ) ) {
00229     perror( head.c_str() );
00230   } else {
00231     datasize += Tsizeof( Tsize_t );
00232   }
00233 
00234 
00235   return (Tint)datasize;
00236 }
00237 
00238 Tint TDataSection::record( TOutputObjectSharedMemory* omemory )
00239 {
00240   Tsize_t datasize = 0;
00241   Tvoid* ptr = omemory -> GetAddress();
00242 
00243   // write record size
00244   *( (Tint*)ptr ) = GetRecordSize();
00245   datasize += Tsizeof( Tint );
00246   ( (Tint*)ptr ) ++;
00247 
00248 
00249   // write object type
00250   *( (Tobject_t*)ptr ) = theObjectType;
00251   datasize += Tsizeof( Tobject_t );
00252   ( (Tobject_t*)ptr ) ++;
00253 
00254 
00255   // write object ID
00256   Tchar charbuf;
00257   Tsize_t nchar = theID.size() + 1;
00258   *( (Tsize_t*)ptr ) = nchar;
00259   datasize += Tsizeof( Tsize_t );
00260   ( (Tsize_t*)ptr ) ++;
00261   for ( Tsize_t i = 0; i < nchar; i ++ ) {
00262     if ( i == ( nchar - 1 ) ) {
00263       charbuf = '\0';
00264     } else {
00265       charbuf = theID[ i ];
00266     }
00267     *( (Tchar*)ptr ) = charbuf;
00268     datasize += Tsizeof( Tchar );
00269     ( (Tchar*)ptr ) ++;
00270   }
00271 
00272 
00273   // write number of entries
00274   Tsize_t entry = size();
00275   *( (Tsize_t*)ptr ) = entry;
00276   datasize += Tsizeof( Tsize_t );
00277   ( (Tsize_t*)ptr ) ++;
00278 
00279 
00280   omemory -> SetAddress( ptr );
00281 
00282   return (Tint)datasize;
00283 }
00284 
00285 const TDataSection& TDataSection::operator=( const TDataSection& right )
00286 {
00287   *( (TStreamableObject*)this ) = *( (TStreamableObject*)(&right) );
00288   *( (TDataSegmentList*)this ) = *( (TDataSegmentList*)(&right) );
00289 
00290   return *this;
00291 }
00292 
00293 Tbool TDataSection::operator==( const TDataSection& right ) const
00294 {
00295   Tbool ret = Ttrue;
00296   ret &= ( *((TStreamableObject*)this) == *((TStreamableObject*)(&right)) );
00297   ret &= ( *( (TDataSegmentList*)this ) == *( (TDataSegmentList*)(&right) ) );
00298 
00299   return ret;
00300 }
00301 
00302 Tbool TDataSection::operator!=( const TDataSection& right ) const
00303 {
00304   Tbool ret = Tfalse;
00305   ret |= ( *((TStreamableObject*)this) != *((TStreamableObject*)(&right)) );
00306   ret |= ( *((TDataSegmentList*)this) != *((TDataSegmentList*)(&right)) );
00307   return ret;
00308 }
00309 
00310 Tbool TDataSection::operator==( const Tstring& right ) const
00311 {
00312   if ( theID == right ) {
00313     return Ttrue;
00314   } else {
00315     return Tfalse;
00316   }
00317 }
00318 
00319 Tbool TDataSection::operator!=( const Tstring& right ) const
00320 {
00321   if ( theID != right ) {
00322     return Ttrue;
00323   } else {
00324     return Tfalse;
00325   }
00326 }
00327 
00328 Tvoid TDataSection::Clear()
00329 {
00330   theID = TunknownID;
00331   for ( Tsize_t i = 0; i < size(); i ++ )
00332     ( (*this)[ i ] ).Clear();
00333   clear();
00334   return;
00335 }
00336 
00337 Tbool TDataSection::FindDataSegment( const Tstring& id, TDataSegment& segment ) const
00338 {
00339   for ( Tsize_t i = 0; i < size(); i ++ ) {
00340     if ( ( (*this)[ i ] ).GetID() == id ) {
00341       segment = (*this)[ i ];
00342       return Ttrue;
00343     }
00344   }
00345   return Tfalse;
00346 }
00347 
00348 Tint TDataSection::FindDataSegment( const Tstring& id ) const
00349 {
00350   Tint retval = -EFAULT;
00351   for ( Tsize_t i = 0; i < size(); i ++ ) {
00352     if ( ( (*this)[ i ] ).GetID() == id ) {
00353       retval = i;
00354       break;
00355     }
00356   }
00357   return retval;
00358 }
00359 
00360 Tbool TDataSection::FindDataElement( const Tstring& segid, const Tstring& eleid, TDataElement& element ) const
00361 {
00362   TDataSegment segment;
00363   if ( FindDataSegment( segid, segment ) ) {
00364     return segment.FindDataElement( eleid, element );
00365   } else {
00366     return Tfalse;
00367   }
00368 }
00369 
00370 Tint TDataSection::FindDataElement( const Tstring& segid, const Tstring& eleid ) const
00371 {
00372   Tint segpos = FindDataSegment( segid );
00373   if ( segpos >= 0 ) {
00374     return ( (*this)[ segpos ] ).FindDataElement( eleid );
00375   } else {
00376     return segpos;
00377   }
00378 }
00379 
00380 Tbool TDataSection::FindDataElement( Tstring idset[ 2 ], TDataElement& element ) const
00381 {
00382   return FindDataElement( idset[ 0 ], idset[ 1 ], element );
00383 }
00384 
00385 Tint TDataSection::FindDataElement( Tstring idset[ 2 ] ) const
00386 {
00387   return FindDataElement( idset[ 0 ], idset[ 1 ] );
00388 }
00389 
00390 Tbool TDataSection::FindDataElement( const TstringList& idset, TDataElement& element ) const
00391 {
00392   return FindDataElement( idset[ 0 ], idset[ 1 ], element );
00393 }
00394 
00395 Tint TDataSection::FindDataElement( const TstringList& idset ) const
00396 {
00397   return FindDataElement( idset[ 0 ], idset[ 1 ] );
00398 }
00399 
00400 Tint TDataSection::Serialize( const Tvoid* buffer )
00401 {
00402   Tvoid* p = const_cast<Tvoid*>(buffer);
00403   Tsize_t datasize = 0;
00404 
00405   // write record size
00406   *( (Tint*)p ) = GetRecordSize();
00407   ( (Tint*)p ) ++;
00408   datasize += Tsizeof( Tint );
00409 
00410 
00411   // write object type
00412   *( (Tobject_t*)p ) = theObjectType;
00413   ( (Tobject_t*)p ) ++;
00414   datasize += Tsizeof( Tobject_t );
00415 
00416 
00417   // write object ID
00418   Tchar charbuf;
00419   Tsize_t nchar = theID.size() + 1;
00420   *( (Tsize_t*)p ) = nchar;
00421   ( (Tsize_t*)p ) ++;
00422   datasize += Tsizeof( Tsize_t );
00423   for ( Tsize_t i = 0; i < nchar; i ++ ) {
00424     if ( i == ( nchar - 1 ) ) {
00425       charbuf = '\0';
00426     } else {
00427       charbuf = theID[ i ];
00428     }
00429     *( (Tchar*)p ) = charbuf;
00430     ( (Tchar*)p ) ++;
00431     datasize += Tsizeof( Tchar );
00432   }
00433 
00434    
00435   // write number of entries
00436   *( (Tsize_t*)p ) = size();
00437   ( (Tsize_t*)p ) ++;
00438   datasize += Tsizeof( Tsize_t );
00439 
00440   for ( Tsize_t i = 0; i < size(); i ++ ) {
00441     Tint d = ( (*this)[ i ] ).Serialize( p );
00442     datasize += d;
00443     (Tbyte*)p += d;
00444   }
00445 
00446   return (Tint)datasize;
00447 }
00448 
00449 Tint TDataSection::Deserialize( const Tvoid* buffer )
00450 {
00451   Tvoid* p = const_cast<Tvoid*>(buffer);  
00452   Tsize_t datasize = 0;
00453 
00454   // read record size
00455   //SetRecordSize( *( (Tint*)p ) );
00456   ( (Tint*)p ) ++;
00457   datasize += Tsizeof( Tint );
00458 
00459 
00460   // read object type
00461   theObjectType = *( (Tobject_t*)p );
00462   ( (Tobject_t*)p ) ++;
00463   datasize += Tsizeof( Tobject_t );
00464 
00465 
00466   // read object ID
00467   Tsize_t nchar = *( (Tsize_t*)p );
00468   ( (Tsize_t*)p ) ++;
00469   datasize += Tsizeof( Tsize_t );
00470 
00471   Tchar* cc = new Tchar[ nchar ];
00472   for ( Tsize_t i = 0; i < nchar; i ++ ) {
00473     cc[i] = *( (Tchar*)p );
00474     datasize += Tsizeof( Tchar );
00475     ( (Tchar*)p ) ++;
00476   }
00477   theID = cc;
00478   delete [] cc;
00479 
00480 
00481   // read number of entries
00482   Tsize_t nentry = *( (Tsize_t*)p );
00483   ( (Tsize_t*)p ) ++;
00484   datasize += Tsizeof( Tsize_t );
00485 
00486 
00487   // push datasection
00488   for ( Tsize_t i = 0; i < nentry; i ++ ) {
00489     TDataSegment segment;
00490     Tint d = segment.Deserialize( p );
00491     this -> push_back( segment );
00492     datasize += d;
00493     (Tbyte*)p += d;
00494   }
00495 
00496 
00497   return (Tint)datasize;
00498 }
00499 
00500 const TDataSegment& TDataSection::operator[]( Tint n ) const
00501 {
00502   return *( begin() + n );
00503 }
00504 
00505 TDataSegment& TDataSection::operator[]( Tint n )
00506 {
00507   return *( begin() + n );
00508 }
00509 
00510 const TDataSegment& TDataSection::operator[]( const Tstring& id ) const
00511 {
00512   return (*this)[ FindDataSegment( id ) ];
00513 }
00514 
00515 TDataSegment& TDataSection::operator[]( const Tstring& id )
00516 {
00517   return (*this)[ FindDataSegment( id ) ];
00518 }
00519 
00520 #ifdef __CLDAQ_ROOT_DLL
00521     ClassImp(TDataSection)
00522 #endif


CLDAQ - a Class Library for DataAcQuisition (Version 1.14.3)
Go IWAI -- goiwai at users.sourceforge.jp