Polymorphic.hxx

Go to the documentation of this file.
00001 /*
00002  * Copyright (c) 2004 MUSIC TECHNOLOGY GROUP (MTG)
00003  *                         UNIVERSITAT POMPEU FABRA
00004  *
00005  *
00006  * This program is free software; you can redistribute it and/or modify
00007  * it under the terms of the GNU General Public License as published by
00008  * the Free Software Foundation; either version 2 of the License, or
00009  * (at your option) any later version.
00010  *
00011  * This program is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  * GNU General Public License for more details.
00015  *
00016  * You should have received a copy of the GNU General Public License
00017  * along with this program; if not, write to the Free Software
00018  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00019  *
00020  */
00021 
00022 #ifndef _Polymorphic_hxx_
00023 #define _Polymorphic_hxx_
00024 
00025 #include "Component.hxx"
00026 #include "XMLStorage.hxx"
00027 #include "XMLAdapter.hxx"
00028 #include <string>
00029 
00030 // TODO
00031 // Memory management
00032 // Pointer, Reference or Get interface?
00033 // DT required interface
00034 // Change Dummys
00035 // Adopt function
00036 
00037 namespace CLAM
00038 {
00049         template <typename FactoryType>
00050         class Polymorphic : public Component
00051         {
00052         public:
00053                 typedef FactoryType Factory;
00054                 typedef typename Factory::AbstractProduct Abstract;
00055         private:
00056                 Abstract * mAdaptee;
00057 
00058         public:
00059                 Polymorphic()
00060                 {
00061                         mAdaptee = 0;
00062                 }
00063 
00064                 Polymorphic(const std::string & concreteClassName)
00065                 {
00066                         mAdaptee = Factory::GetInstance().Create(concreteClassName);
00067                 }
00068 
00069                 Polymorphic(Abstract & toCopy)
00070                 {
00071                         mAdaptee = & toCopy;
00072                 }
00073 
00074                 ~Polymorphic()
00075                 {
00076                         if (mAdaptee) delete mAdaptee;
00077                 }
00078 
00079                 Polymorphic & operator=(Abstract & toCopy)
00080                 {
00081                         mAdaptee = & toCopy;
00082                 }
00083 
00084                 Abstract & Get()
00085                 {
00086                         CLAM_ASSERT(mAdaptee, "Derreferencing a null polymorph pointer");
00087                         return *mAdaptee;
00088                 }
00089 
00090                 const Abstract & Get() const
00091                 {
00092                         CLAM_ASSERT(mAdaptee, "Derreferencing a null polymorph pointer");
00093                         return *mAdaptee;
00094                 }
00095 
00096                 void Set(Abstract * pointee)
00097                 {
00098                         mAdaptee = pointee;
00099                 }
00100 
00101                 const char * GetClassName() const 
00102                 {
00103                         return "Polymorphic";
00104                 }
00105 
00106                 operator Abstract&()
00107                 {
00108                         return *mAdaptee;
00109                 }
00110 
00111                 operator Abstract* ()
00112                 {
00113                         return mAdaptee;
00114                 }
00115 
00116                 operator const Abstract&() const 
00117                 {
00118                         return *mAdaptee;
00119                 }
00120 
00121 
00122                 void StoreOn(Storage & storage) const
00123                 {
00124                         // TODO case nill
00125                         std::string className = mAdaptee->GetClassName();
00126                         XMLAdapter<std::string> typeAttribute(className, "type", false);
00127                         storage.Store(typeAttribute);
00128                         mAdaptee->StoreOn(storage);
00129                 }
00130 
00131                 void LoadFrom(Storage & storage)
00132                 {
00133                         // TODO case nill
00134                         // TODO case incorrect type
00135                         // TODO case mAdaptee!=null
00136                         std::string className;
00137                         XMLAdapter<std::string> typeAttribute(className, "type", false);
00138                         storage.Load(typeAttribute);
00139                         mAdaptee = Factory::GetInstance().Create(className);
00140                         mAdaptee->LoadFrom(storage);
00141                 }
00142                 
00143                 
00144         };
00145 }
00146 #endif//_Polymorphic_hxx_
00147 
Generated by  doxygen 1.6.3