CLAM-Development  1.4.0
Polymorphic.hxx
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2004 MUSIC TECHNOLOGY GROUP (MTG)
3  * UNIVERSITAT POMPEU FABRA
4  *
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19  *
20  */
21 
22 #ifndef _Polymorphic_hxx_
23 #define _Polymorphic_hxx_
24 
25 #include "Component.hxx"
26 #include "XMLStorage.hxx"
27 #include "XMLAdapter.hxx"
28 #include <string>
29 
30 // TODO
31 // Memory management
32 // Pointer, Reference or Get interface?
33 // DT required interface
34 // Change Dummys
35 // Adopt function
36 
37 namespace CLAM
38 {
49  template <typename FactoryType>
50  class Polymorphic : public Component
51  {
52  public:
53  typedef FactoryType Factory;
55  private:
56  Abstract * mAdaptee;
57 
58  public:
60  {
61  mAdaptee = 0;
62  }
63 
64  Polymorphic(const std::string & concreteClassName)
65  {
66  mAdaptee = Factory::GetInstance().Create(concreteClassName);
67  }
68 
70  {
71  mAdaptee = & toCopy;
72  }
73 
75  {
76  if (mAdaptee) delete mAdaptee;
77  }
78 
80  {
81  mAdaptee = & toCopy;
82  }
83 
85  {
86  CLAM_ASSERT(mAdaptee, "Derreferencing a null polymorph pointer");
87  return *mAdaptee;
88  }
89 
90  const Abstract & Get() const
91  {
92  CLAM_ASSERT(mAdaptee, "Derreferencing a null polymorph pointer");
93  return *mAdaptee;
94  }
95 
96  void Set(Abstract * pointee)
97  {
98  mAdaptee = pointee;
99  }
100 
101  const char * GetClassName() const
102  {
103  return "Polymorphic";
104  }
105 
106  operator Abstract&()
107  {
108  return *mAdaptee;
109  }
110 
111  operator Abstract* ()
112  {
113  return mAdaptee;
114  }
115 
116  operator const Abstract&() const
117  {
118  return *mAdaptee;
119  }
120 
121 
122  void StoreOn(Storage & storage) const
123  {
124  // TODO case nill
125  std::string className = mAdaptee->GetClassName();
126  XMLAdapter<std::string> typeAttribute(className, "type", false);
127  storage.Store(typeAttribute);
128  mAdaptee->StoreOn(storage);
129  }
130 
131  void LoadFrom(Storage & storage)
132  {
133  // TODO case nill
134  // TODO case incorrect type
135  // TODO case mAdaptee!=null
136  std::string className;
137  XMLAdapter<std::string> typeAttribute(className, "type", false);
138  storage.Load(typeAttribute);
139  mAdaptee = Factory::GetInstance().Create(className);
140  mAdaptee->LoadFrom(storage);
141  }
142 
143 
144  };
145 }
146 #endif//_Polymorphic_hxx_
147