OpenASIP 2.2
Loading...
Searching...
No Matches
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
43using std::string;
44
45const string OperationBehaviorLoader::CREATE_FUNC = "createOpBehavior_";
46const 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 */
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 */
135void
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}
#define __func__
find Finds info of the inner loops in the false
void setCause(const Exception &cause)
Definition Exception.cc:75
static NullOperationModule & instance()
static const std::string CREATE_FUNC
The name of the creation function in dynamic module.
DestructionMap destructors_
Container of all destruction functions of behavioral models.
BehaviorMap behaviors_
Container of all loaded operation behavior models.
static const std::string DELETE_FUNC
The name of the deletion function in dynamic module.
OperationBehavior & importBehavior(const Operation &parent)
PluginTools tools_
PluginTools for loading dynamic modules.
OperationBehaviorLoader(OperationIndex &index)
virtual TCEString name() const
Definition Operation.cc:93
void importSymbol(const std::string &symbolName, T *&target, const std::string &module)
void unregisterAllModules()
static std::string stringToUpper(const std::string &source)