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
35 ////////////////////////////////////////////////////////////
\r
41 ////////////////////////////////////////////////////////////
\r
42 bool Host::add(Provider* provider){
\r
43 if (provider == NULL){
\r
44 fprintf(stderr, "Trying to add a null provider.\n");
\r
47 if (!validateProvider(provider)){
\r
51 addRequests[ provider->plumaGetType() ].push_back(provider);
\r
56 ////////////////////////////////////////////////////////////
\r
63 ////////////////////////////////////////////////////////////
\r
64 void Host::clearProviders(){
\r
65 ProvidersMap::iterator it;
\r
66 for (it = knownTypes.begin() ; it != knownTypes.end() ; ++it){
\r
67 std::list<Provider*>& providers = it->second.providers;
\r
68 std::list<Provider*>::iterator provIt;
\r
69 for (provIt = providers.begin() ; provIt != providers.end() ; ++provIt){
\r
72 std::list<Provider*>().swap(providers);
\r
77 ////////////////////////////////////////////////////////////
\r
78 bool Host::knows(const std::string& type) const{
\r
79 return knownTypes.find(type) != knownTypes.end();
\r
83 ////////////////////////////////////////////////////////////
\r
84 unsigned int Host::getVersion(const std::string& type) const{
\r
85 ProvidersMap::const_iterator it = knownTypes.find(type);
\r
86 if (it != knownTypes.end())
\r
87 return it->second.version;
\r
92 ////////////////////////////////////////////////////////////
\r
93 unsigned int Host::getLowestVersion(const std::string& type) const{
\r
94 ProvidersMap::const_iterator it = knownTypes.find(type);
\r
95 if (it != knownTypes.end())
\r
96 return it->second.lowestVersion;
\r
101 ////////////////////////////////////////////////////////////
\r
102 void Host::registerType(const std::string& type, unsigned int version, unsigned int lowestVersion){
\r
105 pi.version = version;
\r
106 pi.lowestVersion = lowestVersion;
\r
107 knownTypes[type] = pi;
\r
112 ////////////////////////////////////////////////////////////
\r
113 const std::list<Provider*>* Host::getProviders(const std::string& type) const{
\r
114 ProvidersMap::const_iterator it = knownTypes.find(type);
\r
115 if (it != knownTypes.end())
\r
116 return &it->second.providers;
\r
121 ////////////////////////////////////////////////////////////
\r
122 bool Host::validateProvider(Provider* provider) const{
\r
123 const std::string& type = provider->plumaGetType();
\r
124 if ( !knows(type) ){
\r
125 fprintf(stderr, "%s provider type isn't registered.\n", type.c_str());
\r
128 if (!provider->isCompatible(*this)){
\r
129 fprintf(stderr, "Incompatible %s provider version.\n", type.c_str());
\r
136 ////////////////////////////////////////////////////////////
\r
137 bool Host::registerProvider(Provider* provider){
\r
138 if (!validateProvider(provider)){
\r
142 knownTypes[ provider->plumaGetType() ].providers.push_back(provider);
\r
147 ////////////////////////////////////////////////////////////
\r
148 void Host::cancelAddictions(){
\r
149 TempProvidersMap::iterator it;
\r
150 for( it = addRequests.begin() ; it != addRequests.end() ; ++it){
\r
151 std::list<Provider*> lst = it->second;
\r
152 std::list<Provider*>::iterator providerIt;
\r
153 for (providerIt = lst.begin() ; providerIt != lst.end() ; ++providerIt){
\r
154 delete *providerIt;
\r
158 TempProvidersMap().swap(addRequests);
\r
162 ////////////////////////////////////////////////////////////
\r
163 bool Host::confirmAddictions(){
\r
164 if (addRequests.empty()) return false;
\r
165 TempProvidersMap::iterator it;
\r
166 for( it = addRequests.begin() ; it != addRequests.end() ; ++it){
\r
167 std::list<Provider*> lst = it->second;
\r
168 std::list<Provider*>::iterator providerIt;
\r
169 for (providerIt = lst.begin() ; providerIt != lst.end() ; ++providerIt){
\r
170 knownTypes[it->first].providers.push_back(*providerIt);
\r
174 TempProvidersMap().swap(addRequests);
\r
179 } //namespace pluma
\r