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