OpenASIP  2.0
OperationPropertyLoader.cc
Go to the documentation of this file.
1 /*
2  Copyright (c) 2002-2009 Tampere University.
3 
4  This file is part of TTA-Based Codesign Environment (TCE).
5 
6  Permission is hereby granted, free of charge, to any person obtaining a
7  copy of this software and associated documentation files (the "Software"),
8  to deal in the Software without restriction, including without limitation
9  the rights to use, copy, modify, merge, publish, distribute, sublicense,
10  and/or sell copies of the Software, and to permit persons to whom the
11  Software is furnished to do so, subject to the following conditions:
12 
13  The above copyright notice and this permission notice shall be included in
14  all copies or substantial portions of the Software.
15 
16  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19  THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21  FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22  DEALINGS IN THE SOFTWARE.
23  */
24 /**
25  * @file OperationPropertyLoader.cc
26  *
27  * Definition of OperationPropertyLoader class.
28  *
29  * @author Jussi Nykänen 2004 (nykanen-no.spam-cs.tut.fi)
30  * @note rating: yellow
31  * @note reviewed 7 September 2004 by pj, jn, jm, ao
32  */
33 
34 #include <string>
35 #include <vector>
36 
38 #include "SequenceTools.hh"
39 #include "OperationModule.hh"
40 #include "Operation.hh"
41 #include "TCEString.hh"
42 #include "ObjectState.hh"
43 
44 using std::string;
45 using std::vector;
46 
47 /**
48  * Constructor.
49  */
51 }
52 
53 /**
54  * Destructor.
55  */
57  MapIter it = operations_.begin();
58  while (it != operations_.end()) {
59  SequenceTools::deleteAllItems((*it).second);
60  it++;
61  }
62 }
63 
64 /**
65  * Loads the properties of an operation.
66  *
67  * If operation properties has not yet been loaded, it is done first.
68  *
69  * @param operation Operation which properties are loaded and updated.
70  * @param module Module where properties are loaded from.
71  * @exception InstanceNotFound If reading of XML fails, if Operation fails to
72  * load its state, or if properties are not found
73  * at all.
74  */
75 void
77  Operation& operation, const OperationModule& module) {
78  MapIter it = operations_.find(module.propertiesModule());
79  if (it == operations_.end()) {
80  // properties are not yet read, let's do it first
81  loadModule(module);
82  it = operations_.find(module.propertiesModule());
83  }
84 
85  const vector<ObjectState*> ops = (*it).second;
86  bool operationFound = false;
87 
88  for (unsigned int i = 0; i < ops.size(); i++) {
89  if (ops[i]->stringAttribute(Operation::OPRN_NAME) ==
90  operation.name()) {
91 
92  operationFound = true;
93  try {
94  operation.loadState(ops[i]);
95  } catch (const ObjectStateLoadingException& o) {
96  string msg = "Problems when loading the state of the Object: "
97  + o.errorMessage();
98  throw InstanceNotFound(__FILE__, __LINE__, __func__, msg);
99  }
100  break;
101  }
102  }
103 
104  if (!operationFound) {
105  string msg = string("Properties for operation ") + operation.name() +
106  " not found";
107  throw InstanceNotFound(__FILE__, __LINE__, __func__, msg);
108  }
109 }
110 
111 /**
112  * Loads the module.
113  *
114  * Saves its operations as an ObjectState objects.
115  *
116  * @param module The OperationModule.
117  * @exception InstanceNotFound If loading the module fails.
118  */
119 void
122  ObjectState* root = NULL;
123  try {
124  root = serializer_.readState();
125  } catch (const SerializerException& s) {
126  string msg = "Problems when reading the XML file: " + s.errorMessage();
127  throw InstanceNotFound(__FILE__, __LINE__, __func__, msg);
128  }
129  vector<ObjectState*> ops;
130  for (int i = 0; i < root->childCount(); i++) {
131  ObjectState* child = new ObjectState(*root->child(i));
132  ops.push_back(child);
133  }
134  delete root;
135  operations_.insert(ValueType(module.propertiesModule(), ops));
136 }
OperationPropertyLoader::loadModule
void loadModule(const OperationModule &module)
Definition: OperationPropertyLoader.cc:120
OperationPropertyLoader::loadOperationProperties
void loadOperationProperties(Operation &operation, const OperationModule &module)
Definition: OperationPropertyLoader.cc:76
ObjectStateLoadingException
Definition: Exception.hh:551
OperationPropertyLoader.hh
SequenceTools.hh
Operation::OPRN_NAME
static const char * OPRN_NAME
Object state name for name.
Definition: Operation.hh:67
ObjectState
Definition: ObjectState.hh:59
Operation::name
virtual TCEString name() const
Definition: Operation.cc:93
TCEString.hh
SequenceTools::deleteAllItems
static void deleteAllItems(SequenceType &aSequence)
OperationModule::propertiesModule
virtual std::string propertiesModule() const
Definition: OperationModule.cc:121
OperationPropertyLoader::serializer_
OperationSerializer serializer_
Serializer instance.
Definition: OperationPropertyLoader.hh:76
OperationSerializer::readState
virtual ObjectState * readState()
Definition: OperationSerializer.cc:118
__func__
#define __func__
Definition: Application.hh:67
ObjectState.hh
Operation::loadState
virtual void loadState(const ObjectState *state)
Definition: Operation.cc:480
ObjectState::child
ObjectState * child(int index) const
Definition: ObjectState.cc:471
Operation.hh
ObjectState::childCount
int childCount() const
OperationPropertyLoader::MapIter
ObjectStateCache::iterator MapIter
Iterator for map containing already read ObjectState trees.
Definition: OperationPropertyLoader.hh:64
SerializerException
Definition: Exception.hh:675
OperationSerializer::setSourceFile
void setSourceFile(const std::string &filename)
Definition: OperationSerializer.cc:536
Operation
Definition: Operation.hh:59
Exception::errorMessage
std::string errorMessage() const
Definition: Exception.cc:123
OperationPropertyLoader::OperationPropertyLoader
OperationPropertyLoader()
Definition: OperationPropertyLoader.cc:50
OperationModule
Definition: OperationModule.hh:46
OperationPropertyLoader::~OperationPropertyLoader
virtual ~OperationPropertyLoader()
Definition: OperationPropertyLoader.cc:56
OperationPropertyLoader::ValueType
ObjectStateCache::value_type ValueType
value_type for map containing already read ObjectState trees.
Definition: OperationPropertyLoader.hh:66
OperationModule.hh
InstanceNotFound
Definition: Exception.hh:304
OperationPropertyLoader::operations_
ObjectStateCache operations_
Cache for already read ObjectState trees.
Definition: OperationPropertyLoader.hh:78