00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00026 #include <utility>
00027 #include "HalftonerFactory.h"
00028 #include "Instanciator.h"
00029
00030 #include "FloydSteinbergInstanciator.h"
00031 #include "JarvisJudiceNinkeInstanciator.h"
00032 #include "StuckiInstanciator.h"
00033 #include "VG91Instanciator.h"
00034 #include "VG95Instanciator.h"
00035 #include "VGexpInstanciator.h"
00036
00037 namespace halftoner
00038 {
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048 HalftonerFactory::Repository::Repository() throw()
00049 {
00050 Instanciator* instanciator = 0;
00051 const char* name = 0;
00052 try
00053 {
00054
00055
00056
00057 std::map<const char*, const Instanciator*>& collection = this->depot;
00058
00059 name = HalftonerFactory::FLOYD_STEINBERG;
00060 instanciator = new FloydSteinbergInstanciator();
00061 collection.insert(std::make_pair(name, instanciator));
00062
00063 name = HalftonerFactory::JARVIS_JUDICE_NINKE;
00064 instanciator = new JarvisJudiceNinkeInstanciator();
00065 collection.insert(std::make_pair(name, instanciator));
00066
00067 name = HalftonerFactory::STUCKI;
00068 instanciator = new StuckiInstanciator();
00069 collection.insert(std::make_pair(name, instanciator));
00070
00071 name = HalftonerFactory::VG91;
00072 instanciator = new VG91Instanciator();
00073 collection.insert(std::make_pair(name, instanciator));
00074
00075 name = HalftonerFactory::VG95;
00076 instanciator = new VG95Instanciator();
00077 collection.insert(std::make_pair(name, instanciator));
00078
00079 name = HalftonerFactory::VGEXP;
00080 instanciator = new VGexpInstanciator();
00081 collection.insert(std::make_pair(name, instanciator));
00082
00083
00084
00085
00086
00087
00088
00089
00090
00091
00092
00093
00094
00095 }
00096 catch (std::invalid_argument& ia)
00097 {
00098
00099 }
00100 }
00101
00102
00103
00104
00105
00106
00107 HalftonerFactory::Repository::~Repository() throw()
00108 {
00109 std::map<const char*, const Instanciator*>& collection = this->getMap();
00110
00111 std::map<const char*, const Instanciator*>::iterator iter = collection.begin();
00112 std::map<const char*, const Instanciator*>::iterator end = collection.end();
00113
00114 while (iter != end)
00115 {
00116 const std::pair<const char*, const Instanciator*>& item = *iter;
00117 const Instanciator* instanciator = item.second;
00118 delete instanciator;
00119 iter++;
00120 }
00121
00122 collection.clear();
00123 }
00124
00125
00126
00127
00128
00129
00130 std::map<const char*, const Instanciator*>& HalftonerFactory::Repository::getMap() throw()
00131 {
00132 return this->depot;
00133 }
00134
00135
00136
00137
00138
00139
00140 void HalftonerFactory::Repository::add(const char* name, const Instanciator* instanciator) throw (std::invalid_argument)
00141 {
00142
00143 std::pair<const char*, const Instanciator*> value = std::make_pair(name, instanciator);
00144
00145
00146 std::map<const char*, const Instanciator*>& collection = this->getMap();
00147 std::pair<std::map<const char*, const Instanciator*>::iterator, bool> result = collection.insert(value);
00148
00149
00150 bool success = result.second;
00151 if (false == success)
00152 {
00153 std::string message("The key : ");
00154 message += name;
00155 message += " is already in the map";
00156 throw std::invalid_argument(message);
00157 }
00158 }
00159
00160 const Instanciator* HalftonerFactory::Repository::get(const char* name) throw ()
00161 {
00162
00163 std::map<const char*, const Instanciator*>& collection = this->getMap();
00164 std::map<const char*, const Instanciator*>::iterator iter = collection.find(name);
00165
00166
00167 const Instanciator* instanciator = 0;
00168 if (collection.end() != iter)
00169 {
00170 instanciator = (*iter).second;
00171 }
00172
00173 return instanciator;
00174 }
00175
00176
00177
00178
00179
00180
00181
00182
00183
00184
00185 HalftonerFactory::Repository HalftonerFactory::repository;
00186
00187 const char* HalftonerFactory::FLOYD_STEINBERG = "FloydSteinberg";
00188 const char* HalftonerFactory::JARVIS_JUDICE_NINKE = "JarvisJudiceNinke";
00189 const char* HalftonerFactory::STUCKI = "Stucki";
00190 const char* HalftonerFactory::VG91 = "VG91";
00191 const char* HalftonerFactory::VG95 = "VG95";
00192 const char* HalftonerFactory::VGEXP = "VGexp";
00193
00194
00195
00196
00197
00198
00199 HalftonerFactory::~HalftonerFactory() throw()
00200 {
00201
00202 }
00203
00204
00205
00206
00207
00208
00212 HalftonerFactory::Repository& HalftonerFactory::getRepository() throw()
00213 {
00214 return HalftonerFactory::repository;
00215 }
00216
00217 void HalftonerFactory::addHalftoner(const char* key, const Instanciator* instanciator) throw (std::invalid_argument)
00218 {
00219 if (0 == key)
00220 {
00221 std::string message("key parameter is 0");
00222 throw std::invalid_argument(message);
00223 }
00224 if (0 == instanciator)
00225 {
00226 std::string message("instanciator parameter is 0");
00227 throw std::invalid_argument(message);
00228 }
00229
00230
00231 HalftonerFactory::Repository& repository = HalftonerFactory::getRepository();
00232 repository.add(key, instanciator);
00233 }
00234
00235 Halftoner* HalftonerFactory::getInstance(const char* key) throw (std::invalid_argument)
00236 {
00237 if (0 == key)
00238 {
00239 std::string message("key parameter is 0");
00240 throw std::invalid_argument(message);
00241 }
00242
00243
00244 HalftonerFactory::Repository& repository = HalftonerFactory::getRepository();
00245 const Instanciator* instanciator = repository.get(key);
00246
00247
00248 if (0 == instanciator)
00249 {
00250 std::string message("Could not create halftoner : ");
00251 message += key;
00252 throw std::invalid_argument(message);
00253 }
00254
00255
00256 Halftoner* halftoner = instanciator->operator()();
00257 return halftoner;
00258 }
00259
00260 }