StdAir Logo  0.45.1
C++ Standard Airline IT Object Library
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
FacBom.hpp
Go to the documentation of this file.
1 #ifndef __STDAIR_FAC_FACBOM_HPP
2 #define __STDAIR_FAC_FACBOM_HPP
3 
4 // //////////////////////////////////////////////////////////////////////
5 // Import section
6 // //////////////////////////////////////////////////////////////////////
7 // STL
8 #include <cassert>
9 #include <string>
10 #include <list>
11 // StdAir
15 
16 namespace stdair {
17 
21  template <typename BOM>
22  class FacBom : public FacAbstract {
23 
25  typedef std::list<BOM*> BomPool_T;
26  typedef typename BOM::Key_T Key_T;
27 
28 
29  public:
30  // ///////////// Business methods ////////////
37  static FacBom& instance();
38 
42  BOM& create ();
43  BOM& create (const Key_T&);
44 
45  protected:
49  FacBom() {}
50 
51  public:
55  ~FacBom() {
56  clean();
57  }
58 
62  void clean();
63 
64 
65  private:
66  // ///////////////////// Attributes //////////////////
70  static FacBom* _instance;
71 
75  BomPool_T _pool;
76  };
77 
78 
79  // ////////////////////////////////////////////////////////////////////
80  template <typename BOM> FacBom<BOM>* FacBom<BOM>::_instance = NULL;
81 
82  // ////////////////////////////////////////////////////////////////////
83  template <typename BOM> FacBom<BOM>& FacBom<BOM>::instance () {
84  if (_instance == NULL) {
85  _instance = new FacBom ();
86  assert (_instance != NULL);
87 
89  }
90  return *_instance;
91  }
92 
93  // ////////////////////////////////////////////////////////////////////
94  template <typename BOM> void FacBom<BOM>::clean () {
95  // Destroy all the objects
96  for (typename BomPool_T::iterator itBom = _pool.begin();
97  itBom != _pool.end(); ++itBom) {
98  BOM* currentBom_ptr = *itBom;
99  assert (currentBom_ptr != NULL);
100  delete currentBom_ptr; currentBom_ptr = NULL;
101  }
102 
103  // Empty the pool.
104  _pool.clear();
105 
106  // Reset the static instance.
107  _instance = NULL;
108  }
109 
110  // ////////////////////////////////////////////////////////////////////
111  template <typename BOM> BOM& FacBom<BOM>::create () {
112  Key_T lKey;
113  return instance().create (lKey);
114  }
115 
116  // ////////////////////////////////////////////////////////////////////
117  template <typename BOM> BOM& FacBom<BOM>::create (const Key_T& iKey) {
118  BOM* oBom_ptr = new BOM (iKey);
119  assert (oBom_ptr != NULL);
120  _pool.push_back (oBom_ptr);
121  return *oBom_ptr;
122  }
123 
124 }
125 #endif // __STDAIR_FAC_FACBOM_HPP