[591] | 1 | #ifndef __XIOS_CObjectTemplate_impl__ |
---|
| 2 | #define __XIOS_CObjectTemplate_impl__ |
---|
[219] | 3 | |
---|
[591] | 4 | #include "xios_spl.hpp" |
---|
[327] | 5 | #include "context_client.hpp" |
---|
[300] | 6 | #include "object_factory.hpp" |
---|
| 7 | #include "context.hpp" |
---|
| 8 | #include "buffer_in.hpp" |
---|
| 9 | #include "attribute.hpp" |
---|
| 10 | #include "event_client.hpp" |
---|
[327] | 11 | #include "object_template.hpp" |
---|
[300] | 12 | #include "context_client.hpp" |
---|
[327] | 13 | #include "indent.hpp" |
---|
| 14 | #include "type_util.hpp" |
---|
| 15 | #include "message.hpp" |
---|
| 16 | #include "type.hpp" |
---|
[352] | 17 | #include "type_util.hpp" |
---|
| 18 | #include "group_template.hpp" |
---|
[300] | 19 | |
---|
[335] | 20 | namespace xios |
---|
[219] | 21 | { |
---|
| 22 | /// ////////////////////// Définitions ////////////////////// /// |
---|
| 23 | template <class T> |
---|
| 24 | xios_map<StdString, |
---|
| 25 | xios_map<StdString, |
---|
| 26 | boost::shared_ptr<T> > > CObjectTemplate<T>::AllMapObj; |
---|
| 27 | |
---|
| 28 | template <class T> |
---|
| 29 | xios_map<StdString, |
---|
| 30 | std::vector<boost::shared_ptr<T> > > CObjectTemplate<T>::AllVectObj; |
---|
| 31 | |
---|
| 32 | template <class T> |
---|
[286] | 33 | xios_map<StdString,long int> CObjectTemplate<T>::GenId; |
---|
| 34 | |
---|
| 35 | template <class T> |
---|
[219] | 36 | CObjectTemplate<T>::CObjectTemplate(void) |
---|
[345] | 37 | : CAttributeMap() |
---|
[219] | 38 | , CObject() |
---|
| 39 | { /* Ne rien faire de plus */ } |
---|
| 40 | |
---|
| 41 | template <class T> |
---|
| 42 | CObjectTemplate<T>::CObjectTemplate(const StdString & id) |
---|
[345] | 43 | : CAttributeMap() |
---|
[219] | 44 | , CObject(id) |
---|
| 45 | { /* Ne rien faire de plus */ } |
---|
| 46 | |
---|
| 47 | template <class T> |
---|
| 48 | CObjectTemplate<T>::CObjectTemplate |
---|
| 49 | (const CObjectTemplate<T> & object, bool withAttrList, bool withId) |
---|
[345] | 50 | : CAttributeMap() |
---|
[219] | 51 | , CObject() |
---|
| 52 | { |
---|
| 53 | if (object.hasId() && withId) |
---|
| 54 | this->setId(object.getId()); |
---|
| 55 | ERROR("CObjectTemplate<T> construtor 3", << "Not completly implemented yet !"); |
---|
| 56 | } |
---|
[509] | 57 | |
---|
[219] | 58 | template <class T> |
---|
| 59 | CObjectTemplate<T>::~CObjectTemplate(void) |
---|
| 60 | { /* Ne rien faire de plus */ } |
---|
| 61 | |
---|
| 62 | ///-------------------------------------------------------------- |
---|
| 63 | |
---|
| 64 | template <class T> |
---|
| 65 | std::vector<boost::shared_ptr<T> > & |
---|
| 66 | CObjectTemplate<T>::GetAllVectobject(const StdString & contextId) |
---|
[509] | 67 | { |
---|
| 68 | return (CObjectTemplate<T>::AllVectObj[contextId]); |
---|
[219] | 69 | } |
---|
[509] | 70 | |
---|
[219] | 71 | //--------------------------------------------------------------- |
---|
[509] | 72 | |
---|
[219] | 73 | template <class T> |
---|
| 74 | StdString CObjectTemplate<T>::toString(void) const |
---|
| 75 | { |
---|
| 76 | StdOStringStream oss; |
---|
| 77 | oss << "<" << T::GetName(); |
---|
| 78 | if (this->hasId()) |
---|
| 79 | oss << " id=\"" << this->getId() << "\""; |
---|
| 80 | oss << " " << SuperClassMap::toString() << "/>"; |
---|
| 81 | return (oss.str()); |
---|
| 82 | } |
---|
| 83 | |
---|
| 84 | template <class T> |
---|
| 85 | void CObjectTemplate<T>::fromString(const StdString & str) |
---|
[509] | 86 | { |
---|
[219] | 87 | ERROR("CObjectTemplate<T>::fromString(str)", |
---|
[509] | 88 | << "[ str = " << str << "] Not implemented yet !"); |
---|
[219] | 89 | } |
---|
[509] | 90 | |
---|
[219] | 91 | //--------------------------------------------------------------- |
---|
[369] | 92 | |
---|
[509] | 93 | /* |
---|
[219] | 94 | template <class T> |
---|
| 95 | void CObjectTemplate<T>::toBinary(StdOStream & os) const |
---|
| 96 | { |
---|
[509] | 97 | SuperClassMap::toBinary(os); |
---|
[219] | 98 | } |
---|
[509] | 99 | |
---|
[219] | 100 | template <class T> |
---|
| 101 | void CObjectTemplate<T>::fromBinary(StdIStream & is) |
---|
| 102 | { |
---|
[509] | 103 | SuperClassMap::fromBinary(is); |
---|
[219] | 104 | } |
---|
[369] | 105 | */ |
---|
| 106 | |
---|
[219] | 107 | //--------------------------------------------------------------- |
---|
| 108 | |
---|
| 109 | template <class T> |
---|
| 110 | void CObjectTemplate<T>::parse(xml::CXMLNode & node) |
---|
| 111 | { |
---|
| 112 | xml::THashAttributes attributes = node.getAttributes(); |
---|
| 113 | CAttributeMap::setAttributes(attributes); |
---|
| 114 | } |
---|
| 115 | |
---|
| 116 | //--------------------------------------------------------------- |
---|
| 117 | |
---|
| 118 | template <class T> |
---|
[345] | 119 | ENodeType CObjectTemplate<T>::getType(void) const |
---|
[219] | 120 | { |
---|
| 121 | return (T::GetType()); |
---|
| 122 | } |
---|
[509] | 123 | |
---|
[313] | 124 | template <class T> |
---|
| 125 | string CObjectTemplate<T>::getName(void) const |
---|
| 126 | { |
---|
| 127 | return (T::GetName()); |
---|
| 128 | } |
---|
[509] | 129 | |
---|
[219] | 130 | //--------------------------------------------------------------- |
---|
| 131 | |
---|
| 132 | template <class T> |
---|
| 133 | bool CObjectTemplate<T>::hasChild(void) const |
---|
[509] | 134 | { |
---|
| 135 | return (false); |
---|
[219] | 136 | } |
---|
| 137 | |
---|
| 138 | //--------------------------------------------------------------- |
---|
| 139 | |
---|
| 140 | template <class T> |
---|
[445] | 141 | void CObjectTemplate<T>::solveDescInheritance(bool apply, const CAttributeMap * const parent) |
---|
[509] | 142 | { |
---|
[549] | 143 | if (parent != NULL) |
---|
| 144 | SuperClassMap::setAttributes(parent, apply); |
---|
[219] | 145 | } |
---|
| 146 | |
---|
| 147 | //--------------------------------------------------------------- |
---|
| 148 | |
---|
| 149 | template <class T> |
---|
| 150 | void CObjectTemplate<T>::ClearAllAttributes(void) |
---|
| 151 | { |
---|
[549] | 152 | vector<T*> avect = CObjectTemplate<T>::getAll(); |
---|
[347] | 153 | typename vector<T*>::iterator |
---|
[219] | 154 | it = avect.begin(), end = avect.end(); |
---|
| 155 | |
---|
| 156 | for (;it != end; it++) |
---|
| 157 | { |
---|
| 158 | CAttributeMap & amap = **it; |
---|
| 159 | amap.clearAllAttributes(); |
---|
| 160 | } |
---|
| 161 | } |
---|
| 162 | |
---|
[509] | 163 | template<typename T> |
---|
| 164 | void CObjectTemplate<T>::sendAllAttributesToServer() |
---|
| 165 | { |
---|
| 166 | CAttributeMap& attrMap = *this; |
---|
| 167 | CAttributeMap::const_iterator it = attrMap.begin(), itE = attrMap.end(); |
---|
| 168 | for (; it != itE; ++it) |
---|
| 169 | { |
---|
| 170 | if (!(it->second)->isEmpty()) sendAttributToServer(*(it->second)); |
---|
| 171 | } |
---|
| 172 | |
---|
| 173 | } |
---|
| 174 | |
---|
[300] | 175 | template <class T> |
---|
| 176 | void CObjectTemplate<T>::sendAttributToServer(const string& id) |
---|
| 177 | { |
---|
| 178 | CAttributeMap & attrMap = *this; |
---|
[549] | 179 | CAttribute* attr=attrMap[id]; |
---|
| 180 | sendAttributToServer(*attr); |
---|
[300] | 181 | } |
---|
[219] | 182 | |
---|
[347] | 183 | template <class T> |
---|
| 184 | void CObjectTemplate<T>::sendAttributToServer(CAttribute& attr) |
---|
| 185 | { |
---|
[549] | 186 | CContext* context=CContext::getCurrent(); |
---|
[509] | 187 | |
---|
[300] | 188 | if (!context->hasServer) |
---|
| 189 | { |
---|
[549] | 190 | CContextClient* client=context->client; |
---|
[300] | 191 | |
---|
[549] | 192 | CEventClient event(getType(),EVENT_ID_SEND_ATTRIBUTE); |
---|
[300] | 193 | if (client->isServerLeader()) |
---|
| 194 | { |
---|
[549] | 195 | CMessage msg; |
---|
[581] | 196 | // msg << this->getId(); |
---|
| 197 | msg << this->getIdServer(); |
---|
| 198 | msg << attr.getName(); |
---|
| 199 | msg << attr; |
---|
[595] | 200 | const std::list<int>& ranks = client->getRanksServerLeader(); |
---|
| 201 | for (std::list<int>::const_iterator itRank = ranks.begin(), itRankEnd = ranks.end(); itRank != itRankEnd; ++itRank) |
---|
| 202 | event.push(*itRank,1,msg); |
---|
[549] | 203 | client->sendEvent(event); |
---|
[300] | 204 | } |
---|
[549] | 205 | else client->sendEvent(event); |
---|
[300] | 206 | } |
---|
[509] | 207 | |
---|
[347] | 208 | } |
---|
[509] | 209 | |
---|
[347] | 210 | template <class T> |
---|
| 211 | void CObjectTemplate<T>::recvAttributFromClient(CEventServer& event) |
---|
| 212 | { |
---|
[509] | 213 | |
---|
[347] | 214 | CBufferIn* buffer=event.subEvents.begin()->buffer; |
---|
| 215 | string id,attrId; |
---|
[549] | 216 | *buffer>>id; |
---|
[347] | 217 | CAttributeMap & attrMap = *get(id); |
---|
[549] | 218 | *buffer>>attrId; |
---|
| 219 | CAttribute* attr=attrMap[attrId]; |
---|
[581] | 220 | info(50) << "attribut recu " << attrId << " "; |
---|
| 221 | if (attr->isEmpty()) info(50) << "--> empty" << endl; |
---|
| 222 | else info(50) /*<attr->getValue()*/ << endl; |
---|
[549] | 223 | *buffer>>*attr; |
---|
[581] | 224 | info(50) << "attribut recu " << attrId << " "; |
---|
| 225 | if (attr->isEmpty()) info(50) << "--> empty" << endl; |
---|
| 226 | else info(50) /*attr->getValue()*/ << endl; |
---|
[300] | 227 | } |
---|
| 228 | |
---|
| 229 | template <class T> |
---|
| 230 | bool CObjectTemplate<T>::dispatchEvent(CEventServer& event) |
---|
| 231 | { |
---|
| 232 | switch(event.type) |
---|
| 233 | { |
---|
| 234 | case EVENT_ID_SEND_ATTRIBUTE : |
---|
[549] | 235 | recvAttributFromClient(event); |
---|
| 236 | return true; |
---|
| 237 | break; |
---|
[509] | 238 | |
---|
[300] | 239 | default : |
---|
[549] | 240 | return false; |
---|
[581] | 241 | // ERROR("void CObjectTemplate<T>::recvEvent(CEventServer& event)", << "Unknown Event"); |
---|
[300] | 242 | } |
---|
| 243 | } |
---|
[509] | 244 | |
---|
[300] | 245 | template <typename T> |
---|
| 246 | bool CObjectTemplate<T>::has(const string & id) |
---|
| 247 | { |
---|
[549] | 248 | return CObjectFactory::HasObject<T>(id); |
---|
[300] | 249 | } |
---|
| 250 | |
---|
| 251 | template <typename T> |
---|
[346] | 252 | bool CObjectTemplate<T>::has(const string& contextId, const string & id) |
---|
| 253 | { |
---|
[549] | 254 | return CObjectFactory::HasObject<T>(contextId,id); |
---|
[346] | 255 | } |
---|
| 256 | |
---|
| 257 | template <typename T> |
---|
[347] | 258 | T* CObjectTemplate<T>::get(const string & id) |
---|
[300] | 259 | { |
---|
[549] | 260 | return CObjectFactory::GetObject<T>(id).get(); |
---|
[300] | 261 | } |
---|
| 262 | |
---|
| 263 | template <typename T> |
---|
[347] | 264 | T* CObjectTemplate<T>::get(const T* ptr) |
---|
[346] | 265 | { |
---|
[549] | 266 | return CObjectFactory::GetObject<T>(ptr).get(); |
---|
[347] | 267 | } |
---|
[509] | 268 | |
---|
[347] | 269 | template <typename T> |
---|
| 270 | shared_ptr<T> CObjectTemplate<T>::getShared(const T* ptr) |
---|
| 271 | { |
---|
[549] | 272 | return CObjectFactory::GetObject<T>(ptr); |
---|
[346] | 273 | } |
---|
[347] | 274 | |
---|
| 275 | template <typename T> |
---|
| 276 | shared_ptr<T> CObjectTemplate<T>::getShared(void) |
---|
| 277 | { |
---|
[549] | 278 | return CObjectFactory::GetObject<T>((T*)this); |
---|
[347] | 279 | } |
---|
[509] | 280 | |
---|
[346] | 281 | template <typename T> |
---|
[347] | 282 | const vector<T*> CObjectTemplate<T>::getAll() |
---|
[346] | 283 | { |
---|
[347] | 284 | const vector< shared_ptr<T> >& shared_vect= CObjectFactory::GetObjectVector<T>(); |
---|
[549] | 285 | vector<T*> vect; |
---|
[509] | 286 | |
---|
[347] | 287 | typename vector<shared_ptr<T> >::const_iterator it; |
---|
[549] | 288 | for(it=shared_vect.begin();it!=shared_vect.end();++it) vect.push_back(it->get()); |
---|
| 289 | return vect; |
---|
[346] | 290 | } |
---|
| 291 | |
---|
| 292 | template <typename T> |
---|
[347] | 293 | const vector<T*> CObjectTemplate<T>::getAll(const string & id) |
---|
[346] | 294 | { |
---|
[347] | 295 | const vector< shared_ptr<T> >& shared_vect= CObjectFactory::GetObjectVector<T>(id); |
---|
[549] | 296 | vector<T*> vect; |
---|
[509] | 297 | |
---|
[347] | 298 | typename vector<shared_ptr<T> >::const_iterator it; |
---|
[549] | 299 | for(it=shared_vect.begin();it!=shared_vect.end();++it) vect.push_back(it->get()); |
---|
| 300 | return vect; |
---|
[346] | 301 | } |
---|
| 302 | |
---|
| 303 | template <typename T> |
---|
[347] | 304 | T* CObjectTemplate<T>::get(const string& contextId, const string & id) |
---|
[346] | 305 | { |
---|
[549] | 306 | return CObjectFactory::GetObject<T>(contextId,id).get(); |
---|
[346] | 307 | } |
---|
| 308 | |
---|
| 309 | template <typename T> |
---|
[347] | 310 | T* CObjectTemplate<T>::create(const string & id) |
---|
[300] | 311 | { |
---|
[549] | 312 | return CObjectFactory::CreateObject<T>(id).get(); |
---|
[300] | 313 | } ///-------------------------------------------------------------- |
---|
| 314 | |
---|
| 315 | template <typename T> |
---|
[347] | 316 | T* CObjectTemplate<T>::get(void) |
---|
[300] | 317 | { |
---|
[549] | 318 | return CObjectFactory::GetObject<T>((T*)this).get(); |
---|
[300] | 319 | } |
---|
[509] | 320 | |
---|
[313] | 321 | template <typename T> |
---|
| 322 | void CObjectTemplate<T>::generateCInterface(ostream& oss) |
---|
| 323 | { |
---|
[549] | 324 | string className=getName(); |
---|
| 325 | int found=className.rfind("_group"); |
---|
| 326 | if (found!=string::npos) className.replace(found,1,0,'x'); |
---|
[509] | 327 | |
---|
[581] | 328 | oss << "/* ************************************************************************** *" << iendl; |
---|
| 329 | oss << " * Interface auto generated - do not modify *" << iendl; |
---|
| 330 | oss << " * ************************************************************************** */" << iendl; |
---|
| 331 | oss << iendl; |
---|
| 332 | oss << "#include <boost/multi_array.hpp>" << iendl; |
---|
| 333 | oss << "#include <boost/shared_ptr.hpp>" << iendl; |
---|
[591] | 334 | oss << "#include \"xios.hpp\"" << iendl; |
---|
[581] | 335 | oss << "#include \"attribute_template.hpp\"" << iendl; |
---|
| 336 | oss << "#include \"object_template.hpp\"" << iendl; |
---|
| 337 | oss << "#include \"group_template.hpp\"" << iendl; |
---|
| 338 | oss << "#include \"icutil.hpp\"" << iendl; |
---|
| 339 | oss << "#include \"icdate.hpp\"" << iendl; |
---|
| 340 | oss << "#include \"timer.hpp\"" << iendl; |
---|
| 341 | oss << "#include \"node_type.hpp\"" << iendl; |
---|
| 342 | oss << iendl; |
---|
| 343 | oss << "extern \"C\"" << iendl; |
---|
| 344 | oss << "{" << iendl++; |
---|
| 345 | oss << "typedef xios::" << getStrType<T>() << "* " << className << "_Ptr;"; |
---|
[549] | 346 | SuperClassMap::generateCInterface(oss,className); |
---|
[581] | 347 | oss << "}" << iendl--; |
---|
[313] | 348 | } |
---|
[300] | 349 | |
---|
[313] | 350 | template <typename T> |
---|
| 351 | void CObjectTemplate<T>::generateFortran2003Interface(ostream& oss) |
---|
| 352 | { |
---|
[549] | 353 | string className=getName(); |
---|
| 354 | int found=className.rfind("_group"); |
---|
| 355 | if (found!=string::npos) className.replace(found,1,0,'x'); |
---|
[509] | 356 | |
---|
[581] | 357 | oss << "! * ************************************************************************** *" << iendl; |
---|
| 358 | oss << "! * Interface auto generated - do not modify *" << iendl; |
---|
| 359 | oss << "! * ************************************************************************** *" << iendl; |
---|
| 360 | oss << "#include \"../fortran/xios_fortran_prefix.hpp\"" << iendl; |
---|
| 361 | oss << iendl; |
---|
| 362 | oss << "MODULE " << className << "_interface_attr" << iendl++; |
---|
| 363 | oss << "USE, INTRINSIC :: ISO_C_BINDING" << std::endl; |
---|
| 364 | oss << iendl; |
---|
| 365 | oss << "INTERFACE" << iendl++; |
---|
| 366 | oss << "! Do not call directly / interface FORTRAN 2003 <-> C99"; |
---|
[549] | 367 | SuperClassMap::generateFortran2003Interface(oss,className); |
---|
[581] | 368 | oss << iendl--; |
---|
| 369 | oss << "END INTERFACE" << iendl--; |
---|
| 370 | oss << iendl; |
---|
| 371 | oss << "END MODULE " << className << "_interface_attr" << iendl; |
---|
[313] | 372 | } |
---|
[509] | 373 | |
---|
[313] | 374 | template <typename T> |
---|
| 375 | void CObjectTemplate<T>::generateFortranInterface(ostream& oss) |
---|
| 376 | { |
---|
[549] | 377 | string className=getName(); |
---|
| 378 | int found=className.rfind("_group"); |
---|
| 379 | if (found!=string::npos) className.erase(found,1); |
---|
[313] | 380 | string superClassName=getName(); |
---|
[549] | 381 | found=superClassName.find("_group"); |
---|
| 382 | if (found!=string::npos) superClassName.erase(found,6); |
---|
[509] | 383 | |
---|
[581] | 384 | oss << "! * ************************************************************************** *" << iendl; |
---|
| 385 | oss << "! * Interface auto generated - do not modify *" << iendl; |
---|
| 386 | oss << "! * ************************************************************************** *" << iendl; |
---|
| 387 | oss << "#include \"xios_fortran_prefix.hpp\"" << iendl; |
---|
| 388 | oss << iendl; |
---|
| 389 | oss << "MODULE i" << className << "_attr" << iendl++; |
---|
| 390 | oss << "USE, INTRINSIC :: ISO_C_BINDING" << iendl; |
---|
| 391 | oss << "USE i" << superClassName << iendl; |
---|
| 392 | oss << "USE " << className << "_interface_attr" << iendl--; |
---|
| 393 | // oss << "TYPE txios(" << className << ")" << iendl; |
---|
| 394 | // oss << " INTEGER(kind = C_INTPTR_T) :: daddr" << iendl; |
---|
| 395 | // oss << "END TYPE txios(" << className << ")" << iendl; |
---|
| 396 | oss << iendl; |
---|
| 397 | oss << "CONTAINS" << iendl; |
---|
| 398 | oss << iendl++; |
---|
[549] | 399 | SuperClassMap::generateFortranInterface_id(oss,className); |
---|
[581] | 400 | oss << iendl; |
---|
[549] | 401 | SuperClassMap::generateFortranInterface_hdl(oss,className); |
---|
[581] | 402 | oss << iendl; |
---|
[549] | 403 | SuperClassMap::generateFortranInterface_hdl_(oss,className); |
---|
[581] | 404 | oss << iendl; |
---|
[549] | 405 | SuperClassMap::generateFortranInterfaceGet_id(oss,className); |
---|
[581] | 406 | oss << iendl; |
---|
[549] | 407 | SuperClassMap::generateFortranInterfaceGet_hdl(oss,className); |
---|
[581] | 408 | oss << iendl; |
---|
[549] | 409 | SuperClassMap::generateFortranInterfaceGet_hdl_(oss,className); |
---|
[581] | 410 | oss << iendl; |
---|
[549] | 411 | SuperClassMap::generateFortranInterfaceIsDefined_id(oss,className); |
---|
[581] | 412 | oss << iendl; |
---|
[549] | 413 | SuperClassMap::generateFortranInterfaceIsDefined_hdl(oss,className); |
---|
[581] | 414 | oss << iendl; |
---|
[549] | 415 | SuperClassMap::generateFortranInterfaceIsDefined_hdl_(oss,className); |
---|
[581] | 416 | oss << iendl--; |
---|
| 417 | oss << "END MODULE i" << className << "_attr" << iendl; |
---|
[313] | 418 | } |
---|
[335] | 419 | } // namespace xios |
---|
[219] | 420 | |
---|
[591] | 421 | #endif // __XIOS_CObjectTemplate_impl__ |
---|