• Main Page
  • Namespaces
  • Data Structures
  • Files
  • File List

/www/proggenOrg/dedupe/export/trunk/dataholding/datavariant.h

Go to the documentation of this file.
00001 
00008 #ifndef ORG_PROGGEN_DEDUPE_DATAHOLDING_DATAVARIANT_H
00009 #define ORG_PROGGEN_DEDUPE_DATAHOLDING_DATAVARIANT_H
00010 
00011 #include <typeinfo>
00012 #include <string>
00013 #include <iostream>
00014 
00015 namespace Dedupe
00016 {
00021   namespace Dataholding
00022   {
00028     namespace Variant
00029     {
00035       class Data
00036       {
00037         public:
00038         virtual const std::type_info & GetType() const = 0;
00039         virtual ~Data() {} ;
00040       };
00041 
00046       template<class T> class DataVariant : public Data
00047       {
00048         T HoldedData;
00049 
00050         public:
00051 
00056         DataVariant( T data ) : HoldedData( data ) {};
00057 
00062         virtual const std::type_info & GetType() const
00063         {
00064           const std::type_info &ti = typeid( HoldedData );
00065 
00066           return ti;
00067         }
00068 
00072         T GetData() const
00073         {
00074           return HoldedData;
00075         }
00076 
00080         DataVariant( const DataVariant& origin )
00081         : HoldedData( origin.HoldedData ) {}
00082 
00086         DataVariant& operator=( const DataVariant& origin)
00087         {
00088           if( this == &origin ) return *this;
00089           HoldedData = origin.HoldedData;
00090           return *this;
00091         }
00092 
00093       };
00094 
00098       enum CastResults
00099       {
00100         CastOK,             
00101         DifferentDatatypes, 
00102         CouldNotCast,       
00103         BadCastException,   
00104         NULLPointerGiven    
00105       };
00106 
00114       template<class T> CastResults CastDataVariant( Data *incoming, T &returning )
00115       {
00116         //Make sure, the incoming pointer is not NULL
00117         if( incoming == NULL ) return NULLPointerGiven;
00118 
00119         /*Make sure, holded data and the
00120           returning variable are frome the same type
00121         */
00122         if( incoming->GetType() != typeid( T ) ) return DifferentDatatypes;
00123 
00124         //Define the pointer here, cause the cast is in a different scope
00125         DataVariant<T> *ptr = NULL;
00126 
00127         //catch bad_cast exceptions and return an BadCastExecption error
00128         try
00129         {
00130           ptr = dynamic_cast
00131           <Dedupe::Dataholding::Variant::DataVariant<T> *>(incoming);
00132         }
00133         catch (std::bad_cast& bc)
00134         {
00135           return BadCastException;
00136         }
00137 
00138         if( ptr == NULL ) return CouldNotCast;
00139         //write the data to the returning varible
00140         returning = ptr->GetData();
00141 
00142         return CastOK;
00143       }
00144     }
00145   }
00146 }
00147 #endif

Generated on Thu Nov 10 2011 21:16:26 for Dedupe by  doxygen 1.7.1