Polymorphic.hxx
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
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
00031
00032
00033
00034
00035
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
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
00134
00135
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