StdAir Logo  0.45.1
C++ Standard Airline IT Object Library
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
BomManager.hpp
Go to the documentation of this file.
1 #ifndef __STDAIR_BOM_BOMMANAGER_HPP
2 #define __STDAIR_BOM_BOMMANAGER_HPP
3 
4 // //////////////////////////////////////////////////////////////////////
5 // Import section
6 // //////////////////////////////////////////////////////////////////////
7 // STL
8 #include <iosfwd>
9 #include <string>
10 #include <list>
11 #include <map>
12 // Boost
13 #include <boost/static_assert.hpp>
14 #include <boost/type_traits/is_same.hpp>
15 // StdAir
18 #include <stdair/bom/BomHolder.hpp>
20 // Stdair BOM Objects
22 
23 namespace stdair {
24 
32  class BomManager {
33  friend class FacBomManager;
34 
35  public:
39  template <typename OBJECT2, typename OBJECT1>
40  static const typename BomHolder<OBJECT2>::BomList_T& getList(const OBJECT1&);
41 
45  template <typename OBJECT2, typename OBJECT1>
46  static const typename BomHolder<OBJECT2>::BomMap_T& getMap (const OBJECT1&);
47 
51  template <typename OBJECT2, typename OBJECT1>
52  static bool hasList (const OBJECT1&);
53 
57  template <typename OBJECT2, typename OBJECT1>
58  static bool hasMap (const OBJECT1&);
59 
65  template <typename PARENT, typename CHILD>
66  static PARENT* getParentPtr (const CHILD&);
67 
71  template <typename PARENT, typename CHILD>
72  static PARENT& getParent (const CHILD&);
73 
79  template <typename OBJECT2, typename OBJECT1>
80  static OBJECT2* getObjectPtr (const OBJECT1&, const MapKey_T&);
81 
85  template <typename OBJECT2, typename OBJECT1>
86  static OBJECT2& getObject (const OBJECT1&, const MapKey_T&);
87 
88 
89  private:
94  template <typename OBJECT2, typename OBJECT1>
95  static const BomHolder<OBJECT2>& getBomHolder (const OBJECT1&);
96  };
97 
98  // ////////////////////////////////////////////////////////////////////
99  // Private method.
100  template <typename OBJECT2, typename OBJECT1>
101  const BomHolder<OBJECT2>& BomManager::getBomHolder (const OBJECT1& iObject1) {
102 
103  const HolderMap_T& lHolderMap = iObject1.getHolderMap();
104 
105  HolderMap_T::const_iterator itHolder = lHolderMap.find (&typeid (OBJECT2));
106 
107  if (itHolder == lHolderMap.end()) {
108  const std::string lName (typeid (OBJECT2).name());
109  throw NonInitialisedContainerException ("Cannot find the holder of type "
110  + lName + " within: "
111  + iObject1.describeKey());
112  }
113 
114  const BomHolder<OBJECT2>* lBomHolder_ptr =
115  static_cast<const BomHolder<OBJECT2>*> (itHolder->second);
116  assert (lBomHolder_ptr != NULL);
117 
118  return *lBomHolder_ptr;
119  }
120 
121  // ////////////////////////////////////////////////////////////////////
122  // Public business method.
123  // This method is specialized for the following couple types:
124  // <SegmentDate, SegmentDate>
125  template <typename OBJECT2, typename OBJECT1>
126  const typename BomHolder<OBJECT2>::BomList_T& BomManager::
127  getList (const OBJECT1& iObject1) {
128 
129  const BomHolder<OBJECT2>& lBomHolder = getBomHolder<OBJECT2> (iObject1);
130  return lBomHolder._bomList;
131  }
132 
133  // ////////////////////////////////////////////////////////////////////
134  // Public business method.
135  // Compile time assertation to check OBJECT1 and OBJECT2 types.
136  template <typename OBJECT2, typename OBJECT1>
138  getMap (const OBJECT1& iObject1) {
139 
140  //
141  // Compile time assertation: this function must never be called with the
142  // following list of couple types:
143  // <SegmentDate, SegmentDate>
144  //
145  BOOST_STATIC_ASSERT ((boost::is_same<OBJECT1, SegmentDate>::value == false
146  || boost::is_same<OBJECT2, SegmentDate>::value == false));
147 
148  const BomHolder<OBJECT2>& lBomHolder = getBomHolder<OBJECT2> (iObject1);
149  return lBomHolder._bomMap;
150  }
151 
152  // ////////////////////////////////////////////////////////////////////
153  // Public business method.
154  // This method is specialized for the following couple types:
155  // <SegmentDate, SegmentDate>
156  template <typename OBJECT2, typename OBJECT1>
157  bool BomManager::hasList (const OBJECT1& iObject1) {
158 
159  const HolderMap_T& lHolderMap = iObject1.getHolderMap();
160  HolderMap_T::const_iterator itHolder = lHolderMap.find (&typeid (OBJECT2));
161 
162  if (itHolder == lHolderMap.end()) {
163  return false;
164  }
165  const BomHolder<OBJECT2>* lBomHolder_ptr =
166  static_cast<const BomHolder<OBJECT2>*> (itHolder->second);
167  assert (lBomHolder_ptr != NULL);
168 
169  return !lBomHolder_ptr->_bomList.empty();
170  }
171 
172  // ////////////////////////////////////////////////////////////////////
173  // Public business method.
174  // This method is specialized for the following couple types:
175  // <SegmentDate, SegmentDate>
176  template <typename OBJECT2, typename OBJECT1>
177  bool BomManager::hasMap (const OBJECT1& iObject1) {
178 
179  const HolderMap_T& lHolderMap = iObject1.getHolderMap();
180  HolderMap_T::const_iterator itHolder = lHolderMap.find (&typeid (OBJECT2));
181 
182  if (itHolder == lHolderMap.end()) {
183  return false;
184  }
185  const BomHolder<OBJECT2>* lBomHolder_ptr =
186  static_cast<const BomHolder<OBJECT2>*> (itHolder->second);
187  assert (lBomHolder_ptr != NULL);
188 
189  return !lBomHolder_ptr->_bomMap.empty();
190  }
191 
192  // ////////////////////////////////////////////////////////////////////
193  // Public business method valid for all PARENT and CHILD types.
194  // (No compile time assertation to check PARENT and CHILD types.)
195  template <typename PARENT, typename CHILD>
196  PARENT* BomManager::getParentPtr (const CHILD& iChild) {
197 
198  PARENT* const lParent_ptr = static_cast<PARENT* const> (iChild.getParent());
199  return lParent_ptr;
200  }
201 
202  // ////////////////////////////////////////////////////////////////////
203  // Public business method valid for all PARENT and CHILD types.
204  // (No compile time assertation to check PARENT and CHILD types.)
205  template <typename PARENT, typename CHILD>
206  PARENT& BomManager::getParent (const CHILD& iChild) {
207 
208  PARENT* const lParent_ptr = getParentPtr<PARENT> (iChild);
209  assert (lParent_ptr != NULL);
210  return *lParent_ptr;
211  }
212 
213  // ////////////////////////////////////////////////////////////////////
214  // Public business method.
215  // Compile time assertation to check OBJECT1 and OBJECT2 types.
216  template <typename OBJECT2, typename OBJECT1>
217  OBJECT2* BomManager::getObjectPtr (const OBJECT1& iObject1,
218  const MapKey_T& iKey) {
219 
220  //
221  // Compile time assertation: this function must never be called with the
222  // following list of couple types:
223  // <SegmentDate, SegmentDate>
224  //
225  BOOST_STATIC_ASSERT ((boost::is_same<OBJECT1, SegmentDate>::value == false
226  || boost::is_same<OBJECT2, SegmentDate>::value == false));
227 
228  OBJECT2* oBom_ptr = NULL;
229 
230  const HolderMap_T& lHolderMap = iObject1.getHolderMap();
231 
232  typename HolderMap_T::const_iterator itHolder =
233  lHolderMap.find (&typeid (OBJECT2));
234 
235  if (itHolder != lHolderMap.end()) {
236 
237  BomHolder<OBJECT2>* const lBomHolder_ptr =
238  static_cast<BomHolder<OBJECT2>* const> (itHolder->second);
239  assert (lBomHolder_ptr != NULL);
240 
241  //
242  typedef typename BomHolder<OBJECT2>::BomMap_T BomMap_T;
243  BomMap_T& lBomMap = lBomHolder_ptr->_bomMap;
244  typename BomMap_T::iterator itBom = lBomMap.find (iKey);
245 
246  if (itBom != lBomMap.end()) {
247  oBom_ptr = itBom->second;
248  assert (oBom_ptr != NULL);
249  }
250  }
251 
252  return oBom_ptr;
253  }
254 
255  // ////////////////////////////////////////////////////////////////////
256  // Public business method.
257  // Compile time assertation to check OBJECT1 and OBJECT2 types.
258  template <typename OBJECT2, typename OBJECT1>
259  OBJECT2& BomManager::getObject (const OBJECT1& iObject1,
260  const MapKey_T& iKey) {
261 
262  //
263  // Compile time assertation: this function must never be called with the
264  // following list of couple types:
265  // <SegmentDate, SegmentDate>
266  //
267  BOOST_STATIC_ASSERT ((boost::is_same<OBJECT1, SegmentDate>::value == false
268  || boost::is_same<OBJECT2, SegmentDate>::value == false));
269 
270  OBJECT2* oBom_ptr = NULL;
271 
272  typedef std::map<const MapKey_T, OBJECT2*> BomMap_T;
273  const BomMap_T& lBomMap = getMap<OBJECT2> (iObject1);
274 
275  typename BomMap_T::const_iterator itBom = lBomMap.find (iKey);
276 
277  if (itBom == lBomMap.end()) {
278  const std::string lName (typeid (OBJECT2).name());
279 
280  STDAIR_LOG_ERROR ("Cannot find the objet of type " << lName
281  << " with key " << iKey << " within: "
282  << iObject1.describeKey());
283  assert (false);
284  }
285 
286  oBom_ptr = itBom->second;
287  assert (oBom_ptr != NULL);
288 
289  return *oBom_ptr;
290  }
291 
292  // ////////////////////////////////////////////////////////////////////
293  //
294  // Specialization of the template methods above for a segment
295  // date and its corresponding marketing segment dates.
296  //
297  // ////////////////////////////////////////////////////////////////////
298 
299  // Specialization of the template method hasList above for the types
300  // <SegmentDate, SegmentDate>.
301  // Add an element to the marketing segment date list of a segment date.
302  template<>
303  inline bool BomManager::hasList<SegmentDate,SegmentDate>
304  (const SegmentDate& ioSegmentDate) {
305 
306  const SegmentDateList_T& lMarketingSegmentDateList =
307  ioSegmentDate.getMarketingSegmentDateList ();
308  const bool isMarketingSegmentDateListEmpty =
309  lMarketingSegmentDateList.empty();
310  return isMarketingSegmentDateListEmpty;
311  }
312 
313  // Specialization of the template method hasList above for the types
314  // <SegmentDate, SegmentDate>.
315  // Return a boolean saying if the marketing segment date list is empty
316  // or not.
317  template<>
319  BomManager::getList<SegmentDate,SegmentDate> (const SegmentDate& ioSegmentDate) {
320 
321  const SegmentDateList_T& lMarketingSegmentDateList =
322  ioSegmentDate.getMarketingSegmentDateList ();
323  return lMarketingSegmentDateList;
324  }
325 
326  // Specialization of the template method hasMap above for the types
327  // <SegmentDate, SegmentDate>.
328  // A segment date does not have a Segment Date Map but it can have a
329  // Segment Date list (containing its marketing segment dates).
330  template<>
331  inline bool BomManager::hasMap<SegmentDate,SegmentDate>
332  (const SegmentDate& ioSegmentDate) {
333 
334  const bool hasList = false;
335  return hasList;
336  }
337 
338 }
339 #endif // __STDAIR_BOM_BOMMANAGER_HPP