TEmulatedMapProxy.cxx

Go to the documentation of this file.
00001 // @(#)root/io:$Id: TEmulatedMapProxy.cxx 36061 2010-10-04 16:05:51Z pcanal $
00002 // Author: Markus Frank 28/10/04
00003 
00004 /*************************************************************************
00005  * Copyright (C) 1995-2000, Rene Brun and Fons Rademakers.               *
00006  * All rights reserved.                                                  *
00007  *                                                                       *
00008  * For the licensing terms see $ROOTSYS/LICENSE.                         *
00009  * For the list of contributors see $ROOTSYS/README/CREDITS.             *
00010  *************************************************************************/
00011 
00012 //////////////////////////////////////////////////////////////////////////
00013 //                                                                      //
00014 // TEmulatedMapProxy
00015 //
00016 // Streamer around an arbitrary container, which implements basic
00017 // functionality and iteration.
00018 //
00019 // In particular this is used to implement splitting and abstract
00020 // element access of any container. Access to compiled code is necessary
00021 // to implement the abstract iteration sequence and functionality like
00022 // size(), clear(), resize(). resize() may be a void operation.
00023 //
00024 //////////////////////////////////////////////////////////////////////////
00025 
00026 #include "TEmulatedMapProxy.h"
00027 #include "TClassEdit.h"
00028 #include "TStreamerInfo.h"
00029 #include "TError.h"
00030 
00031 TEmulatedMapProxy::TEmulatedMapProxy(const TEmulatedMapProxy& copy)
00032    : TEmulatedCollectionProxy(copy)
00033 {
00034    // copy constructor
00035    if ( !(fSTL_type == TClassEdit::kMap || fSTL_type == TClassEdit::kMultiMap) )  {
00036       Fatal("TEmulatedMapProxy","Class %s is not a map-type!",fName.c_str());
00037    }
00038 }
00039 
00040 TEmulatedMapProxy::TEmulatedMapProxy(const char* cl_name)
00041    : TEmulatedCollectionProxy(cl_name)
00042 {
00043    // Build a Streamer for an emulated vector whose type is 'name'.
00044    if ( !(fSTL_type == TClassEdit::kMap || fSTL_type == TClassEdit::kMultiMap) )  {
00045       Fatal("TEmulatedMapProxy","Class %s is not a map-type!",fName.c_str());
00046    }
00047 }
00048 
00049 TEmulatedMapProxy::~TEmulatedMapProxy()
00050 {
00051    // Standard destructor.
00052 }
00053 
00054 TVirtualCollectionProxy* TEmulatedMapProxy::Generate() const
00055 {
00056    // Virtual copy constructor.
00057    if ( !fClass ) Initialize();
00058    return new TEmulatedMapProxy(*this);
00059 }
00060 
00061 void* TEmulatedMapProxy::At(UInt_t idx)
00062 {
00063    // Return the address of the value at index 'idx'.
00064    if ( fEnv && fEnv->fObject )   {
00065       PCont_t c = PCont_t(fEnv->fObject);
00066       return idx<(c->size()/fValDiff) ? ((char*)&(*c->begin())) + idx*fValDiff : 0;
00067    }
00068    Fatal("TEmulatedMapProxy","At> Logic error - no proxy object set.");
00069    return 0;
00070 }
00071 
00072 UInt_t TEmulatedMapProxy::Size() const
00073 {
00074    // Return the current size of the container.
00075    if ( fEnv && fEnv->fObject )   {
00076       PCont_t c = PCont_t(fEnv->fObject);
00077       return fEnv->fSize = (c->size()/fValDiff);
00078    }
00079    Fatal("TEmulatedMapProxy","Size> Logic error - no proxy object set.");
00080    return 0;
00081 }
00082 
00083 void TEmulatedMapProxy::ReadMap(int nElements, TBuffer &b)
00084 {
00085    // Map input streamer.
00086    Bool_t vsn3 = b.GetInfo() && b.GetInfo()->GetOldVersion()<=3;
00087    int    idx, loop, off[2] = {0, fValOffset };
00088    Value  *v, *val[2] = { fKey, fVal };
00089    StreamHelper* helper;
00090    float f;
00091    char* addr = 0;
00092    char* temp = (char*)At(0);
00093    for ( idx = 0; idx < nElements; ++idx )  {
00094       addr = temp + idx*fValDiff;
00095       for ( loop=0; loop<2; loop++)  {
00096          addr += off[loop];
00097          helper = (StreamHelper*)addr;
00098          v = val[loop];
00099          switch (v->fCase) {
00100          case G__BIT_ISFUNDAMENTAL:  // Only handle primitives this way
00101          case G__BIT_ISENUM:
00102             switch( int(v->fKind) )   {
00103             case kBool_t:    b >> helper->boolean;     break;
00104             case kChar_t:    b >> helper->s_char;      break;
00105             case kShort_t:   b >> helper->s_short;     break;
00106             case kInt_t:     b >> helper->s_int;       break;
00107             case kLong_t:    b >> helper->s_long;      break;
00108             case kLong64_t:  b >> helper->s_longlong;  break;
00109             case kFloat_t:   b >> helper->flt;         break;
00110             case kFloat16_t: b >> f;
00111                helper->flt = float(f);  break;
00112             case kDouble_t:  b >> helper->dbl;         break;
00113             case kBOOL_t:    b >> helper->boolean;     break;
00114             case kUChar_t:   b >> helper->u_char;      break;
00115             case kUShort_t:  b >> helper->u_short;     break;
00116             case kUInt_t:    b >> helper->u_int;       break;
00117             case kULong_t:   b >> helper->u_long;      break;
00118             case kULong64_t: b >> helper->u_longlong;  break;
00119             case kDouble32_t:b >> f;
00120                helper->dbl = double(f);  break;
00121             case kchar:
00122             case kNoType_t:
00123             case kOther_t:
00124                Error("TEmulatedMapProxy","fType %d is not supported yet!\n",v->fKind);
00125             }
00126             break;
00127          case G__BIT_ISCLASS:
00128             b.StreamObject(helper,v->fType);
00129             break;
00130          case kBIT_ISSTRING:
00131             helper->read_std_string(b);
00132             break;
00133          case G__BIT_ISPOINTER|G__BIT_ISCLASS:
00134             helper->set(b.ReadObjectAny(v->fType));
00135             break;
00136          case G__BIT_ISPOINTER|kBIT_ISSTRING:
00137             helper->read_std_string_pointer(b);
00138             break;
00139          case G__BIT_ISPOINTER|kBIT_ISTSTRING|G__BIT_ISCLASS:
00140             helper->read_tstring_pointer(vsn3,b);
00141             break;
00142          }
00143       }
00144    }
00145 }
00146 
00147 void TEmulatedMapProxy::WriteMap(int nElements, TBuffer &b)
00148 {
00149    // Map output streamer.
00150    Value  *v, *val[2] = { fKey, fVal };
00151    int    off[2]      = { 0, fValOffset };
00152    StreamHelper* i;
00153    char* addr = 0;
00154    char* temp = (char*)At(0);
00155    for (int loop, idx = 0; idx < nElements; ++idx )  {
00156       addr = temp + idx*fValDiff;
00157       for ( loop = 0; loop<2; ++loop )  {
00158          addr += off[loop];
00159          i = (StreamHelper*)addr;
00160          v = val[loop];
00161          switch (v->fCase) {
00162          case G__BIT_ISFUNDAMENTAL:  // Only handle primitives this way
00163          case G__BIT_ISENUM:
00164             switch( int(v->fKind) )   {
00165             case kBool_t:    b << i->boolean;     break;
00166             case kChar_t:    b << i->s_char;      break;
00167             case kShort_t:   b << i->s_short;     break;
00168             case kInt_t:     b << i->s_int;       break;
00169             case kLong_t:    b << i->s_long;      break;
00170             case kLong64_t:  b << i->s_longlong;  break;
00171             case kFloat_t:   b << i->flt;         break;
00172             case kFloat16_t: b << float(i->flt);  break;
00173             case kDouble_t:  b << i->dbl;         break;
00174             case kBOOL_t:    b << i->boolean;     break;
00175             case kUChar_t:   b << i->u_char;      break;
00176             case kUShort_t:  b << i->u_short;     break;
00177             case kUInt_t:    b << i->u_int;       break;
00178             case kULong_t:   b << i->u_long;      break;
00179             case kULong64_t: b << i->u_longlong;  break;
00180             case kDouble32_t:b << float(i->dbl);  break;
00181             case kchar:
00182             case kNoType_t:
00183             case kOther_t:
00184                Error("TEmulatedMapProxy","fType %d is not supported yet!\n",v->fKind);
00185             }
00186             break;
00187          case G__BIT_ISCLASS:
00188             b.StreamObject(i,v->fType);
00189             break;
00190          case kBIT_ISSTRING:
00191             TString(i->c_str()).Streamer(b);
00192             break;
00193          case G__BIT_ISPOINTER|G__BIT_ISCLASS:
00194             b.WriteObjectAny(i->ptr(),v->fType);
00195             break;
00196          case kBIT_ISSTRING|G__BIT_ISPOINTER:
00197             i->write_std_string_pointer(b);
00198             break;
00199          case kBIT_ISTSTRING|G__BIT_ISCLASS|G__BIT_ISPOINTER:
00200             i->write_tstring_pointer(b);
00201             break;
00202          }
00203       }
00204    }
00205 }
00206 
00207 void TEmulatedMapProxy::ReadBuffer(TBuffer &b, void *obj, const TClass *onfileClass)
00208 {
00209    // Read portion of the streamer.
00210 
00211    SetOnFileClass((TClass*)onfileClass);
00212    ReadBuffer(b,obj);
00213 }
00214 
00215 void TEmulatedMapProxy::ReadBuffer(TBuffer &b, void *obj)
00216 {
00217    // Read portion of the streamer.
00218 
00219    TPushPop env(this,obj);
00220    int nElements = 0;
00221    b >> nElements;
00222    if ( fEnv->fObject )  {
00223       Resize(nElements,true);
00224    }
00225    if ( nElements > 0 )  {
00226       ReadMap(nElements, b);
00227    }
00228 }
00229 
00230 void TEmulatedMapProxy::Streamer(TBuffer &b)
00231 {
00232    // TClassStreamer IO overload.
00233    if ( b.IsReading() ) {  //Read mode
00234       int nElements = 0;
00235       b >> nElements;
00236       if ( fEnv->fObject )  {
00237          Resize(nElements,true);
00238       }
00239       if ( nElements > 0 )  {
00240          ReadMap(nElements, b);
00241       }
00242    }
00243    else {     // Write case
00244       int nElements = fEnv->fObject ? Size() : 0;
00245       b << nElements;
00246       if ( nElements > 0 )  {
00247          WriteMap(nElements, b);
00248       }
00249    }
00250 }

Generated on Tue Jul 5 14:30:14 2011 for ROOT_528-00b_version by  doxygen 1.5.1