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

stdair/factory/FacBom.hpp

Go to the documentation of this file.
00001 #ifndef __STDAIR_FAC_FACBOM_HPP
00002 #define __STDAIR_FAC_FACBOM_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 FacBom : 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 FacBom& instance();
00038 
00042     BOM& create ();
00043     BOM& create (const Key_T&);
00044     BOM& create (const BOM&);
00045     
00046   protected:
00050     FacBom() {}
00051 
00052   public:
00056     ~FacBom() {
00057       clean();
00058     }
00059 
00063     void clean();
00064     
00065 
00066   private:
00067     // ///////////////////// Attributes //////////////////
00071     static FacBom* _instance;
00072 
00076     BomPool_T _pool;
00077   };
00078 
00079 
00080   // ////////////////////////////////////////////////////////////////////
00081   template <typename BOM> FacBom<BOM>* FacBom<BOM>::_instance = NULL;
00082   
00083   // ////////////////////////////////////////////////////////////////////
00084   template <typename BOM> FacBom<BOM>& FacBom<BOM>::instance () {
00085     if (_instance == NULL) {
00086       _instance = new FacBom ();
00087       assert (_instance != NULL);
00088 
00089       FacSupervisor::instance().registerPersistentBomFactory (_instance);
00090     }
00091     return *_instance;
00092   }
00093 
00094   // ////////////////////////////////////////////////////////////////////
00095   template <typename BOM> void FacBom<BOM>::clean () {
00096     // Destroy all the objects
00097     for (typename BomPool_T::iterator itBom = _pool.begin();
00098          itBom != _pool.end(); ++itBom) {
00099       BOM* currentBom_ptr = *itBom;
00100       assert (currentBom_ptr != NULL);
00101       delete currentBom_ptr; currentBom_ptr = NULL;
00102     }
00103 
00104     // Empty the pool.
00105     _pool.clear();
00106 
00107     // Reset the static instance.
00108     _instance = NULL;
00109   }
00110   
00111   // ////////////////////////////////////////////////////////////////////
00112   template <typename BOM> BOM& FacBom<BOM>::create () {
00113     Key_T lKey;
00114     return instance().create (lKey);
00115   }
00116   
00117   // ////////////////////////////////////////////////////////////////////
00118   template <typename BOM> BOM& FacBom<BOM>::create (const Key_T& iKey) {
00119     BOM* oBom_ptr = new BOM (iKey);
00120     assert (oBom_ptr != NULL);
00121     _pool.push_back (oBom_ptr);
00122     return *oBom_ptr;
00123   }
00124 
00125   // ////////////////////////////////////////////////////////////////////
00126   template <typename BOM> BOM& FacBom<BOM>::create (const BOM& iBom) {
00127     BOM* oBom_ptr = new BOM (iBom);
00128     assert (oBom_ptr != NULL);
00129     _pool.push_back (oBom_ptr);
00130     return *oBom_ptr;
00131   }
00132   
00133 }
00134 #endif // __STDAIR_FAC_FACBOM_HPP