1 ////////////////////////////////////////////////////////////
\r
3 // Pluma - Plug-in Management Framework
\r
4 // Copyright (C) 2010-2012 Gil Costa (gsaurus@gmail.com)
\r
6 // This software is provided 'as-is', without any express or implied warranty.
\r
7 // In no event will the authors be held liable for any damages arising from the use of this software.
\r
9 // Permission is granted to anyone to use this software for any purpose,
\r
10 // including commercial applications, and to alter it and redistribute it freely,
\r
11 // subject to the following restrictions:
\r
13 // 1. The origin of this software must not be misrepresented;
\r
14 // you must not claim that you wrote the original software.
\r
15 // If you use this software in a product, an acknowledgment
\r
16 // in the product documentation would be appreciated but is not required.
\r
18 // 2. Altered source versions must be plainly marked as such,
\r
19 // and must not be misrepresented as being the original software.
\r
21 // 3. This notice may not be removed or altered from any source distribution.
\r
23 ////////////////////////////////////////////////////////////
\r
26 ////////////////////////////////////////////////////////////
\r
28 ////////////////////////////////////////////////////////////
\r
29 #include <Pluma/Host.hpp>
\r
31 #include <stdexcept>
\r
36 ////////////////////////////////////////////////////////////
\r
42 ////////////////////////////////////////////////////////////
\r
43 bool Host::add(Provider* provider){
\r
44 if (provider == NULL){
\r
45 //fprintf(stderr, "Trying to add a null provider.\n");
\r
46 throw std::runtime_error( "Trying to add a null provider." );
\r
49 if (!validateProvider(provider)){
\r
53 addRequests[ provider->plumaGetType() ].push_back(provider);
\r
58 ////////////////////////////////////////////////////////////
\r
65 ////////////////////////////////////////////////////////////
\r
66 void Host::clearProviders(){
\r
67 ProvidersMap::iterator it;
\r
68 for (it = knownTypes.begin() ; it != knownTypes.end() ; ++it){
\r
69 std::list<Provider*>& providers = it->second.providers;
\r
70 std::list<Provider*>::iterator provIt;
\r
71 for (provIt = providers.begin() ; provIt != providers.end() ; ++provIt){
\r
74 std::list<Provider*>().swap(providers);
\r
79 ////////////////////////////////////////////////////////////
\r
80 bool Host::knows(const std::string& type) const{
\r
81 return knownTypes.find(type) != knownTypes.end();
\r
85 ////////////////////////////////////////////////////////////
\r
86 unsigned int Host::getVersion(const std::string& type) const{
\r
87 ProvidersMap::const_iterator it = knownTypes.find(type);
\r
88 if (it != knownTypes.end())
\r
89 return it->second.version;
\r
94 ////////////////////////////////////////////////////////////
\r
95 unsigned int Host::getLowestVersion(const std::string& type) const{
\r
96 ProvidersMap::const_iterator it = knownTypes.find(type);
\r
97 if (it != knownTypes.end())
\r
98 return it->second.lowestVersion;
\r
103 ////////////////////////////////////////////////////////////
\r
104 void Host::registerType(const std::string& type, unsigned int version, unsigned int lowestVersion){
\r
107 pi.version = version;
\r
108 pi.lowestVersion = lowestVersion;
\r
109 knownTypes[type] = pi;
\r
114 ////////////////////////////////////////////////////////////
\r
115 const std::list<Provider*>* Host::getProviders(const std::string& type) const{
\r
116 ProvidersMap::const_iterator it = knownTypes.find(type);
\r
117 if (it != knownTypes.end())
\r
118 return &it->second.providers;
\r
123 ////////////////////////////////////////////////////////////
\r
124 bool Host::validateProvider(Provider* provider) const{
\r
125 const std::string& type = provider->plumaGetType();
\r
126 if ( !knows(type) ){
\r
127 // fprintf(stderr, "%s provider type isn't registered.\n", type.c_str());
\r
128 throw std::runtime_error(
\r
129 type + std::string( " provider type isn't registered." )
\r
133 if (!provider->isCompatible(*this)){
\r
134 // fprintf(stderr, "Incompatible %s provider version.\n", type.c_str());
\r
135 throw std::runtime_error(
\r
136 std::string( "Incompatible " ) +
\r
138 std::string( " provider version." )
\r
146 ////////////////////////////////////////////////////////////
\r
147 bool Host::registerProvider(Provider* provider){
\r
148 if (!validateProvider(provider)){
\r
152 knownTypes[ provider->plumaGetType() ].providers.push_back(provider);
\r
157 ////////////////////////////////////////////////////////////
\r
158 void Host::cancelAddictions(){
\r
159 TempProvidersMap::iterator it;
\r
160 for( it = addRequests.begin() ; it != addRequests.end() ; ++it){
\r
161 std::list<Provider*> lst = it->second;
\r
162 std::list<Provider*>::iterator providerIt;
\r
163 for (providerIt = lst.begin() ; providerIt != lst.end() ; ++providerIt){
\r
164 delete *providerIt;
\r
168 TempProvidersMap().swap(addRequests);
\r
172 ////////////////////////////////////////////////////////////
\r
173 bool Host::confirmAddictions(){
\r
174 if (addRequests.empty()) return false;
\r
175 TempProvidersMap::iterator it;
\r
176 for( it = addRequests.begin() ; it != addRequests.end() ; ++it){
\r
177 std::list<Provider*> lst = it->second;
\r
178 std::list<Provider*>::iterator providerIt;
\r
179 for (providerIt = lst.begin() ; providerIt != lst.end() ; ++providerIt){
\r
180 knownTypes[it->first].providers.push_back(*providerIt);
\r
184 TempProvidersMap().swap(addRequests);
\r
189 } //namespace pluma
\r