Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Namespace Members   Compound Members   File Members   Related Pages  

HalftonerFactory.cxx

Go to the documentation of this file.
00001 /*
00002  * @(#)src/HalftonerFactory.cxx  1.0  2002-08-26 10:53
00003  *
00004  * Copyright (C)  2002  Daniel Léonard
00005  *
00006  * This program is free software; you can redistribute it and/or
00007  * modify it under the terms of the GNU General Public License
00008  * as published by the Free Software Foundation; either version 2
00009  * of the License, or (at your option) any later version.
00010  *
00011  * This program is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  * GNU General Public License for more details.
00015  *
00016  * You should have received a copy of the GNU General Public License
00017  * along with this program; if not, write to the Free Software
00018  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
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 // Class HalftonerFactory::Repository
00042 //------------------------------------------
00043 
00044 //---------------------------
00045 // Constructor
00046 //---------------------------
00047 
00048    HalftonerFactory::Repository::Repository() throw()
00049    {
00050       Instanciator* instanciator = 0;
00051       const char* name = 0;
00052       try
00053       {
00054    //std::cout << "got instanciator, adding it" << std::endl;
00055    //      this->add(HalftonerFactory::FLOYD_STEINBERG, instanciator);
00056 
00057          std::map<const char*, const Instanciator*>& collection = this->depot;
00058 
00059          name = HalftonerFactory::FLOYD_STEINBERG;
00060          instanciator = new FloydSteinbergInstanciator();
00061          /*std::pair<std::map<const char*, const Instanciator*>::iterator, bool> result = */collection.insert(std::make_pair(name, instanciator));
00062 
00063          name = HalftonerFactory::JARVIS_JUDICE_NINKE;
00064          instanciator = new JarvisJudiceNinkeInstanciator();
00065          /*std::pair<std::map<const char*, const Instanciator*>::iterator, bool> result = */collection.insert(std::make_pair(name, instanciator));
00066 
00067          name = HalftonerFactory::STUCKI;
00068          instanciator = new StuckiInstanciator();
00069          /*std::pair<std::map<const char*, const Instanciator*>::iterator, bool> result = */collection.insert(std::make_pair(name, instanciator));
00070 
00071          name = HalftonerFactory::VG91;
00072          instanciator = new VG91Instanciator();
00073          /*std::pair<std::map<const char*, const Instanciator*>::iterator, bool> result = */collection.insert(std::make_pair(name, instanciator));
00074 
00075          name = HalftonerFactory::VG95;
00076          instanciator = new VG95Instanciator();
00077          /*std::pair<std::map<const char*, const Instanciator*>::iterator, bool> result = */collection.insert(std::make_pair(name, instanciator));
00078 
00079          name = HalftonerFactory::VGEXP;
00080          instanciator = new VGexpInstanciator();
00081          /*std::pair<std::map<const char*, const Instanciator*>::iterator, bool> result = */collection.insert(std::make_pair(name, instanciator));
00082    /*
00083          bool success = result.second;
00084 
00085          if (false == success)
00086          {
00087             std::string message("The key : ");
00088             message += name;
00089             message += " is already in the map";
00090             throw std::invalid_argument(message);
00091          }
00092    */
00093 
00094 
00095       }
00096       catch (std::invalid_argument& ia)
00097       {
00098    // TODO : notify the user of the error
00099       }
00100    }
00101 
00102 
00103 //---------------------------
00104 // Destructor
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 // Accessor
00128 //---------------------------
00129 
00130    std::map<const char*, const Instanciator*>& HalftonerFactory::Repository::getMap() throw()
00131    {
00132       return this->depot;
00133    }
00134 
00135 
00136 //---------------------------
00137 // Instance method
00138 //---------------------------
00139 
00140    void HalftonerFactory::Repository::add(const char* name, const Instanciator* instanciator) throw (std::invalid_argument)
00141    {
00142    // make the object to be inserted
00143       std::pair<const char*, const Instanciator*> value = std::make_pair(name, instanciator);
00144 
00145    // insert the pair
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    // notify if the user if the object had not an unique key
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    // get the iterator pointing to the pair holding the instanciator
00163       std::map<const char*, const Instanciator*>& collection = this->getMap();
00164       std::map<const char*, const Instanciator*>::iterator iter = collection.find(name);
00165 
00166    // verify if it maps
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 // Class HalftonerFactory
00179 //------------------------------------------
00180 
00181 //---------------------------
00182 // Class variables
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 // Destructor
00197 //---------------------------
00198 
00199    HalftonerFactory::~HalftonerFactory() throw()
00200    {
00201    // nothing
00202    }
00203 
00204 
00205 //---------------------------
00206 // Class methods
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    // add to the repository
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    // get an instanciator
00244       HalftonerFactory::Repository& repository = HalftonerFactory::getRepository();
00245       const Instanciator* instanciator = repository.get(key);
00246 
00247    // make sure it exists
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    // make the halftoner object
00256       Halftoner* halftoner = instanciator->operator()();
00257       return halftoner;
00258    }
00259 
00260 }

Generated on Sat Sep 7 16:31:38 2002 for Halftoning Library by doxygen1.2.17