]> Creatis software - cpPlugins.git/blob - lib/third_party/Pluma/Host.cpp
6fb5539b89dea0555c7a9988f46e7e4c74555f33
[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 #include <stdexcept>\r
32 \r
33 \r
34 namespace pluma{\r
35 \r
36 ////////////////////////////////////////////////////////////\r
37 Host::Host(){\r
38     // Nothing to do\r
39 }\r
40 \r
41 \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
47         return false;\r
48     }\r
49     if (!validateProvider(provider)){\r
50         delete provider;\r
51         return false;\r
52     }\r
53     addRequests[ provider->plumaGetType() ].push_back(provider);\r
54     return true;\r
55 }\r
56 \r
57 \r
58 ////////////////////////////////////////////////////////////\r
59 Host::~Host(){\r
60     clearProviders();\r
61     // map frees itself\r
62 }\r
63 \r
64 \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
72             delete *provIt;\r
73         }\r
74         std::list<Provider*>().swap(providers);\r
75     }\r
76 }\r
77 \r
78 \r
79 ////////////////////////////////////////////////////////////\r
80 bool Host::knows(const std::string& type) const{\r
81     return knownTypes.find(type) != knownTypes.end();\r
82 }\r
83 \r
84 \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
90     return 0;\r
91 }\r
92 \r
93 \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
99     return 0;\r
100 }\r
101 \r
102 \r
103 ////////////////////////////////////////////////////////////\r
104 void Host::registerType(const std::string& type, unsigned int version, unsigned int lowestVersion){\r
105     if (!knows(type)){\r
106         ProviderInfo pi;\r
107         pi.version = version;\r
108         pi.lowestVersion = lowestVersion;\r
109         knownTypes[type] = pi;\r
110     }\r
111 }\r
112 \r
113 \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
119     return NULL;\r
120 }\r
121 \r
122 \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
130         );\r
131         return false;\r
132     }\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
137         type +\r
138         std::string( " provider version." )\r
139         );\r
140         return false;\r
141     }\r
142     return true;\r
143 }\r
144 \r
145 \r
146 ////////////////////////////////////////////////////////////\r
147 bool Host::registerProvider(Provider* provider){\r
148     if (!validateProvider(provider)){\r
149         delete provider;\r
150         return false;\r
151     }\r
152     knownTypes[ provider->plumaGetType() ].providers.push_back(provider);\r
153     return true;\r
154 }\r
155 \r
156 \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
165         }\r
166     }\r
167     // clear map\r
168     TempProvidersMap().swap(addRequests);\r
169 }\r
170 \r
171 \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
181         }\r
182     }\r
183     // clear map\r
184     TempProvidersMap().swap(addRequests);\r
185     return true;\r
186 }\r
187 \r
188 \r
189 } //namespace pluma\r