1 | #ifndef __XIOS_CObjectFactory_impl__ |
---|
2 | #define __XIOS_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_ptr->size() == 0) |
---|
13 | ERROR("CObjectFactory::GetObjectNum(void)", |
---|
14 | << "please define current context id !"); |
---|
15 | |
---|
16 | if(U::AllVectObj == NULL) return 0; |
---|
17 | |
---|
18 | |
---|
19 | return (*U::AllVectObj)[*CObjectFactory::CurrContext_ptr].size(); |
---|
20 | } |
---|
21 | |
---|
22 | template <typename U> |
---|
23 | int CObjectFactory::GetObjectIdNum(void) |
---|
24 | { |
---|
25 | if (CurrContext_ptr->size() == 0) |
---|
26 | ERROR("CObjectFactory::GetObjectIdNum(void)", |
---|
27 | << "please define current context id !"); |
---|
28 | if(U::AllMapObj == NULL) return 0; |
---|
29 | |
---|
30 | |
---|
31 | |
---|
32 | return (* U::AllMapObj) [*CObjectFactory::CurrContext_ptr].size(); |
---|
33 | } |
---|
34 | |
---|
35 | template <typename U> |
---|
36 | bool CObjectFactory::HasObject(const StdString & id) |
---|
37 | { |
---|
38 | if (CurrContext_ptr->size() == 0) |
---|
39 | ERROR("CObjectFactory::HasObject(const StdString & id)", |
---|
40 | << "[ id = " << id << " ] please define current context id !"); |
---|
41 | |
---|
42 | if(U::AllMapObj == NULL) return false; |
---|
43 | |
---|
44 | |
---|
45 | |
---|
46 | return ((*U::AllMapObj)[*CObjectFactory::CurrContext_ptr].find(id) != |
---|
47 | (*U::AllMapObj)[*CObjectFactory::CurrContext_ptr].end()); |
---|
48 | } |
---|
49 | |
---|
50 | template <typename U> |
---|
51 | bool CObjectFactory::HasObject(const StdString & context, const StdString & id) |
---|
52 | { |
---|
53 | if(U::AllMapObj == NULL) return false; |
---|
54 | |
---|
55 | if (U::AllMapObj->find(context) == U::AllMapObj->end()) return false ; |
---|
56 | |
---|
57 | else |
---|
58 | { |
---|
59 | return ((*U::AllMapObj)[context].find(id) != (*U::AllMapObj)[context].end()); |
---|
60 | } |
---|
61 | |
---|
62 | } |
---|
63 | |
---|
64 | template <typename U> |
---|
65 | boost::shared_ptr<U> CObjectFactory::GetObject(const U * const object) |
---|
66 | { |
---|
67 | if(U::AllVectObj == NULL) return (boost::shared_ptr<U>()); |
---|
68 | |
---|
69 | if (CurrContext_ptr->size() == 0) |
---|
70 | ERROR("CObjectFactory::GetObject(const U * const object)", |
---|
71 | << "please define current context id !"); |
---|
72 | std::vector<boost::shared_ptr<U> > & vect = |
---|
73 | (*U::AllVectObj)[*CObjectFactory::CurrContext_ptr]; |
---|
74 | |
---|
75 | typename std::vector<boost::shared_ptr<U> >::const_iterator |
---|
76 | it = vect.begin(), end = vect.end(); |
---|
77 | |
---|
78 | for (; it != end; it++) |
---|
79 | { |
---|
80 | boost::shared_ptr<U> ptr = *it; |
---|
81 | if (ptr.get() == object) |
---|
82 | return (ptr); |
---|
83 | } |
---|
84 | |
---|
85 | ERROR("CObjectFactory::GetObject(const U * const object)", |
---|
86 | << "[type = " << U::GetName() << ", adress = " << object << "] " |
---|
87 | << "object was not found."); |
---|
88 | return (boost::shared_ptr<U>()); // jamais atteint |
---|
89 | } |
---|
90 | |
---|
91 | template <typename U> |
---|
92 | boost::shared_ptr<U> CObjectFactory::GetObject(const StdString & id) |
---|
93 | { |
---|
94 | if(U::AllMapObj == NULL) return (boost::shared_ptr<U>()); |
---|
95 | |
---|
96 | if (CurrContext_ptr->size() == 0) |
---|
97 | ERROR("CObjectFactory::GetObject(const StdString & id)", |
---|
98 | << "[ id = " << id << " ] please define current context id !"); |
---|
99 | if (!CObjectFactory::HasObject<U>(id)) |
---|
100 | ERROR("CObjectFactory::GetObject(const StdString & id)", |
---|
101 | << "[ id = " << id << ", U = " << U::GetName() << " ] " |
---|
102 | << "object was not found."); |
---|
103 | return (*U::AllMapObj)[*CObjectFactory::CurrContext_ptr][id]; |
---|
104 | } |
---|
105 | |
---|
106 | template <typename U> |
---|
107 | boost::shared_ptr<U> CObjectFactory::GetObject(const StdString & context, const StdString & id) |
---|
108 | { |
---|
109 | if(U::AllMapObj == NULL) return (boost::shared_ptr<U>()); |
---|
110 | |
---|
111 | if (!CObjectFactory::HasObject<U>(context,id)) |
---|
112 | ERROR("CObjectFactory::GetObject(const StdString & id)", |
---|
113 | << "[ id = " << id << ", U = " << U::GetName() <<", context = "<<context<< " ] " |
---|
114 | << "object was not found."); |
---|
115 | |
---|
116 | return (*U::AllMapObj)[context][id]; |
---|
117 | } |
---|
118 | |
---|
119 | template <typename U> |
---|
120 | boost::shared_ptr<U> CObjectFactory::CreateObject(const StdString& id) |
---|
121 | { |
---|
122 | if(U::AllVectObj == NULL) U::AllVectObj = new xios_map<StdString, std::vector<boost::shared_ptr<U> > >; |
---|
123 | if(U::AllMapObj == NULL) U::AllMapObj = new xios_map<StdString, xios_map<StdString, boost::shared_ptr<U> > >; |
---|
124 | |
---|
125 | if (CurrContext_ptr->empty()) |
---|
126 | ERROR("CObjectFactory::CreateObject(const StdString& id)", |
---|
127 | << "[ id = " << id << " ] please define current context id !"); |
---|
128 | |
---|
129 | if (CObjectFactory::HasObject<U>(id)) |
---|
130 | { |
---|
131 | return CObjectFactory::GetObject<U>(id); |
---|
132 | } |
---|
133 | else |
---|
134 | { |
---|
135 | boost::shared_ptr<U> value(new U(id.empty() ? CObjectFactory::GenUId<U>() : id)); |
---|
136 | |
---|
137 | (* U::AllVectObj)[*CObjectFactory::CurrContext_ptr].insert((*U::AllVectObj)[*CObjectFactory::CurrContext_ptr].end(), value); |
---|
138 | (* U::AllMapObj) [*CObjectFactory::CurrContext_ptr].insert(std::make_pair(value->getId(), value)); |
---|
139 | |
---|
140 | return value; |
---|
141 | } |
---|
142 | } |
---|
143 | |
---|
144 | template <typename U> |
---|
145 | const std::vector<boost::shared_ptr<U> > & |
---|
146 | CObjectFactory::GetObjectVector(const StdString & context) |
---|
147 | { |
---|
148 | if(U::AllVectObj != NULL) |
---|
149 | |
---|
150 | |
---|
151 | return (*U::AllVectObj)[context]; |
---|
152 | } |
---|
153 | |
---|
154 | template <typename U> |
---|
155 | const StdString& CObjectFactory::GetUIdBase(void) |
---|
156 | { |
---|
157 | static const StdString base = "__" + U::GetName() + "_undef_id_"; |
---|
158 | return base; |
---|
159 | } |
---|
160 | |
---|
161 | template <typename U> |
---|
162 | StdString CObjectFactory::GenUId(void) |
---|
163 | { |
---|
164 | StdOStringStream oss; |
---|
165 | if(U::GenId == NULL) U::GenId = new xios_map< StdString, long int >; |
---|
166 | oss << GetUIdBase<U>() << (*U::GenId)[*CObjectFactory::CurrContext_ptr]++; |
---|
167 | return oss.str(); |
---|
168 | } |
---|
169 | |
---|
170 | template <typename U> |
---|
171 | bool CObjectFactory::IsGenUId(const StdString& id) |
---|
172 | { |
---|
173 | const StdString& base = GetUIdBase<U>(); |
---|
174 | return (id.size() > base.size() && id.compare(0, base.size(), base) == 0); |
---|
175 | } |
---|
176 | |
---|
177 | } // namespace xios |
---|
178 | |
---|
179 | #endif // __XIOS_CObjectFactory_impl__ |
---|