1 | #ifndef __XMLIO_CObjectFactory_impl__ |
---|
2 | #define __XMLIO_CObjectFactory_impl__ |
---|
3 | |
---|
4 | #include "object_factory.hpp" |
---|
5 | |
---|
6 | namespace xios |
---|
7 | { |
---|
8 | /// ////////////////////// Définitions ////////////////////// /// |
---|
9 | template <typename U> |
---|
10 | int CObjectFactory::GetObjectNum(void) |
---|
11 | { |
---|
12 | if (CurrContext.size() == 0) |
---|
13 | ERROR("CObjectFactory::GetObjectNum(void)", |
---|
14 | << "please define current context id !"); |
---|
15 | return (U::AllVectObj[CObjectFactory::CurrContext].size()); |
---|
16 | } |
---|
17 | |
---|
18 | template <typename U> |
---|
19 | int CObjectFactory::GetObjectIdNum(void) |
---|
20 | { |
---|
21 | if (CurrContext.size() == 0) |
---|
22 | ERROR("CObjectFactory::GetObjectIdNum(void)", |
---|
23 | << "please define current context id !"); |
---|
24 | return (U::AllMapObj[CObjectFactory::CurrContext].size()); |
---|
25 | } |
---|
26 | |
---|
27 | template <typename U> |
---|
28 | bool CObjectFactory::HasObject(const StdString & id) |
---|
29 | { |
---|
30 | if (CurrContext.size() == 0) |
---|
31 | ERROR("CObjectFactory::HasObject(const StdString & id)", |
---|
32 | << "[ id = " << id << " ] please define current context id !"); |
---|
33 | return (U::AllMapObj[CObjectFactory::CurrContext].find(id) != |
---|
34 | U::AllMapObj[CObjectFactory::CurrContext].end()); |
---|
35 | } |
---|
36 | |
---|
37 | template <typename U> |
---|
38 | bool CObjectFactory::HasObject(const StdString & context, const StdString & id) |
---|
39 | { |
---|
40 | if (U::AllMapObj.find(context) == U::AllMapObj.end()) return false ; |
---|
41 | else return (U::AllMapObj[context].find(id) != U::AllMapObj[context].end()); |
---|
42 | } |
---|
43 | |
---|
44 | template <typename U> |
---|
45 | boost::shared_ptr<U> CObjectFactory::GetObject(const U * const object) |
---|
46 | { |
---|
47 | if (CurrContext.size() == 0) |
---|
48 | ERROR("CObjectFactory::GetObject(const U * const object)", |
---|
49 | << "please define current context id !"); |
---|
50 | std::vector<boost::shared_ptr<U> > & vect = |
---|
51 | U::AllVectObj[CObjectFactory::CurrContext]; |
---|
52 | |
---|
53 | typename std::vector<boost::shared_ptr<U> >::const_iterator |
---|
54 | it = vect.begin(), end = vect.end(); |
---|
55 | |
---|
56 | for (; it != end; it++) |
---|
57 | { |
---|
58 | boost::shared_ptr<U> ptr = *it; |
---|
59 | if (ptr.get() == object) |
---|
60 | return (ptr); |
---|
61 | } |
---|
62 | |
---|
63 | ERROR("CObjectFactory::GetObject(const U * const object)", |
---|
64 | << "[type = " << U::GetName() << ", " |
---|
65 | << "adress = " << object << "]" |
---|
66 | << " object was not found !"); |
---|
67 | return (boost::shared_ptr<U>()); // jamais atteint |
---|
68 | } |
---|
69 | |
---|
70 | template <typename U> |
---|
71 | boost::shared_ptr<U> CObjectFactory::GetObject(const StdString & id) |
---|
72 | { |
---|
73 | if (CurrContext.size() == 0) |
---|
74 | ERROR("CObjectFactory::GetObject(const StdString & id)", |
---|
75 | << "[ id = " << id << " ] please define current context id !"); |
---|
76 | if (!CObjectFactory::HasObject<U>(id)) |
---|
77 | ERROR("CObjectFactory::GetObject(const StdString & id)", |
---|
78 | << "[ id = " << id << ", U = " << U::GetName() << " ] " |
---|
79 | << " object is not referenced !"); |
---|
80 | return (U::AllMapObj[CObjectFactory::CurrContext][id]); |
---|
81 | } |
---|
82 | |
---|
83 | template <typename U> |
---|
84 | boost::shared_ptr<U> CObjectFactory::GetObject(const StdString & context, const StdString & id) |
---|
85 | { |
---|
86 | if (!CObjectFactory::HasObject<U>(context,id)) |
---|
87 | ERROR("CObjectFactory::GetObject(const StdString & id)", |
---|
88 | << "[ id = " << id << ", U = " << U::GetName() <<", context = "<<context<< " ] " |
---|
89 | << " object is not referenced !"); |
---|
90 | return (U::AllMapObj[context][id]); |
---|
91 | } |
---|
92 | |
---|
93 | template <typename U> |
---|
94 | boost::shared_ptr<U> CObjectFactory::CreateObject(const StdString & id) |
---|
95 | { |
---|
96 | if (CurrContext.size() == 0) |
---|
97 | ERROR("CObjectFactory::CreateObject(const StdString & id)", |
---|
98 | << "[ id = " << id << " ] please define current context id !"); |
---|
99 | if (id.size() == 0) |
---|
100 | { |
---|
101 | boost::shared_ptr<U> value(new U(CObjectFactory::GenUId<U>())); |
---|
102 | U::AllVectObj[CObjectFactory::CurrContext].insert |
---|
103 | (U::AllVectObj[CObjectFactory::CurrContext].end(), value); |
---|
104 | U::AllMapObj[CObjectFactory::CurrContext].insert(std::make_pair(value->getId(), value)); |
---|
105 | return (value); |
---|
106 | } |
---|
107 | else if (CObjectFactory::HasObject<U>(id)) |
---|
108 | { |
---|
109 | return (CObjectFactory::GetObject<U>(id)); |
---|
110 | } |
---|
111 | else |
---|
112 | { |
---|
113 | boost::shared_ptr<U> value(new U(id)); |
---|
114 | U::AllVectObj[CObjectFactory::CurrContext].insert |
---|
115 | (U::AllVectObj[CObjectFactory::CurrContext].end(), value); |
---|
116 | U::AllMapObj[CObjectFactory::CurrContext].insert(std::make_pair(id, value)); |
---|
117 | return (value); |
---|
118 | } |
---|
119 | } |
---|
120 | |
---|
121 | template <typename U> |
---|
122 | const std::vector<boost::shared_ptr<U> > & |
---|
123 | CObjectFactory::GetObjectVector(const StdString & context) |
---|
124 | { |
---|
125 | return (U::AllVectObj[context]); |
---|
126 | } |
---|
127 | |
---|
128 | template <typename U> |
---|
129 | StdString CObjectFactory::GenUId(void) |
---|
130 | { |
---|
131 | long int seed ; |
---|
132 | |
---|
133 | xios_map<StdString,long int>::iterator it ; |
---|
134 | it=U::GenId.find(CObjectFactory::CurrContext); |
---|
135 | if (it==U::GenId.end()) |
---|
136 | { |
---|
137 | seed=0 ; |
---|
138 | U::GenId[CObjectFactory::CurrContext]=seed ; |
---|
139 | } |
---|
140 | else |
---|
141 | { |
---|
142 | seed=it->second ; |
---|
143 | seed++ ; |
---|
144 | it->second=seed ; |
---|
145 | } |
---|
146 | |
---|
147 | StdOStringStream oss ; |
---|
148 | oss<<"__"<<U::GetName()<<"_undef_id_"<<seed ; |
---|
149 | return StdString(oss.str()) ; |
---|
150 | } |
---|
151 | |
---|
152 | } // namespace xios |
---|
153 | |
---|
154 | #endif // __XMLIO_CObjectFactory_impl__ |
---|