Adapters.cxx

Go to the documentation of this file.
00001 // Bindings
00002 #include "PyROOT.h"
00003 #include "Adapters.h"
00004 
00005 // ROOT
00006 #include "TBaseClass.h"
00007 #include "TClass.h"
00008 #include "TClassEdit.h"
00009 #include "TDataType.h"
00010 #include "TDataMember.h"
00011 #include "TMethod.h"
00012 #include "TFunction.h"
00013 #include "TMethodArg.h"
00014 #include "TList.h"
00015 #include "TError.h"
00016 
00017 // CINT
00018 #include "Api.h"
00019 
00020 
00021 //= TReturnTypeAdapter =======================================================
00022 std::string PyROOT::TReturnTypeAdapter::Name( unsigned int mod ) const
00023 {
00024 // get the name of the return type that is being adapted
00025    std::string name = fName;
00026 
00027    if ( ! ( mod & ( ROOT::Reflex::QUALIFIED | ROOT::Reflex::Q ) ) )
00028       name = TClassEdit::CleanType( fName.c_str(), 1 );
00029 
00030    if ( mod & ( ROOT::Reflex::FINAL | ROOT::Reflex::F ) )
00031       return TClassEdit::ResolveTypedef( name.c_str(), true );
00032 
00033    return name;
00034 }
00035 
00036 
00037 //= TMemberAdapter ===========================================================
00038 PyROOT::TMemberAdapter::TMemberAdapter( TMethod* meth ) : fMember( meth )
00039 {
00040    /* empty */
00041 }
00042 
00043 //____________________________________________________________________________
00044 PyROOT::TMemberAdapter::operator TMethod*() const
00045 {
00046 // cast the adapter to a TMethod* being adapted, returns 0 on failure
00047    return dynamic_cast< TMethod* >( const_cast< TDictionary* >( fMember ) );
00048 }
00049 
00050 //____________________________________________________________________________
00051 PyROOT::TMemberAdapter::TMemberAdapter( TFunction* func ) : fMember( func )
00052 {
00053    /* empty */
00054 }
00055 
00056 //____________________________________________________________________________
00057 PyROOT::TMemberAdapter::operator TFunction*() const
00058 {
00059 // cast the adapter to a TFunction* being adapted, returns 0 on failure
00060    return dynamic_cast< TFunction* >( const_cast< TDictionary* >( fMember ) );
00061 }
00062 
00063 //____________________________________________________________________________
00064 PyROOT::TMemberAdapter::TMemberAdapter( TDataMember* mb ) : fMember( mb )
00065 {
00066    /* empty */
00067 }
00068 
00069 //____________________________________________________________________________
00070 PyROOT::TMemberAdapter::operator TDataMember*() const
00071 {
00072 // cast the adapter to a TDataMember* being adapted, returns 0 on failure
00073    return dynamic_cast< TDataMember* >( const_cast< TDictionary* >( fMember ) );
00074 }
00075 
00076 //____________________________________________________________________________
00077 PyROOT::TMemberAdapter::TMemberAdapter( TMethodArg* ma ) : fMember( ma )
00078 {
00079    /* empty */
00080 }
00081 
00082 //____________________________________________________________________________
00083 PyROOT::TMemberAdapter::operator TMethodArg*() const
00084 {
00085 // cast the adapter to a TMethodArg* being adapted, returns 0 on failure
00086    return dynamic_cast< TMethodArg* >( const_cast< TDictionary* >( fMember ) );
00087 }
00088 
00089 //____________________________________________________________________________
00090 std::string PyROOT::TMemberAdapter::Name( unsigned int mod ) const
00091 {
00092 // Return name of the type described by fMember
00093    TMethodArg* arg = (TMethodArg*)*this;
00094 
00095    if ( arg ) {
00096 
00097       std::string name = arg->GetTypeName();
00098       if ( mod & ( ROOT::Reflex::QUALIFIED | ROOT::Reflex::Q ) )
00099          name = arg->GetFullTypeName();
00100 
00101       if ( mod & ( ROOT::Reflex::FINAL | ROOT::Reflex::F ) )
00102          return TClassEdit::ResolveTypedef( name.c_str(), true );
00103 
00104       return name;
00105 
00106    } else if ( mod & ( ROOT::Reflex::FINAL | ROOT::Reflex::F ) )
00107       return TClassEdit::ResolveTypedef( fMember->GetName(), true );
00108 
00109    return fMember->GetName();
00110 }
00111 
00112 //____________________________________________________________________________
00113 Bool_t PyROOT::TMemberAdapter::IsEnum() const
00114 {
00115 // test if the adapted member is of an enum type
00116    return fMember->Property() & kIsEnum;
00117 }
00118 
00119 //____________________________________________________________________________
00120 Bool_t PyROOT::TMemberAdapter::IsPublic() const
00121 {
00122 // test if the adapted member represents an public (data) member
00123    return fMember->Property() & kIsPublic;
00124 }
00125 
00126 //____________________________________________________________________________
00127 Bool_t PyROOT::TMemberAdapter::IsStatic() const
00128 {
00129 // test if the adapted member represents a class (data) member
00130    return fMember->Property() & G__BIT_ISSTATIC;
00131 }
00132 
00133 //____________________________________________________________________________
00134 size_t PyROOT::TMemberAdapter::FunctionParameterSize( bool required ) const
00135 {
00136 // get the total number of parameters that the adapted function/method takes
00137    TFunction* func = (TFunction*)fMember;
00138    if ( ! func )
00139       return 0;
00140 
00141    if ( required == true )
00142       return func->GetNargs() - func->GetNargsOpt();
00143 
00144    return func->GetNargs();
00145 }
00146 
00147 //____________________________________________________________________________
00148 PyROOT::TMemberAdapter PyROOT::TMemberAdapter::FunctionParameterAt( size_t nth ) const
00149 {
00150 // get the type info of the function parameter at position nth
00151    return (TMethodArg*)((TFunction*)fMember)->GetListOfMethodArgs()->At( nth );
00152 }
00153 
00154 //____________________________________________________________________________
00155 std::string PyROOT::TMemberAdapter::FunctionParameterNameAt( size_t nth ) const
00156 {
00157 // get the formal name, if available, of the function parameter at position nth
00158    const char* name =
00159       ((TMethodArg*)((TFunction*)fMember)->GetListOfMethodArgs()->At( nth ))->GetName();
00160 
00161    if ( name )
00162       return name;
00163    return "";
00164 }
00165 
00166 //____________________________________________________________________________
00167 std::string PyROOT::TMemberAdapter::FunctionParameterDefaultAt( size_t nth ) const
00168 {
00169 // get the default value, if available, of the function parameter at position nth
00170    TMethodArg* arg = (TMethodArg*)((TFunction*)fMember)->GetListOfMethodArgs()->At( nth );
00171    const char* def = arg->GetDefault();
00172 
00173    if ( ! def )
00174       return "";
00175 
00176 // special case for strings: "some value" -> ""some value"
00177    if ( strstr( TClassEdit::ResolveTypedef( arg->GetTypeName(), true ).c_str(), "char*" ) ) {
00178       std::string sdef = "\"";
00179       sdef += def;
00180       sdef += "\"";
00181       return sdef;
00182    }
00183 
00184    return def;
00185 }
00186 
00187 //____________________________________________________________________________
00188 PyROOT::TReturnTypeAdapter PyROOT::TMemberAdapter::ReturnType() const
00189 {
00190 // get the return type of the wrapped function/method
00191    return TReturnTypeAdapter( ((TFunction*)fMember)->GetReturnTypeName() );
00192 }
00193 
00194 //____________________________________________________________________________
00195 PyROOT::TScopeAdapter PyROOT::TMemberAdapter::DeclaringScope() const
00196 {
00197 // get the declaring scope (class) of the wrapped function/method
00198    TMethod* method = (TMethod*)*this;
00199    if ( method )
00200       return method->GetClass();
00201 
00202 // happens for free-standing functions (i.e. global scope)
00203    return std::string( "" );
00204 }
00205 
00206 
00207 //= TBaseAdapter =============================================================
00208 std::string PyROOT::TBaseAdapter::Name() const
00209 {
00210 // get the name of the base class that is being adapted
00211    return fBase->GetName();
00212 }
00213 
00214 
00215 //= TScopeAdapter ============================================================
00216 PyROOT::TScopeAdapter::TScopeAdapter( TClass* klass ) : fClass( klass )
00217 {
00218 // wrap a class (scope)
00219    if ( fClass.GetClass() != 0 )
00220       fName = fClass->GetName();
00221 }
00222 
00223 //____________________________________________________________________________
00224 PyROOT::TScopeAdapter::TScopeAdapter( const std::string& name ) :
00225    fClass( name.c_str() ), fName( name )
00226 {
00227    /* empty */
00228 }
00229 
00230 PyROOT::TScopeAdapter::TScopeAdapter( const TMemberAdapter& mb ) :
00231       fClass( mb.Name( ROOT::Reflex::SCOPED ).c_str() ),
00232       fName( mb.Name( ROOT::Reflex::Q | ROOT::Reflex::S ) )
00233 {
00234    /* empty */
00235 }
00236 
00237 //____________________________________________________________________________
00238 PyROOT::TScopeAdapter PyROOT::TScopeAdapter::ByName( const std::string & name )
00239 {
00240 // lookup a scope (class) by name
00241    return TClass::GetClass( name.c_str() );
00242 }
00243 
00244 //____________________________________________________________________________
00245 std::string PyROOT::TScopeAdapter::Name( unsigned int mod ) const
00246 {
00247 // Return name of type described by fClass
00248    if ( ! fClass.GetClass() || ! fClass->Property() ) {
00249    // fundamental types have no class, and unknown classes have no property
00250       std::string name = fName;
00251 
00252       if ( ! ( mod & ( ROOT::Reflex::QUALIFIED | ROOT::Reflex::Q ) ) )
00253          name = TClassEdit::CleanType( fName.c_str(), 1 );
00254 
00255       if ( mod & ( ROOT::Reflex::FINAL | ROOT::Reflex::F ) )
00256          return TClassEdit::ResolveTypedef( name.c_str(), true );
00257 
00258       return name;
00259    }
00260 
00261    if ( mod & ( ROOT::Reflex::FINAL | ROOT::Reflex::F ) ) {
00262       G__ClassInfo* clInfo = (G__ClassInfo*)fClass->GetClassInfo();
00263       if ( mod & ( ROOT::Reflex::SCOPED | ROOT::Reflex::S ) )
00264          return clInfo ? clInfo->Fullname() : fClass->GetName();
00265 
00266    // unscoped name ...
00267       std::string actual = clInfo ? clInfo->Name() : fClass->GetName();
00268 
00269    // in case of missing dictionaries, the scope won't have been stripped
00270       if ( ! ( clInfo && clInfo->IsValid() ) ) {
00271          std::string::size_type pos = actual.substr( 0, actual.find( '<' ) ).rfind( "::" );
00272          if ( pos != std::string::npos ) {
00273          // this is somewhat of a gamble, but the alternative is a guaranteed crash
00274             actual = actual.substr( pos + 2, std::string::npos );
00275          }
00276       }
00277 
00278       return actual;
00279 
00280    } else if ( ! ( mod & ( ROOT::Reflex::SCOPED | ROOT::Reflex::S ) ) ) {
00281       G__ClassInfo* clInfo = (G__ClassInfo*)fClass->GetClassInfo();
00282       return clInfo ? clInfo->Name() : fClass->GetName();
00283    }
00284 
00285    return fClass->GetName();
00286 }
00287 
00288 //____________________________________________________________________________
00289 size_t PyROOT::TScopeAdapter::BaseSize() const
00290 {
00291 // get the total number of base classes that this class has
00292    if ( fClass.GetClass() && fClass->GetListOfBases() != 0 )
00293       return fClass->GetListOfBases()->GetSize();
00294 
00295    return 0;
00296 }
00297 
00298 //____________________________________________________________________________
00299 PyROOT::TBaseAdapter PyROOT::TScopeAdapter::BaseAt( size_t nth ) const
00300 {
00301 // get the nth base of this class
00302    return (TBaseClass*)fClass->GetListOfBases()->At( nth );
00303 }
00304 
00305 //____________________________________________________________________________
00306 size_t PyROOT::TScopeAdapter::FunctionMemberSize() const
00307 {
00308 // get the total number of methods that this class has
00309    if ( fClass.GetClass() )
00310       return fClass->GetListOfMethods()->GetSize();
00311 
00312    return 0;
00313 }
00314 
00315 //____________________________________________________________________________
00316 PyROOT::TMemberAdapter PyROOT::TScopeAdapter::FunctionMemberAt( size_t nth ) const
00317 {
00318 // get the nth method of this class
00319    return (TMethod*)fClass->GetListOfMethods()->At( nth );
00320 }
00321 
00322 //____________________________________________________________________________
00323 size_t PyROOT::TScopeAdapter::DataMemberSize() const
00324 {
00325 // get the total number of data members that this class has
00326    if ( fClass.GetClass() )
00327       return fClass->GetListOfDataMembers()->GetSize();
00328 
00329    return 0;
00330 }
00331 
00332 //____________________________________________________________________________
00333 PyROOT::TMemberAdapter PyROOT::TScopeAdapter::DataMemberAt( size_t nth ) const
00334 {
00335 // get the nth data member of this class
00336    return (TDataMember*)fClass->GetListOfDataMembers()->At( nth );
00337 }
00338 
00339 //____________________________________________________________________________
00340 PyROOT::TScopeAdapter::operator bool() const
00341 {
00342 // check the validity of this scope (class)
00343    if ( fName.empty() )
00344       return false;
00345 
00346    Int_t oldEIL = gErrorIgnoreLevel;
00347    gErrorIgnoreLevel = 3000;
00348    bool b = G__TypeInfo( Name( ROOT::Reflex::Q | ROOT::Reflex::S ).c_str() ).IsValid();
00349    gErrorIgnoreLevel = oldEIL;
00350    return b;
00351 }
00352       
00353 //____________________________________________________________________________
00354 Bool_t PyROOT::TScopeAdapter::IsComplete() const
00355 {
00356 // verify whether the dictionary of this class is fully available
00357    return G__TypeInfo( Name( ROOT::Reflex::SCOPED ).c_str() ).IsLoaded();
00358 }
00359 
00360 //____________________________________________________________________________
00361 Bool_t PyROOT::TScopeAdapter::IsClass() const
00362 {
00363 // test if this scope represents a class
00364    if ( fClass.GetClass() ) {
00365    // some inverted logic: we don't have a TClass, but a builtin will be recognized, so
00366    // if it is NOT a builtin, it is a class or struct (but may be missing dictionary)
00367       return (fClass->Property() & kIsClass) || ! (fClass->Property() & kIsFundamental);
00368    }
00369 
00370 // no class can mean either is no class (i.e. builtin), or no dict but coming in
00371 // through PyCintex/Reflex ... as a workaround, use TDataTypes that has a full
00372 // enumeration of builtin types
00373    return TDataType( Name( ROOT::Reflex::F | ROOT::Reflex::S ).c_str() ).GetType() == kOther_t;
00374 }
00375 
00376 //____________________________________________________________________________
00377 Bool_t PyROOT::TScopeAdapter::IsStruct() const
00378 {
00379 // test if this scope represents a struct
00380    if ( fClass.GetClass() ) {
00381    // same logic as for IsClass() above ...
00382       return (fClass->Property() & kIsStruct) || ! (fClass->Property() & kIsFundamental);
00383    }
00384 
00385 // same logic as for IsClass() above ...
00386    return TDataType( Name( ROOT::Reflex::F | ROOT::Reflex::S ).c_str() ).GetType() == kOther_t;
00387 }
00388 
00389 //____________________________________________________________________________
00390 Bool_t PyROOT::TScopeAdapter::IsNamespace() const
00391 {
00392 // test if this scope represents a namespace
00393    if ( fClass.GetClass() )
00394       return fClass->Property() & G__BIT_ISNAMESPACE;
00395 
00396    return kFALSE;
00397 }
00398 
00399 //____________________________________________________________________________
00400 Bool_t PyROOT::TScopeAdapter::IsAbstract() const
00401 {
00402 // test if this scope represents an abstract class
00403    if ( fClass.GetClass() )
00404       return fClass->Property() & kIsAbstract;   // assume set only for classes
00405 
00406    return kFALSE;
00407 }

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