CLAM-Development  1.4.0
ProcessingDefinitionAdapter.cxx
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2001-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 
24 #include "Assert.hxx"
25 #include "Processing.hxx"
26 #include "ProcessingConfig.hxx"
27 #include "ProcessingFactory.hxx"
28 #include "XMLAdapter.hxx"
29 #include "XmlStorageErr.hxx"
30 
31 namespace CLAM
32 {
33  ProcessingDefinitionAdapter::ProcessingDefinitionAdapter( Processing * adaptee, const std::string & name, const std::string & position, const std::string & size )
34  : mAdaptee(adaptee)
35  , mConfiguration(0)
36  , mName(name)
37  , mPosition(position)
38  , mSize(size)
39  {
40  }
41 
43  {
44  if (mConfiguration) delete mConfiguration;
45  }
46 
48  {
49  Text nameCopy(mName);
50  XMLAdapter<Text> nameAdapter( nameCopy, "id");
51  Text className(mAdaptee->GetClassName());
52  XMLAdapter<Text> classNameAdapter( className, "type");
53  store.Store(nameAdapter);
54  store.Store(classNameAdapter);
55  if (mPosition!="") // check if position is defined (QPoint values are integers)
56  {
57  Text positionCopy(mPosition);
58  XMLAdapter<Text> positionAdapter (positionCopy, "position");
59  store.Store(positionAdapter);
60  }
61  if (mSize!="") // check if size is defined
62  {
63  Text sizeCopy(mSize);
64  XMLAdapter<Text> sizeAdapter (sizeCopy, "size");
65  store.Store(sizeAdapter);
66  }
67  XMLComponentAdapter configAdapter((ProcessingConfig&)mAdaptee->GetConfig());
68  store.Store(configAdapter);
69  }
70 
72  {
73  XMLAdapter<Text> nameAdapter( mName, "id");
74  if (!store.Load(nameAdapter))
75  throw XmlStorageErr("Processing without id attribute");
76  Text className;
77  XMLAdapter<Text> classNameAdapter( className, "type");
78  if (!store.Load(classNameAdapter))
79  throw XmlStorageErr("Processing without type attribute");
80 
81  XMLAdapter<Text> positionAdapter (mPosition, "position");
82  store.Load(positionAdapter);
83  XMLAdapter<Text> sizeAdapter (mSize, "size");
84  store.Load(sizeAdapter);
85 
86  try
87  {
88  mAdaptee = ProcessingFactory::GetInstance().CreateSafe(className);
89  } catch (ErrFactory & e)
90  {
91  std::string msg =
92  "Error creating a processing object of type '" +
93  className + "' which is not available.";
94  throw XmlStorageErr(msg);
95  }
96  Component * cfg = mAdaptee->GetConfig().DeepCopy();
97  XMLComponentAdapter configAdapter( *cfg );
98  store.Load(configAdapter);
99  mConfiguration = dynamic_cast<ProcessingConfig *>(cfg);
100  CLAM_ASSERT(mConfiguration, "Retrieved config fails to cast as a ProcessingConfig");
101  }
102 } // namespace CLAM
103