1 | #ifndef _XMLLIGHT_VECTOR_ |
---|
2 | #define _XMLLIGHT_VECTOR_ |
---|
3 | |
---|
4 | #include "Behavioural/include/XMLLightFunctors.h" |
---|
5 | #include <vector> |
---|
6 | #include <algorithm> |
---|
7 | #include <iostream> |
---|
8 | |
---|
9 | using std::basic_ostream; |
---|
10 | using std::basic_ios; |
---|
11 | using std::endl; |
---|
12 | |
---|
13 | namespace morpheo { |
---|
14 | namespace XMLUtils { |
---|
15 | |
---|
16 | //********************************************Class Declaration***************************************// |
---|
17 | |
---|
18 | // an helper class to handle XMLLight * vector |
---|
19 | template < typename XMLLightImpl> |
---|
20 | class XMLLightVector : public std::vector<XMLLightImpl *> { |
---|
21 | public : |
---|
22 | inline explicit XMLLightVector(void); |
---|
23 | inline ~XMLLightVector(void); |
---|
24 | |
---|
25 | XMLLightVector(const XMLLightVector & xmlLightVectorCopied); |
---|
26 | XMLLightVector & operator= ( const XMLLightVector & copy ); |
---|
27 | |
---|
28 | // must be defined inside template class declaration |
---|
29 | template < typename charT, typename traits> friend |
---|
30 | basic_ostream<charT, traits> & operator<< |
---|
31 | ( basic_ostream<charT, traits> & os , const XMLLightVector<XMLLightImpl> & vect) { |
---|
32 | if ( vect.size() == 0 ) { |
---|
33 | os << "There's no Child Nodes " << endl ; |
---|
34 | } else { |
---|
35 | for ( unsigned int i = 0 ; i < vect.size() ; ++i ) { |
---|
36 | os <<"Nodes "<< i << ": \n\t" << vect[i] ->getInternalString() << endl; |
---|
37 | } |
---|
38 | } |
---|
39 | return os; |
---|
40 | } |
---|
41 | |
---|
42 | }; |
---|
43 | |
---|
44 | //******************************************Inline implementation************************************// |
---|
45 | |
---|
46 | template< typename T > class XMLLightDeleter; |
---|
47 | |
---|
48 | template < typename XMLLightImpl > |
---|
49 | XMLLightVector<XMLLightImpl>::XMLLightVector() { |
---|
50 | } |
---|
51 | |
---|
52 | template < typename XMLLightImpl > |
---|
53 | XMLLightVector< XMLLightImpl>::~XMLLightVector(void) { |
---|
54 | transform ( this -> begin(), this -> end(), this -> begin(), XMLLightDeleter<XMLLightImpl>() ); |
---|
55 | } |
---|
56 | |
---|
57 | //**************************************** template implementation*******************************// |
---|
58 | |
---|
59 | template < typename XMLLightImpl > |
---|
60 | XMLLightVector<XMLLightImpl>::XMLLightVector(const XMLLightVector<XMLLightImpl> & copy ) { |
---|
61 | for ( unsigned int i = 0 ; i < copy.size() ; ++i ) { |
---|
62 | this -> push_back(copy[i]); |
---|
63 | copy[i] -> attach(); |
---|
64 | } |
---|
65 | } |
---|
66 | |
---|
67 | template < typename XMLLightImpl > |
---|
68 | XMLLightVector<XMLLightImpl> & XMLLightVector<XMLLightImpl>::operator= ( const XMLLightVector<XMLLightImpl> & vector ) { |
---|
69 | if ( &vector != this ) { |
---|
70 | unsigned int this_size = this -> size(); |
---|
71 | unsigned int copy_max_size = vector.size(); |
---|
72 | unsigned int max_size = copy_max_size < this_size ? this_size : copy_max_size; |
---|
73 | for ( unsigned int i = 0 ; i < max_size ; ++i ) { |
---|
74 | if ( this_size ) { |
---|
75 | (*this)[i] -> detach(); |
---|
76 | } |
---|
77 | if ( i < copy_max_size ) { |
---|
78 | if ( this_size == 0 ) { |
---|
79 | this -> push_back(vector[i]); |
---|
80 | } else { |
---|
81 | (*this)[i] = vector[i]; |
---|
82 | } |
---|
83 | (*this)[i] -> attach(); |
---|
84 | } else { |
---|
85 | (*this)[i] = NULL; |
---|
86 | } |
---|
87 | } |
---|
88 | } |
---|
89 | return *this; |
---|
90 | } |
---|
91 | |
---|
92 | }// end of XMLUtils namespace |
---|
93 | } // end of morpheo namespace |
---|
94 | |
---|
95 | #endif // _XMLLIGHT_VECTOR_ |
---|