PLearn 0.1
object_conversions.cc
Go to the documentation of this file.
00001 // Test object conversion functions
00002 
00003 #include <iostream>
00004 
00005 #include <plearn/base/ObjectConversions.h>
00006 #include <plearn/base/Storage.h>
00007 #include <plearn/math/StatsCollector.h>
00008 
00009 using namespace std;
00010 
00011 namespace PLearn 
00012 {
00013 
00014 void notConvertible()
00015 {
00016   int x = 0;
00017   int* y = 0;
00018   Vec v;
00019   Mat m;
00020   Array<int> aint;
00021   TVec<int> vint;
00022   PPointable* p = 0;
00023   Storage<int> stor;
00024   PP<PLearn::Storage<int> > ppstor;
00025 
00026   cout << "*** SHOULD NOT BE CONVERTIBLE: ***" << endl;
00027   cout << "isConvertibleToObjectPtr(int)        : " << isConvertibleToObjectPtr(x)      << endl;
00028   cout << "isConvertibleToObjectPtr(int*)       : " << isConvertibleToObjectPtr(y)      << endl;
00029   cout << "isConvertibleToObjectPtr(Vec)        : " << isConvertibleToObjectPtr(v)      << endl;
00030   cout << "isConvertibleToObjectPtr(Mat)        : " << isConvertibleToObjectPtr(m)      << endl;
00031   cout << "isConvertibleToObjectPtr(Array<int>) : " << isConvertibleToObjectPtr(aint)   << endl;
00032   cout << "isConvertibleToObjectPtr(TVec<int>)  : " << isConvertibleToObjectPtr(vint)   << endl;
00033   cout << "isConvertibleToObjectPtr(PPointable*): " << isConvertibleToObjectPtr(p)      << endl;
00034   cout << "isConvertibleToObjectPtr(Storage)    : " << isConvertibleToObjectPtr(stor)   << endl;
00035   cout << "isConvertibleToObjectPtr(PP<Storage>): " << isConvertibleToObjectPtr(ppstor) << endl;
00036 }
00037 
00038 void convertible()
00039 {
00040   Object* u = 0;
00041   StatsCollector sc;
00042   PP<StatsCollector> ppsc = new StatsCollector;
00043   StatsCollector*    psc  = ppsc;
00044   const StatsCollector* cpsc = ppsc;
00045   TVec<StatsCollector> vsc;
00046   TVec< PP<StatsCollector> > ppvsc;
00047   int i;
00048 
00049   cout << endl << "*** SHOULD BE CONVERTIBLE: ***" << endl;
00050   cout << "isConvertibleToObjectPtr(Object*)                    : " << isConvertibleToObjectPtr(u)     << endl;
00051   cout << "isConvertibleToObjectPtr(StatsCollector)             : " << isConvertibleToObjectPtr(sc)    << endl;
00052   cout << "isConvertibleToObjectPtr(StatsCollector*)            : " << isConvertibleToObjectPtr(psc)   << endl;
00053   cout << "isConvertibleToObjectPtr(const StatsCollector*)      : " << isConvertibleToObjectPtr(cpsc)  << endl;
00054   cout << "isConvertibleToObjectPtr(PP<StatsCollector>)         : " << isConvertibleToObjectPtr(ppsc)  << endl;
00055   cout << "isConvertibleToObjectPtr(TVec<StatsCollector>)       : " << isConvertibleToObjectPtr(vsc)   << endl;
00056   cout << "isConvertibleToObjectPtr(TVec< PP<StatsCollector> >) : " << isConvertibleToObjectPtr(ppvsc) << endl;
00057 
00058   cout << endl << "*** TEST CONVERSIONS: ***" << endl;
00059   cout << "toObjectPtr(Object*)               : " << toObjectPtr(u)    << endl
00060        << "toObjectPtr(StatsCollector)        : " << toObjectPtr(sc)   << endl
00061        << "toObjectPtr(StatsCollector) [2]    : " << toObjectPtr(*psc) << endl
00062        << "toObjectPtr(StatsCollector*)       : " << toObjectPtr(psc)  << endl
00063        << "toObjectPtr(const StatsCollector*) : " << toObjectPtr(cpsc) << endl
00064        << "toObjectPtr(PP<StatsCollector>)    : " << toObjectPtr(ppsc) << endl;
00065 
00066   try {
00067     cout << "toObjectPtr(int)                 : " << toObjectPtr(i) << endl;
00068   }
00069   catch (PLearnError e) {
00070     cout << "... caught error '" << e.message() << "'" << endl;
00071   }
00072 }
00073 
00074 void notIndexable()
00075 {
00076   int x = 0;
00077   int* y = 0;
00078   Vec v;
00079   Mat m;
00080   Array<int> aint;
00081   TVec<int> vint;
00082   PPointable* p = 0;
00083   Storage<int> stor;
00084   PP<PLearn::Storage<int> > ppstor;
00085   Object* u = 0;
00086   StatsCollector sc;
00087   PP<StatsCollector> ppsc = new StatsCollector;
00088   StatsCollector*    psc  = ppsc;
00089   const StatsCollector* cpsc = ppsc;
00090 
00091   cout << endl << "*** INDEXABLE SIZE SHOULD BE ZERO: ***" << endl;
00092   cout << "indexableObjectSize(int)                   : " << indexableObjectSize(x)    << endl
00093        << "indexableObjectSize(int*)                  : " << indexableObjectSize(y)    << endl
00094        << "indexableObjectSize(Vec)                   : " << indexableObjectSize(v)    << endl
00095        << "indexableObjectSize(Array<int>)            : " << indexableObjectSize(aint) << endl
00096        << "indexableObjectSize(PPointable*)           : " << indexableObjectSize(p)    << endl
00097        << "indexableObjectSize(Storage<int>)          : " << indexableObjectSize(stor) << endl
00098        << "indexableObjectSize(Object*)               : " << indexableObjectSize(u)    << endl
00099        << "indexableObjectSize(StatsCollector)        : " << indexableObjectSize(sc)   << endl
00100        << "indexableObjectSize(PP<StatsCollector>)    : " << indexableObjectSize(ppsc) << endl
00101        << "indexableObjectSize(StatsCollector*)       : " << indexableObjectSize(psc)  << endl
00102        << "indexableObjectSize(const StatsCollector*) : " << indexableObjectSize(cpsc) << endl;
00103 }
00104 
00105 void indexable()
00106 {
00107   TVec<StatsCollector> vsc(5);
00108   TVec< PP<StatsCollector> > ppvsc;
00109 
00110   for (int i=0 ; i<5 ; ++i)
00111     ppvsc.push_back(new StatsCollector);
00112 
00113   cout << endl << "*** INDEXABLE SIZE SHOULD BE 5: ***" << endl;
00114   cout << "indexableObjectSize(TVec<StatsCollector>)       : " << indexableObjectSize(vsc) << endl
00115        << "indexableObjectSize(TVec< PP<StatsCollector> >) : " << indexableObjectSize(ppvsc) << endl;
00116 
00117   cout << endl << "*** TRY INDEXING: ***" << endl;
00118   for (int i=0 ; i<5 ; ++i)
00119     cout << "toIndexedObjectPtr(TVec<StatsCollector>[" << i << "])       : "
00120          << toIndexedObjectPtr(vsc, i) << endl;
00121   for (int i=0 ; i<5 ; ++i)
00122     cout << "toIndexedObjectPtr(TVec< PP<StatsCollector> >[" << i << "]) : "
00123          << toIndexedObjectPtr(ppvsc, i) << endl;
00124 }
00125 
00126 
00127 
00128 } // end of namespace PLearn
00129 
00130 
00131 
00132 using namespace PLearn;
00133 
00134 int main()
00135 {
00136   notConvertible();
00137   convertible();
00138   notIndexable();
00139   indexable();
00140   
00141   return 0;
00142 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines