Changeset 146
- Timestamp:
- 08/20/08 17:08:16 (16 years ago)
- Files:
-
- 2 modified
Legend:
- Unmodified
- Added
- Removed
-
bdm/userinfo.h
r142 r146 86 86 class BindingFrame 87 87 { 88 public: 88 protected: 89 BindingFrame() 90 { 91 // trik, chci jen to, aby se ZAVCAS inicializoval!! 92 // tj pred vsema UI apod.. hlavne pred praci s XMLStringem 93 XercesConnector::instance(); 94 } 95 96 public: 97 89 98 // nacpe na prislusne pointery, kdyztak da NULL 90 99 virtual void AssemblyBindedData( DOMElement &element ) = 0; 91 100 92 101 // uvolneni pameti po slozeni objektu, tj. lze jedine po AssemblyBD 93 virtual void ReleaseUsedMemory() = 0; 102 virtual void ReleaseUsedMemory() 103 { 104 } 94 105 95 106 // vrati bool, pokud se povedlo rozebrat a naplnit element … … 97 108 virtual bool DisassemblyBindedData( DOMElement &element ) = 0; 98 109 }; 99 110 /* 111 class BindedAttribute : public BindingFrame 112 { 113 private: 114 const XMLCh* const transcodedAttributeName; 115 116 public: 117 BindedAttribute( string attributeName ) 118 : transcodedAttributeName( XMLString::transcode attributeName.c_str() ) ) 119 { 120 } 121 122 ~BindedAttribute() 123 { 124 XMLString::release( (XMLCh**)&transcodedAttributeName ); 125 } 126 127 // TAM JSOU TY HODNOTY 128 string str; 129 130 // nacpe na prislusne pointery, kdyztak da NULL 131 void AssemblyBindedData( DOMElement &element ) 132 { 133 const XMLCh* const transcoded_str = element.getAttribute( transcodedAttributeName ); 134 str = string( XMLString::transcode( transcoded_str ) ); 135 } 136 137 bool DisassemblyBindedData( DOMElement &element ) 138 { 139 const XMLCh* transcoded_str = XMLString::transcode(str.c_str()); 140 element.setAttribute( transcodedAttributeName, transcoded_str ); 141 XMLString::release( (XMLCh**) &transcoded_str ); 142 } 143 }; 100 144 101 145 /*! … … 110 154 private: 111 155 //! Type definition of mapping which transforms type names to the related user infors 112 typedef map< const string, UserInfo*> Mapped Name2UI;156 typedef map< const string, UserInfo*> MappedString2UI; 113 157 114 158 //! Mapped 115 static Mapped Name2UI& mappedName2UI()116 { 117 static Mapped Name2UI var;159 static MappedString2UI& userFriendlyNames2UI() 160 { 161 static MappedString2UI var; 118 162 return var; 119 163 } 120 164 121 const string underlyingTypeName; 122 123 public: 165 //! Mapped 166 static MappedString2UI& namesByRTTI2UI() 167 { 168 static MappedString2UI var; 169 return var; 170 } 171 172 const string userFriendlyTypeName; 173 public: 174 const string typeNameByRTTI; 124 175 125 176 //! The only constructor which fills both the transcodedTypeName and the help attribute 126 UserInfo( const string& underlyingTypeName ) 127 : underlyingTypeName ( underlyingTypeName ) 177 UserInfo( const string& userFriendlyTypeName, const string& typeNameByRTTI ) 178 : userFriendlyTypeName ( userFriendlyTypeName ), 179 typeNameByRTTI( typeNameByRTTI ) 128 180 { 129 // trik, chci jen to, aby se inicializoval!! 130 XercesConnector::instance(); 131 132 cout << underlyingTypeName; 133 134 mappedName2UI().insert( make_pair( underlyingTypeName, this ) ); 181 userFriendlyNames2UI().insert( make_pair( userFriendlyTypeName, this ) ); 182 namesByRTTI2UI().insert( make_pair( typeNameByRTTI, this ) ); 135 183 } 136 184 … … 142 190 143 191 virtual void BuildInternal(void* &pInstance) = 0; 192 193 virtual void AbsorbInternal(void* &pInstance) = 0; 144 194 145 195 //! returns object of templated type filled with data stored in this CompoundUserInfo instance 146 196 // NULL if error.. 147 197 template<class T> 148 static void Assembly( const string tagName, DOMElement &element, T* &pInstance)198 static void Assembly( T* &pInstance, DOMElement &element, const string tagName ) 149 199 { 150 200 pInstance = NULL; … … 153 203 XMLString::upperCase( transcodedTagName ); 154 204 155 /*/////////////////////////156 DOMNodeList* nodeListe = element.getChildNodes();157 if( nodeListe )158 for( int i = 0; i < nodeListe->getLength(); i++ )159 {160 DOMNode* node = nodeListe->item(i);161 if( node->getNodeType() == DOMNode::ELEMENT_NODE )162 {163 DOMElement* childElement = (DOMElement*) node;164 cout << XercesConnector::instance().XMLCh2str( childElement->getTagName() );165 }166 }167 getchar();168 169 */////////////////////////////170 171 205 172 206 DOMNodeList* const nodeList = element.getElementsByTagName( transcodedTagName ); 173 207 XMLString::release( (XMLCh**)&transcodedTagName ); 174 208 175 if( nodeList != NULL &&nodeList->getLength() != 1 )209 if( nodeList == NULL || nodeList->getLength() != 1 ) 176 210 return; 177 211 178 212 // TAKZE MAME V RUCE ELEMENT SE JMENEM "tagName" 179 DOMElement* childElement = (DOMElement*) nodeList->item(0);213 DOMElement* selectedElement = (DOMElement*) nodeList->item(0); 180 214 181 215 // COZ O TO, UDELAL BYCH TO STATICKOU KONSTANTOU, ALE JAK JI PAK SMAZU?? 182 216 XMLCh* transcodedTypeAttributeName = XMLString::transcode("type"); 183 const XMLCh* const transcodedType = childElement->getAttribute( transcodedTypeAttributeName );217 const XMLCh* const transcodedType = selectedElement->getAttribute( transcodedTypeAttributeName ); 184 218 XMLString::release( (XMLCh**)&transcodedTypeAttributeName ); 185 219 … … 188 222 189 223 // TED MAME V RUCE JMENO TYPU Z ATRIBUTU TYPE 190 string type = XercesConnector::instance().XMLCh2str( transcodedType );191 192 Mapped Name2UI::const_iterator iter = mappedName2UI().find( type);193 if( iter == mappedName2UI().end())224 string userFriendlyTypeName = XercesConnector::instance().XMLCh2str( transcodedType ); 225 226 MappedString2UI::const_iterator iter = userFriendlyNames2UI().find( userFriendlyTypeName ); 227 if( iter == userFriendlyNames2UI().end()) 194 228 return; 195 229 196 230 // A TED PRISLUSNE UI 197 231 UserInfo *relatedUI = iter->second; 198 relatedUI->AssemblyBindedData( * childElement );232 relatedUI->AssemblyBindedData( *selectedElement ); 199 233 relatedUI->BuildInternal( (void*&)pInstance ); 200 234 relatedUI->ReleaseUsedMemory(); … … 205 239 try 206 240 { 207 // takhle to zkontroluju? name musi byt shodne s name nahore!! 208 // takze mame typ T, ale name bud od jeho potomka.. oki?? 209 string resultingType = typeid( *pInstance ).name(); 210 if( resultingType != type ) 241 // typova kontrola "do it yourself":) 242 string resultingTypeNameByRTTI = typeid( *pInstance ).name(); 243 if( resultingTypeNameByRTTI != relatedUI->typeNameByRTTI ) 211 244 { 212 245 cout << "FATAL ERROR!!" << endl; … … 222 255 } 223 256 224 // ? pri destrukci si za ne naopak zodpovida ten, kdo je vytvoril.. protoze 225 // budou zit v tom objektu!! tak jakapak destrkuce.. 257 //! returns object of templated type filled with data stored in this CompoundUserInfo instance 258 // NULL if error.. 259 template<class T> 260 static bool Disassembly( T* pInstance, DOMElement &element, const string tagName, const string help) 261 { 262 if( pInstance == NULL) 263 return false; 264 265 XMLCh* transcodedTagName = XMLString::transcode( tagName.c_str() ); 266 XMLString::upperCase( transcodedTagName ); 267 268 // ADD NEW ELEMENT 269 DOMDocument* pDoc = element.getOwnerDocument(); 270 DOMElement* selectedElement = pDoc->createElement( transcodedTagName ); 271 element.appendChild( selectedElement ); 272 XMLString::release( (XMLCh**)&transcodedTagName ); 273 274 275 // hledame vhodne UI - co ty mapy jeste nejako zapouzdrit?!?! 276 MappedString2UI::const_iterator iter = namesByRTTI2UI().find( typeid(*pInstance).name() ); 277 if( iter == namesByRTTI2UI().end()) 278 return false; 279 UserInfo *relatedUI = iter->second; 280 281 // PRIDAM ATRIBUT TYPE - UDELAT NA TO METODU!! NEBO JESTE LEPE SPECIALNI BINDOVACI OBJEKT.. 282 // opet obousmernej, jen na stringy.. pouziju i u valuedUI.. 283 const XMLCh* transcodedTypeAttributeName = XMLString::transcode("type"); 284 const XMLCh* transcodedUserFriendlyTypeName = XMLString::transcode(relatedUI->userFriendlyTypeName.c_str()); 285 selectedElement->setAttribute( transcodedTypeAttributeName, transcodedUserFriendlyTypeName ); 286 XMLString::release( (XMLCh**)&transcodedTypeAttributeName ); 287 XMLString::release( (XMLCh**)&transcodedUserFriendlyTypeName ); 288 289 if( help.length() ) 290 { 291 // PRIDAM ATRIBUT TYPE 292 const XMLCh* transcodedHelpAttributeName = XMLString::transcode("help"); 293 const XMLCh* transcodedHelpString = XMLString::transcode( help.c_str() ); 294 selectedElement->setAttribute( transcodedHelpAttributeName, transcodedHelpString ); 295 XMLString::release( (XMLCh**)&transcodedHelpAttributeName ); 296 XMLString::release( (XMLCh**)&transcodedHelpString ); 297 } 298 299 300 // A TED PRISLUSNE UI 301 // NASMERUJE UKAZATELE BindingFrames.. nejak lip pojmenovat 302 // sloz z pointeru, rozloz na pointery.. 303 relatedUI->AbsorbInternal( (void*&)pInstance ); 304 // rozebere soucastky 305 return relatedUI->DisassemblyBindedData( *selectedElement ); 306 } 307 308 //! returns object of templated type filled with data stored in this CompoundUserInfo instance 309 // NULL if error.. 310 template<class T> 311 static bool Disassembly( T* pInstance, DOMElement &element, const string tagName ) 312 { 313 return Disassembly( pInstance, element, tagName, "" ); 314 } 226 315 }; 227 316 … … 232 321 233 322 //! The only constructor which fills 234 TypedUserInfo<T>( )235 : UserInfo( typeid(T).name() )323 TypedUserInfo<T>( const string &userFriendlyTypeName) 324 : UserInfo( userFriendlyTypeName, typeid(T).name() ) 236 325 { 237 326 }; … … 251 340 252 341 virtual void Build(T* &pTypedInstance) = 0; 342 343 // FRIEND radeji 344 void AbsorbInternal(void* &pInstance) 345 { 346 T* &pTypedInstance = (T*&) pInstance; 347 Absorb( pTypedInstance ); 348 } 349 350 virtual void Absorb(T* &pTypedInstance) = 0; 253 351 254 352 static const TypedUserInfo<T> &instance; … … 308 406 bool DisassemblyBindedData( DOMElement &element ) 309 407 { 310 // return factory.Disassembly( (void*&) pInstance, element, name, help ); 311 return true; 408 return factory.Disassembly( pInstance, element, name, help ); 312 409 } 313 410 }; 411 412 CompoundUserInfo<T>( string userFriendlyTypeName ) 413 : TypedUserInfo<T>( userFriendlyTypeName ) 414 { 415 } 314 416 315 417 … … 338 440 339 441 340 /*341 //! returns object of templated type filled with data stored in this CompoundUserInfo instance342 bool Disassembly(void * &pInstance, DOMElement &element, string name, char * help = "" )343 {344 T* &pTypedInstance = (T*&) pInstance;345 // do elementu prida vetev se jmenem name a vyplni obsahujici data z instance346 // zde nemuze vzniknout zadna chyba, ponevadz XML vytvarime347 348 /*349 for( MappedElements::const_iterator iter = elements.begin(); iter != elements.end(); iter++)350 {351 iter->second->AppendElement( *pHead );352 {353 DOMDocument* pDoc = element.getOwnerDocument();354 355 DOMElement* pHead = pDoc->createElement( transcodedTypeName );356 element.appendChild( pHead );357 358 FillElement( pHead );359 }360 }361 362 for( MappedAttributes::iterator iter = attributes.begin(); iter != attributes.end(); iter++)363 {364 // we do not create attributes, which are empty365 if( !strcmp( iter->second->c_str(), "") )366 continue;367 368 DOMDocument* pDoc = pHead->getOwnerDocument();369 DOMAttr* pAttribute = pDoc->createAttribute( iter->first );370 371 XMLCh* transcodedValue = XMLString::transcode( iter->second->c_str() );372 pAttribute->setValue( transcodedValue );373 XMLString::release( &transcodedValue );374 375 pHead->setAttributeNode(pAttribute);376 }377 return true;378 }379 */380 381 382 442 template<typename T> class ValuedUserInfo : public TypedUserInfo<T> 383 443 { … … 390 450 string value; 391 451 392 ValuedUserInfo<T>() 393 : transcodedValueName( XMLString::transcode( "value" )) 452 ValuedUserInfo<T>( string userFriendlyTypeName ) 453 : TypedUserInfo<T>( userFriendlyTypeName ), 454 transcodedValueName( XMLString::transcode( "value" )) 394 455 { 395 456 } … … 406 467 } 407 468 408 void ReleaseUsedMemory()409 {410 }411 412 469 bool DisassemblyBindedData( DOMElement &element ) 413 470 { 414 // if( value.length()==0 ) 415 // return false; 471 if( value.length() ) 472 { 473 const XMLCh* transcodedValueString = XMLString::transcode( value.c_str() ); 474 element.setAttribute( transcodedValueName, transcodedValueString ); 475 XMLString::release( (XMLCh**)&transcodedValueString ); 476 } 416 477 return true; 417 478 } -
tests/testUI.cpp
r142 r146 4 4 { 5 5 public: 6 IntUI():ValuedUserInfo<int>("int") 7 { 8 } 6 9 7 10 void Build(int* &pInstance) 8 11 { 9 // KDO SE POSTARA O DEALOKACI TOHOTO? NADRAZENE UI? ASI..10 // PAK TO CHCE ALE OBDOBNE UDELAT I STRING NIZE, ZKOPIROVAT atd11 12 pInstance = new int( atoi( value.c_str()) ); 12 13 } 13 14 15 void Absorb(int* &pInstance) 16 { 17 char buff[30]; 18 sprintf(buff, "%d", *pInstance ); 19 value = buff; 20 } 14 21 }; 15 22 … … 20 27 { 21 28 public: 29 StringUI():ValuedUserInfo<string>("string") 30 { 31 } 32 22 33 void Build(string* &pInstance) 23 34 { 24 pInstance = &value; 35 pInstance = new string( value ); 36 } 37 38 void Absorb(string* &pInstance) 39 { 40 value = *pInstance; 25 41 } 26 42 }; 27 43 28 const TypedUserInfo<string>& StringUI::instance = StringUI(); 44 const TypedUserInfo<string>& StringUI::instance = StringUI( ); 45 46 class Car 47 { 48 public: 49 int age; 50 string brand; 51 52 Car( int age, string brand) 53 : age( age), brand( brand) 54 { 55 } 56 }; 57 58 class CarUI: public CompoundUserInfo<Car> 59 { 60 public: 61 BindedType<int> age; 62 BindedType<string> brand; 63 64 CarUI() 65 :CompoundUserInfo<Car>("car"), 66 age( this, "age" ), 67 brand( this, "brand") 68 { 69 } 70 71 void Build(Car* &pInstance) 72 { 73 if( age.pInstance == NULL ) age.pInstance = new int (0); 74 if( brand.pInstance == NULL ) brand.pInstance = new string("noname"); 75 pInstance = new Car( *age.pInstance, *brand.pInstance ); 76 } 77 78 void Absorb(Car* &pInstance) 79 { 80 age.pInstance = &pInstance->age; 81 brand.pInstance = &pInstance->brand; 82 } 83 }; 84 85 const TypedUserInfo<Car>& CarUI::instance = CarUI( ); 29 86 30 87 31 88 int main() 32 89 { 33 int i = 9, *pi = &i; 90 /////////////////////////////////// SAVING /////////////////////////// 91 Car newCar( 20, "audina" ); 34 92 35 93 RootElement root("car.xml"); 94 // NESLO BY TAM MISTO POINTERU PRIMO REFERENCI, 95 // NEBO TAK NECO?!?! ASI JO..! 96 if( !UserInfo::Disassembly( &newCar,root,"kara") ) 97 { 98 cout << "there was some error!" << endl; 99 getchar(); 100 return 0; 101 } 102 103 root.Save(); 104 cout << "the new car was saved correctly!" << endl; 105 getchar(); 106 107 //////////////////////////////////// LOADING //////////////////////////////// 108 109 110 Car *loadedCar; 36 111 root.Load(); 37 38 UserInfo::Assembly("clovek",root,pi); 112 UserInfo::Assembly(loadedCar,root,"kara"); 39 113 40 //root.Save(); 41 42 cout << "press any key to quit, pi=" << *pi << endl; 114 if( loadedCar ) 115 cout << "loaded car: age=" << loadedCar->age << ", brand=" << loadedCar->brand << endl; 116 else 117 cout << "there was some error during car loading!" << endl; 118 43 119 getchar(); 44 120 return 0;