OpenASIP  2.0
CostEstimationPluginRegistry.icc
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 CostEstimationPluginRegistry.icc
26  *
27  * Implementations of templated CostEstimationPluginRegistry class
28  *
29  * @author Pekka Jääskeläinen 2005 (pjaaskel-no.spam-cs.tut.fi)
30  * @note rating: red
31  */
32 
33 #include "boost/format.hpp"
34 
35 #include "AssocTools.hh"
36 #include "FileSystem.hh"
37 #include "Exception.hh"
38 #include "Application.hh"
39 
40 namespace CostEstimator {
41 
42 /// the prefix of the function in the plugin file that is used to create new
43 /// estimator plugin instances
44 const std::string PLUGIN_CREATOR_FUNCTION_PREFIX =
45 "create_estimator_plugin_";
46 
47 /**
48  * Constructor.
49  */
50 template <typename T>
51 CostEstimationPluginRegistry<T>::CostEstimationPluginRegistry() {
52  pluginTools_.addSearchPath(FileSystem::currentWorkingDir());
53 }
54 
55 /**
56  * Destructor.
57  */
58 template <typename T>
59 CostEstimationPluginRegistry<T>::~CostEstimationPluginRegistry() {
60  /// @todo call the deletion function in the plugin instead, just in case
61  /// delete is overloaded in the plugin
62  AssocTools::deleteAllValues(registry_);
63 }
64 
65 /**
66  * Returns the plugin that is stored in the given path.
67  *
68  * In case the plugin is not found, tries to open it.
69  *
70  * @param pluginFileName The path of the plugin.
71  * @param pluginName The (class) name of the plugin.
72  * @param sourceHDB The HDB from which the plugin should fetch its data.
73  * @param pluginID The id of the plugin in the sourceHDB.
74  * @return The plugin.
75  * @exception IOException In case an I/O error occured.
76  * @exception FileNotfound In case the plugin file was not found.
77  * @exception DynamicLibraryException In case the plugin interface is broken.
78  * @exception WrongSubClass In case the plugin is of wrong estimation plugin
79  * type.
80  */
81 template <typename T>
82 T&
83 CostEstimationPluginRegistry<T>::plugin(
84  const std::string& pluginFileName,
85  const std::string& pluginName)
86  {
87 
88  T* pluginInstance = NULL;
89 
90  if (AssocTools::containsKey(registry_, pluginFileName)) {
91  pluginInstance = registry_[pluginFileName];
92  } else {
93  /// try to load the plugin
94  try {
95  std::string pluginFile = FileSystem::findFileInSearchPaths(
96  Environment::estimatorPluginPaths(), pluginFileName);
97  T* (*creator)();
98  pluginTools_.importSymbol(
99  PLUGIN_CREATOR_FUNCTION_PREFIX + pluginName, creator,
100  pluginFile);
101  pluginInstance = creator();
102  } catch (const FileNotFound& e) {
103  throw FileNotFound(
104  __FILE__, __LINE__, __func__,
105  (boost::format("Plugin file '%s' not found.") %
106  pluginFileName).str());
107  } catch (const Exception& e) {
108  throw DynamicLibraryException(
109  __FILE__, __LINE__, __func__,
110  std::string("Error while trying to import plugin. ") +
111  e.errorMessage());
112  }
113  registry_[pluginFileName] = pluginInstance;
114  }
115 
116  return *pluginInstance;
117 }
118 
119 }