]> Creatis software - cpPlugins.git/blob - lib/third_party/Pluma/Host.cpp
Base objects migration
[cpPlugins.git] / lib / third_party / Pluma / Host.cpp
1 ////////////////////////////////////////////////////////////\r
2 //\r
3 // Pluma - Plug-in Management Framework\r
4 // Copyright (C) 2010-2012 Gil Costa (gsaurus@gmail.com)\r
5 //\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
8 //\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
12 //\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
17 //\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
20 //\r
21 // 3. This notice may not be removed or altered from any source distribution.\r
22 //\r
23 ////////////////////////////////////////////////////////////\r
24 \r
25 \r
26 ////////////////////////////////////////////////////////////\r
27 // Headers\r
28 ////////////////////////////////////////////////////////////\r
29 #include <Pluma/Host.hpp>\r
30 #include <cstdio>\r
31 \r
32 \r
33 namespace pluma{\r
34 \r
35 ////////////////////////////////////////////////////////////\r
36 Host::Host(){\r
37     // Nothing to do\r
38 }\r
39 \r
40 \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
45         return false;\r
46     }\r
47     if (!validateProvider(provider)){\r
48         delete provider;\r
49         return false;\r
50     }\r
51     addRequests[ provider->plumaGetType() ].push_back(provider);\r
52     return true;\r
53 }\r
54 \r
55 \r
56 ////////////////////////////////////////////////////////////\r
57 Host::~Host(){\r
58     clearProviders();\r
59     // map frees itself\r
60 }\r
61 \r
62 \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
70             delete *provIt;\r
71         }\r
72         std::list<Provider*>().swap(providers);\r
73     }\r
74 }\r
75 \r
76 \r
77 ////////////////////////////////////////////////////////////\r
78 bool Host::knows(const std::string& type) const{\r
79     return knownTypes.find(type) != knownTypes.end();\r
80 }\r
81 \r
82 \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
88     return 0;\r
89 }\r
90 \r
91 \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
97     return 0;\r
98 }\r
99 \r
100 \r
101 ////////////////////////////////////////////////////////////\r
102 void Host::registerType(const std::string& type, unsigned int version, unsigned int lowestVersion){\r
103     if (!knows(type)){\r
104         ProviderInfo pi;\r
105         pi.version = version;\r
106         pi.lowestVersion = lowestVersion;\r
107         knownTypes[type] = pi;\r
108     }\r
109 }\r
110 \r
111 \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
117     return NULL;\r
118 }\r
119 \r
120 \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
126         return false;\r
127     }\r
128     if (!provider->isCompatible(*this)){\r
129         fprintf(stderr, "Incompatible %s provider version.\n", type.c_str());\r
130         return false;\r
131     }\r
132     return true;\r
133 }\r
134 \r
135 \r
136 ////////////////////////////////////////////////////////////\r
137 bool Host::registerProvider(Provider* provider){\r
138     if (!validateProvider(provider)){\r
139         delete provider;\r
140         return false;\r
141     }\r
142     knownTypes[ provider->plumaGetType() ].providers.push_back(provider);\r
143     return true;\r
144 }\r
145 \r
146 \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
155         }\r
156     }\r
157     // clear map\r
158     TempProvidersMap().swap(addRequests);\r
159 }\r
160 \r
161 \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
171         }\r
172     }\r
173     // clear map\r
174     TempProvidersMap().swap(addRequests);\r
175     return true;\r
176 }\r
177 \r
178 \r
179 } //namespace pluma\r