$treeview $search $mathjax
StdAir Logo  1.00.2
$projectbrief
$projectbrief
$searchbox

stdair/factory/FacCloneBom.hpp

Go to the documentation of this file.
00001 #ifndef __STDAIR_FAC_FACCLONEBOM_HPP
00002 #define __STDAIR_FAC_FACCLONEBOM_HPP
00003 
00004 // //////////////////////////////////////////////////////////////////////
00005 // Import section
00006 // //////////////////////////////////////////////////////////////////////
00007 // STL
00008 #include <cassert>
00009 #include <string>
00010 #include <list>
00011 // StdAir 
00012 #include <stdair/factory/FacAbstract.hpp>
00013 #include <stdair/service/FacSupervisor.hpp>
00014 #include <stdair/service/Logger.hpp>
00015 
00016 namespace stdair {  
00017 
00021   template <typename BOM>
00022   class FacCloneBom : public FacAbstract {
00023 
00025     typedef std::list<BOM*> BomPool_T;
00026     typedef typename BOM::Key_T Key_T;
00027 
00028 
00029   public:
00030     // ///////////// Business methods ////////////
00037     static FacCloneBom& instance();
00038 
00042     BOM& clone (const BOM&);
00043     
00044   protected:
00048     FacCloneBom() {}
00049 
00050   public:
00054     ~FacCloneBom() {
00055       clean();
00056     }
00057 
00061     void clean();
00062     
00063 
00064   private:
00065     // ///////////////////// Attributes //////////////////
00069     static FacCloneBom* _instance;
00070 
00074     BomPool_T _pool;
00075   };
00076 
00077 
00078   // ////////////////////////////////////////////////////////////////////
00079   template <typename BOM> FacCloneBom<BOM>* FacCloneBom<BOM>::_instance = NULL;
00080   
00081   // ////////////////////////////////////////////////////////////////////
00082   template <typename BOM> FacCloneBom<BOM>& FacCloneBom<BOM>::instance () {
00083     if (_instance == NULL) {
00084       _instance = new FacCloneBom ();
00085       assert (_instance != NULL);
00086 
00087       FacSupervisor::instance().registerCloneBomFactory (_instance);
00088     }
00089     return *_instance;
00090   }
00091 
00092   // ////////////////////////////////////////////////////////////////////
00093   template <typename BOM> void FacCloneBom<BOM>::clean () {
00094     // Destroy all the objects
00095     for (typename BomPool_T::iterator itBom = _pool.begin();
00096          itBom != _pool.end(); ++itBom) {
00097       BOM* currentBom_ptr = *itBom;
00098       assert (currentBom_ptr != NULL);
00099       delete currentBom_ptr; currentBom_ptr = NULL;
00100     }
00101 
00102     // Empty the pool.
00103     _pool.clear();
00104 
00105     // Reset the static instance.
00106     _instance = NULL;
00107   }
00108   
00109   // ////////////////////////////////////////////////////////////////////
00110   template <typename BOM> BOM& FacCloneBom<BOM>::clone (const BOM& iBom) {
00111     BOM* oBom_ptr = new BOM (iBom);
00112     assert (oBom_ptr != NULL);
00113     _pool.push_back (oBom_ptr);
00114     return *oBom_ptr;
00115   }
00116   
00117 }
00118 #endif // __STDAIR_FAC_FACCLONEBOM_HPP