Main Page   Namespace List   Class Hierarchy   Compound List   File List   Namespace Members   Compound Members   File Members  

CdbRooTechNeutralProxyTest.cc

Go to the documentation of this file.
00001 // File and Version Information:
00002 //      $Id: CdbRooTechNeutralProxyTest.cc,v 1.4 2005/11/04 02:09:40 gapon Exp $
00003 
00004 #include "BaBar/BaBar.hh"
00005 
00006 /// This is a test application for "technology-neutral" proxies
00007 /**
00008   * This application will instantiate a simple technology-neutral proxy class
00009   * template and the corresponding "persistent-to-transient" translator.
00010   *
00011   * @see class 
00012   * @see class CdbRooSimpleTranslator
00013   */
00014 #include "CdbBase/CdbTransaction.hh"
00015 #include "CdbBase/Cdb.hh"
00016 #include "CdbBase/CdbEnvProxy.hh"
00017 
00018 #include "CdbRoo/CdbRooSimpleTranslator.hh"
00019 
00020 #include "CdbRooTable/CdbRooNTupleSimpleImplR.hh"
00021 
00022 #include "CdbRooReadonly/CdbRooReadonly.hh"
00023 
00024 #include "BdbTime/BdbTime.hh"
00025 
00026 #include "CdbRooTests/CdbRooTestClassR.hh"
00027 #include "CdbRooTests/CdbRooTestClassNewR.hh"
00028 
00029 #include "ProxyDict/IfdSimpleProxyDict.hh"
00030 #include "ProxyDict/Ifd.hh"
00031 #include "ProxyDict/IfdStrKey.hh"
00032 
00033 #include "AbsEnv/AbsEnv.hh"
00034 #include "GenEnv/GenEnv.hh"
00035 
00036 #include <stdio.h>  // sscanf()
00037 
00038 #include <string>
00039 using std::cerr;
00040 using std::cout;
00041 using std::endl;
00042 
00043 namespace {
00044 
00045   /// Loader for ROOT I/O dictionary of persistent classes
00046   /**
00047     * Default constructors of the 'payload' classes to be loaded from CDB
00048     * needs to be executed at least one to bring in the corresponding dictionary.
00049     * Otherwise the loaded objects will have incomplete shape (they'll be TObject-s only).
00050     */
00051     void loadPayloadClassesDictionary( )
00052     {
00053         CdbRooObjectR       base;
00054         CdbRooTestClassR    test1;
00055         CdbRooTestClassNewR test2;
00056         CdbRooNTupleSimpleImplR<unsigned int> test3;
00057     }
00058 
00059   /// A test translator for N-Tuples
00060   /**
00061     * Note, it's just a test. The normal translator will be implemented as a part of
00062     * of the CdbRooTable package.
00063     */
00064     class TestNTupleTranslator : public CdbRooObjectTranslatorRT< CdbRooNTupleSimpleImplR<unsigned int>, CdbRooTestClass > {
00065     public:
00066         virtual CdbStatus toTransientRT( CdbRooTestClass*&                                       theTransientPtr,
00067                                          const CdbCPtr< CdbRooNTupleSimpleImplR<unsigned int> >& theObjectPtr ) const
00068         {
00069             theTransientPtr = new CdbRooTestClass( "translated from: " + persistentTypeName( ));
00070             return CdbStatus::Success;
00071         }
00072     };
00073 };
00074 
00075 ///////////
00076 // Tests //
00077 ///////////
00078 
00079 static void doTest( IfdDataProxyTemplate<CdbRooTestClass>* theProxyPtr );
00080 
00081 ////////////////////////////////////////
00082 // Starting point for the application //
00083 ////////////////////////////////////////
00084 
00085 int
00086 main( int argc, char** argv )
00087 {
00088   // Make sure the proper libraries are linked and factories
00089   // are instantiated.
00090 
00091     CdbRooReadonly::forceLoad( );
00092 
00093   // Make sure there is a proper transaction context
00094 
00095     CdbTransaction readOnlyTransaction;
00096 
00097   // Install translators
00098 
00099     {
00100         CdbObjectTranslator* translatorPtr = new CdbRooSimpleTranslator< CdbRooTestClassR, CdbRooTestClass >( );
00101         if( CdbStatus::Success != Cdb::instance( )->registerTranslator( translatorPtr )) {
00102             cerr << "failed to register the translator #1." << endl;
00103             delete translatorPtr;
00104             return 1;
00105         }
00106     }
00107     {
00108         CdbObjectTranslator* translatorPtr = new TestNTupleTranslator( );
00109         if( CdbStatus::Success != Cdb::instance( )->registerTranslator( translatorPtr )) {
00110             cerr << "failed to register the translator #2." << endl;
00111             delete translatorPtr;
00112             return 1;
00113         }
00114     }
00115 
00116   // Test if translators got properly installed. Run the test two times per
00117   // each translator to see if any memory corruption is taking place.
00118 
00119     for( int i = 0; i < 2; ++i ) {
00120         CdbCPtr<CdbObjectTranslator> ptr;
00121         if( CdbStatus::Success != Cdb::instance( )->findTranslator( ptr,
00122                                                                     CdbType2Id<CdbRooTestClass>::id( ),
00123                                                                     "CdbRooTestClassR" )) {
00124             cerr << "failed to find the previously installed translator for:\n"
00125                  << "    transientTypeId=" << CdbType2Id<CdbRooTestClass>::id( ) << "\n"
00126                  << "    persistentTypeName='CdbRooTestClassR'" << endl;
00127         }
00128         cout << "found previously installed translator for:\n"
00129                  << "    transientTypeId=" << CdbType2Id<CdbRooTestClass>::id( ) << "\n"
00130                  << "    persistentTypeName='CdbRooTestClassR'\n"
00131                  << "    at address=" << ptr.get( ) << endl;
00132     }
00133 
00134   // Proceed to tests
00135 
00136     doTest( new CdbEnvProxy< CdbRooTestClass >( "test", "C1" ));
00137 
00138     return 0;
00139 }
00140 
00141 static void
00142 doTest( IfdDataProxyTemplate<CdbRooTestClass>* theProxyPtr )
00143 {
00144     assert( 0 != theProxyPtr );
00145 
00146   // Simulate global environment
00147 
00148     gblEnv = new AbsEnv( );
00149     gblEnv->setGen( new GenEnv( ));
00150 
00151     BdbTime currentTime( 10 );
00152     EidCondKeyTriplet primaryTriplet   ( 0, 0, currentTime );
00153     EidCondKeyTriplet backgroundTriplet( 0, 0, currentTime );
00154 
00155     gblEnv->getGen( )->setConditionsKeys( &currentTime,
00156                                                       &primaryTriplet, 
00157                                                       &backgroundTriplet );
00158 
00159     gblPEnv = new IfdSimpleProxyDict;
00160 
00161   // Register our simple proxy
00162 
00163     bool result = Ifd<CdbRooTestClass>::put( gblPEnv,
00164                                              theProxyPtr,
00165                                              IfdStrKey( "/test/C1" ));
00166     if( !result ) cerr << "failed to put the proxy into the dictionary." << endl;
00167 
00168   // Try to use the proxy
00169 
00170     const CdbRooTestClass* t;
00171     
00172     for( int i = 0; i < 5; ++i ) {
00173         t = Ifd<CdbRooTestClass>::get( gblPEnv,
00174                                        IfdStrKey( "/test/C1" ));
00175         if( 0 == t ) {
00176             cout << "Ifd<T>::get() failed and returned 0 pointer onto a transient object." << endl
00177                  << endl;
00178         } else {
00179             cout << "Ifd<T>::get() succeded. The value: \"" << t->description( ) << "\"" << endl
00180                  << endl;
00181         }
00182 
00183       // Change the time at the global environment. This will invalidate the proxy
00184 
00185         currentTime       = BdbTime( BdbTime::now( ));
00186         primaryTriplet    = EidCondKeyTriplet( 0, 0, currentTime );
00187         backgroundTriplet = EidCondKeyTriplet( 0, 0, currentTime );
00188 
00189         gblEnv->getGen( )->setConditionsKeys( &currentTime,
00190                                                       &primaryTriplet, 
00191                                                       &backgroundTriplet );
00192     }
00193 }
00194 
00195 /////////////////
00196 // End Of File //
00197 /////////////////

Generated on Mon Dec 5 18:22:11 2005 for CDB by doxygen1.3-rc3