Host.cpp
1 //
3 // Pluma - Plug-in Management Framework
4 // Copyright (C) 2010-2012 Gil Costa (gsaurus@gmail.com)
5 //
6 // This software is provided 'as-is', without any express or implied warranty.
7 // In no event will the authors be held liable for any damages arising from the use of this software.
8 //
9 // Permission is granted to anyone to use this software for any purpose,
10 // including commercial applications, and to alter it and redistribute it freely,
11 // subject to the following restrictions:
12 //
13 // 1. The origin of this software must not be misrepresented;
14 // you must not claim that you wrote the original software.
15 // If you use this software in a product, an acknowledgment
16 // in the product documentation would be appreciated but is not required.
17 //
18 // 2. Altered source versions must be plainly marked as such,
19 // and must not be misrepresented as being the original software.
20 //
21 // 3. This notice may not be removed or altered from any source distribution.
22 //
24 
25 
27 // Headers
29 #include <Pluma/Host.hpp>
30 #include <cstdio>
31 
32 
33 namespace pluma{
34 
36 Host::Host(){
37  // Nothing to do
38 }
39 
40 
42 bool Host::add(Provider* provider){
43  if (provider == NULL){
44  fprintf(stderr, "Trying to add a null provider.\n");
45  return false;
46  }
47  if (!validateProvider(provider)){
48  delete provider;
49  return false;
50  }
51  addRequests[ provider->plumaGetType() ].push_back(provider);
52  return true;
53 }
54 
55 
57 Host::~Host(){
58  clearProviders();
59  // map frees itself
60 }
61 
62 
64 void Host::clearProviders(){
65  ProvidersMap::iterator it;
66  for (it = knownTypes.begin() ; it != knownTypes.end() ; ++it){
67  std::list<Provider*>& providers = it->second.providers;
68  std::list<Provider*>::iterator provIt;
69  for (provIt = providers.begin() ; provIt != providers.end() ; ++provIt){
70  delete *provIt;
71  }
72  std::list<Provider*>().swap(providers);
73  }
74 }
75 
76 
78 bool Host::knows(const std::string& type) const{
79  return knownTypes.find(type) != knownTypes.end();
80 }
81 
82 
84 unsigned int Host::getVersion(const std::string& type) const{
85  ProvidersMap::const_iterator it = knownTypes.find(type);
86  if (it != knownTypes.end())
87  return it->second.version;
88  return 0;
89 }
90 
91 
93 unsigned int Host::getLowestVersion(const std::string& type) const{
94  ProvidersMap::const_iterator it = knownTypes.find(type);
95  if (it != knownTypes.end())
96  return it->second.lowestVersion;
97  return 0;
98 }
99 
100 
102 void Host::registerType(const std::string& type, unsigned int version, unsigned int lowestVersion){
103  if (!knows(type)){
104  ProviderInfo pi;
105  pi.version = version;
106  pi.lowestVersion = lowestVersion;
107  knownTypes[type] = pi;
108  }
109 }
110 
111 
113 const std::list<Provider*>* Host::getProviders(const std::string& type) const{
114  ProvidersMap::const_iterator it = knownTypes.find(type);
115  if (it != knownTypes.end())
116  return &it->second.providers;
117  return NULL;
118 }
119 
120 
122 bool Host::validateProvider(Provider* provider) const{
123  const std::string& type = provider->plumaGetType();
124  if ( !knows(type) ){
125  fprintf(stderr, "%s provider type isn't registered.\n", type.c_str());
126  return false;
127  }
128  if (!provider->isCompatible(*this)){
129  fprintf(stderr, "Incompatible %s provider version.\n", type.c_str());
130  return false;
131  }
132  return true;
133 }
134 
135 
137 bool Host::registerProvider(Provider* provider){
138  if (!validateProvider(provider)){
139  delete provider;
140  return false;
141  }
142  knownTypes[ provider->plumaGetType() ].providers.push_back(provider);
143  return true;
144 }
145 
146 
148 void Host::cancelAddictions(){
149  TempProvidersMap::iterator it;
150  for( it = addRequests.begin() ; it != addRequests.end() ; ++it){
151  std::list<Provider*> lst = it->second;
152  std::list<Provider*>::iterator providerIt;
153  for (providerIt = lst.begin() ; providerIt != lst.end() ; ++providerIt){
154  delete *providerIt;
155  }
156  }
157  // clear map
158  TempProvidersMap().swap(addRequests);
159 }
160 
161 
163 bool Host::confirmAddictions(){
164  if (addRequests.empty()) return false;
165  TempProvidersMap::iterator it;
166  for( it = addRequests.begin() ; it != addRequests.end() ; ++it){
167  std::list<Provider*> lst = it->second;
168  std::list<Provider*>::iterator providerIt;
169  for (providerIt = lst.begin() ; providerIt != lst.end() ; ++providerIt){
170  knownTypes[it->first].providers.push_back(*providerIt);
171  }
172  }
173  // clear map
174  TempProvidersMap().swap(addRequests);
175  return true;
176 }
177 
178 
179 } //namespace pluma
Interface to provide applications with objects from plugins.
Definition: Provider.hpp:42
bool add(Provider *provider)
Add provider.
Definition: Host.cpp:42