OpenASIP  2.0
OperationBehaviorLoader.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 OperationBehaviorLoader.cc
26  *
27  * Definition of OperationBehaviorLoader class.
28  *
29  * @author Jussi Nykänen 2004 (nykanen-no.spam-cs.tut.fi)
30  * @note rating: yellow
31  * @note reviewed 19 August 2004 by pj, jn, ao, ac
32  */
33 
35 #include "Operation.hh"
36 #include "OperationBehavior.hh"
37 #include "OperationIndex.hh"
38 #include "FileSystem.hh"
39 #include "StringTools.hh"
40 #include "OperationModule.hh"
41 #include "TCEString.hh"
42 
43 using std::string;
44 
45 const string OperationBehaviorLoader::CREATE_FUNC = "createOpBehavior_";
46 const string OperationBehaviorLoader::DELETE_FUNC = "deleteOpBehavior_";
47 
48 /**
49  * Constructor.
50  *
51  * OperationBehavior uses OperationIndex for getting the right modules.
52  *
53  * @param index OperationIndex.
54  *
55  * @note The PluginTools instance should load the behavior modules in the LOCAL
56  * mode to enable overriding behavior definitions from later definitions in
57  * the search path. E.g., if base.opb defines ADD, a custom.opb can redefine
58  * it. If the symbols are made global then the latter symbol is not reloaded
59  * but the one from base.opb reused.
60  */
62  index_(index), tools_(false, true) {
63 }
64 
65 /**
66  * Destructor.
67  *
68  * Deletes all the loader operation behavior models.
69  */
71  freeBehavior();
72 }
73 
74 /**
75  * Imports the behavior model of an operation.
76  *
77  * First, the module that contains the given operation is obtained from
78  * OperationIndex. A pointer to appropriate create function is obtained and
79  * used to create OperationBehavior. Also pointer to appropriate destruction
80  * function is obtained and stored.
81  *
82  * @param parent The operation that owns the loaded behavior.
83  * @return Operation behavior model of the operation.
84  * @exception DynamicLibraryException If an error occurs while accessing the
85  * dynamic module.
86  * @exception FileNotFound If the .opb was not found.
87  * @exception SymbolNotFound If the constructor/destructor functions could
88  * not be imported from the .opb.
89  */
92  string name = parent.name();
93  // if behavior was already created, use it
94  BehaviorMap::iterator iter = behaviors_.find(name);
95  if (iter != behaviors_.end()) {
96  return *((*iter).second);
97  }
98 
99  try {
100  OperationModule& module = index_.moduleOf(name);
101  if (&module == &NullOperationModule::instance()) {
102  string msg = "Module for operation " + name + " not found";
103  throw InstanceNotFound(__FILE__, __LINE__, __func__, msg);
104  }
105 
106  string creatorName = CREATE_FUNC + StringTools::stringToUpper(name);
107  string destructorName = DELETE_FUNC + StringTools::stringToUpper(name);
108  string modName = module.behaviorModule();
109  OperationBehavior* (*behaviorCreator)(const Operation&);
110  void (*behaviorDestructor)(OperationBehavior*);
111  tools_.importSymbol(creatorName, behaviorCreator, modName);
112  tools_.importSymbol(destructorName, behaviorDestructor, modName);
113  OperationBehavior* behavior = behaviorCreator(parent);
114  behaviors_[name] = behavior;
115  destructors_[behavior] = behaviorDestructor;
116  return *behavior;
117 
118  } catch (const FileNotFound& e) {
119  throw e;
120  } catch (const SymbolNotFound& e) {
121  throw e;
122  } catch (const Exception& e) {
123  string msg =
124  std::string("Behavior definition for ") + parent.name() +
125  " could not be loaded.";
126  DynamicLibraryException error(__FILE__, __LINE__, __func__, msg);
127  error.setCause(e);
128  throw error;
129  }
130 }
131 
132 /**
133  * Frees all the imported behavior models of the operations.
134  */
135 void
137  DestructionMap::iterator iter = destructors_.begin();
138  while (iter != destructors_.end()) {
139  void (*behaviorDestructor)(OperationBehavior*) = (*iter).second;
140  behaviorDestructor((*iter).first);
141  iter++;
142  }
143  destructors_.clear();
144  behaviors_.clear();
146 }
OperationBehaviorLoader::importBehavior
OperationBehavior & importBehavior(const Operation &parent)
Definition: OperationBehaviorLoader.cc:91
FileSystem.hh
FileNotFound
Definition: Exception.hh:224
OperationBehaviorLoader::tools_
PluginTools tools_
PluginTools for loading dynamic modules.
Definition: OperationBehaviorLoader.hh:83
OperationBehaviorLoader::behaviors_
BehaviorMap behaviors_
Container of all loaded operation behavior models.
Definition: OperationBehaviorLoader.hh:85
OperationModule::behaviorModule
virtual std::string behaviorModule() const
Definition: OperationModule.cc:105
OperationBehaviorLoader::~OperationBehaviorLoader
virtual ~OperationBehaviorLoader()
Definition: OperationBehaviorLoader.cc:70
Exception::setCause
void setCause(const Exception &cause)
Definition: Exception.cc:75
OperationBehaviorLoader.hh
Operation::name
virtual TCEString name() const
Definition: Operation.cc:93
TCEString.hh
StringTools::stringToUpper
static std::string stringToUpper(const std::string &source)
Definition: StringTools.cc:143
StringTools.hh
PluginTools::importSymbol
void importSymbol(const std::string &symbolName, T *&target, const std::string &module)
SymbolNotFound
Definition: Exception.hh:623
OperationBehaviorLoader::index_
OperationIndex & index_
Indexed table of all modules and operations accessible for this loader.
Definition: OperationBehaviorLoader.hh:81
OperationIndex.hh
__func__
#define __func__
Definition: Application.hh:67
Operation.hh
Exception
Definition: Exception.hh:54
Operation
Definition: Operation.hh:59
OperationBehavior.hh
OperationBehavior
Definition: OperationBehavior.hh:53
OperationBehaviorLoader::OperationBehaviorLoader
OperationBehaviorLoader(OperationIndex &index)
Definition: OperationBehaviorLoader.cc:61
false
find Finds info of the inner loops in the false
Definition: InnerLoopFinder.cc:81
OperationModule
Definition: OperationModule.hh:46
OperationBehaviorLoader::freeBehavior
void freeBehavior()
Definition: OperationBehaviorLoader.cc:136
OperationIndex
Definition: OperationIndex.hh:58
OperationBehaviorLoader::CREATE_FUNC
static const std::string CREATE_FUNC
The name of the creation function in dynamic module.
Definition: OperationBehaviorLoader.hh:76
OperationBehaviorLoader::DELETE_FUNC
static const std::string DELETE_FUNC
The name of the deletion function in dynamic module.
Definition: OperationBehaviorLoader.hh:78
NullOperationModule::instance
static NullOperationModule & instance()
DynamicLibraryException
Definition: Exception.hh:588
OperationIndex::moduleOf
OperationModule & moduleOf(const std::string &name)
Definition: OperationIndex.cc:315
OperationModule.hh
InstanceNotFound
Definition: Exception.hh:304
PluginTools::unregisterAllModules
void unregisterAllModules()
Definition: PluginTools.cc:236
OperationBehaviorLoader::destructors_
DestructionMap destructors_
Container of all destruction functions of behavioral models.
Definition: OperationBehaviorLoader.hh:87