| |
libFAUDES
Sections
Index
|
basevector.hGo to the documentation of this file.00001 /** @file basevector.h @brief Class TBaseVector */ 00002 00003 00004 /* FAU Discrete Event Systems Library (libfaudes) 00005 00006 Copyright (C) 2009 Thomas Moor 00007 00008 This library is free software; you can redistribute it and/or 00009 modify it under the terms of the GNU Lesser General Public 00010 License as published by the Free Software Foundation; either 00011 version 2.1 of the License, or (at your option) any later version. 00012 00013 This library is distributed in the hope that it will be useful, 00014 but WITHOUT ANY WARRANTY; without even the implied warranty of 00015 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00016 Lesser General Public License for more details. 00017 00018 You should have received a copy of the GNU Lesser General Public 00019 License along with this library; if not, write to the Free Software 00020 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 00021 */ 00022 00023 00024 00025 #ifndef FAUDES_BASEVECTOR_H 00026 #define FAUDES_BASEVECTOR_H 00027 00028 #include "definitions.h" 00029 #include "tokenwriter.h" 00030 #include "tokenreader.h" 00031 #include "rtitypes.h" 00032 #include "attributes.h" 00033 #include <vector> 00034 #include <algorithm> 00035 00036 namespace faudes { 00037 00038 /** @addtogroup ContainerClasses */ 00039 /** @{*/ 00040 00041 /** 00042 * Vector template. 00043 * 00044 * This class is designed as a random access container for a small number of 00045 * comperatively large objects, eg a vector of generators to model a decentralized DES. 00046 * The API is restricted to simple positional access and there are no explicit 00047 * iterators nor is there a deferred copy mechanism. 00048 * 00049 * Internally, the Vector template uses STL vector of pointers to the individual entries. 00050 * When adding an entry, you may either do so be refernce or by pointer. When setting 00051 * by reference, the vector takes a copy and owns the new entry. When setting by a pointer, 00052 * the vector only records the reference. The vector tracks ownership of each entry in order 00053 * to properly destruct entries. 00054 * 00055 * TVectorSet serves as a base class for all libFaudes vectors: 00056 * - GeneratorVector (vector or generators) 00057 * - EventSetVector (vector of event sets) 00058 * 00059 * Token io assumes that the type parameter is a faudes type, ie. entries of the vector 00060 * provide token io themselfs. To derive a vector class with non-faudes-type entries, 00061 * you will need to reimplement token io. As a convenience feature, the vector keeps a record 00062 * of filenames associated with individual entries. You can inspect and edit this record via 00063 * the FilenameAt members. When writing the vector to a file and all entries have an associated filename, 00064 * output will be to the individual files. 00065 * 00066 */ 00067 00068 template<class T> 00069 class TBaseVector : public Type { 00070 00071 FAUDES_TYPE_DECLARATION(TBaseVector,Type) 00072 00073 public: 00074 00075 /** 00076 * Constructor. 00077 */ 00078 TBaseVector(void); 00079 00080 /** 00081 * Copy-constructor. 00082 * 00083 * @param rOtherSet 00084 * Source to copy from 00085 */ 00086 TBaseVector(const TBaseVector& rOtherSet); 00087 00088 /** 00089 * Constructor from file. 00090 * 00091 * @param rFilename 00092 * Name of File 00093 * @param rLabel 00094 * Section for the set in the file; 00095 */ 00096 TBaseVector(const std::string& rFilename, const std::string& rLabel = "BaseVector"); 00097 00098 /** 00099 * Virtual destructor 00100 */ 00101 virtual ~TBaseVector(void); 00102 00103 /** 00104 * Return name of TBaseVector 00105 * 00106 * @return 00107 * Name of TBaseVector 00108 */ 00109 const std::string& Name(void) const; 00110 00111 /** 00112 * Set name of TBaseVector 00113 * 00114 * @param rName 00115 * Name to set 00116 */ 00117 void Name(const std::string& rName); 00118 00119 /** 00120 * Clear all vector 00121 */ 00122 virtual void Clear(void); 00123 00124 /** 00125 * Get size of vector. 00126 * 00127 * @return 00128 * Number of entries. 00129 */ 00130 Idx Size(void) const; 00131 00132 /** 00133 * Set size of vector. 00134 * If the new size is smaller than the current size, 00135 * the vector is truncated. If it is larger, default members 00136 * are inserted at the end. 00137 * 00138 * @param len 00139 * Number of entries in vector 00140 */ 00141 void Size(Idx len); 00142 00143 /** 00144 * Check if the TBaseVector ist Empty 00145 * 00146 * @return 00147 * True if empty 00148 */ 00149 bool Empty(void) const; 00150 00151 /** 00152 * convenience typedef for positions 00153 */ 00154 typedef std::vector<int>::size_type Position; 00155 00156 /** 00157 * Access elemend. 00158 * 00159 * @param pos 00160 * Specify entry to access 00161 * @exception Exception 00162 * - Position out of range (id 69) 00163 */ 00164 virtual const T& At(const Position& pos) const; 00165 00166 /** 00167 * Access elemend. 00168 * 00169 * @param pos 00170 * Specify entry to access 00171 * @exception Exception 00172 * - Position out of range (id 69) 00173 */ 00174 virtual T& At(const Position& pos); 00175 00176 00177 /** 00178 * Replace specified entry. 00179 * This method takes a copy of the entry to replace and the 00180 * vector becomes the owner of the copy. 00181 * 00182 * @param pos 00183 * Position to replace 00184 * @param rElem 00185 * New entry 00186 * @exception Exception 00187 * - Position out of range (id 69) 00188 */ 00189 virtual void Replace(const Position& pos, const T& rElem); 00190 00191 /** 00192 * Replace specified entry. 00193 * This method avoids to copy the entry to replace and only records the reference. 00194 * The vector does not take ownership of the new entry. I.e., when the vector is destroyed, or the 00195 * entry is deleted from the vector, the entry itself remains allocated. 00196 * 00197 * @param pos 00198 * Position to replace 00199 * @param pElem 00200 * New entry 00201 * @exception Exception 00202 * - Position out of range (id 69) 00203 */ 00204 virtual void Replace(const Position& pos, T* pElem); 00205 00206 /** 00207 * Replace specified entry. 00208 * This method reads the sepcified entry from file and the 00209 * vector becomes the owner of the new entry. 00210 * 00211 * @param pos 00212 * Position to replace 00213 * @param rFileName 00214 * New entry to be read from file 00215 * @exception Exception 00216 * - Position out of range (id 69) 00217 */ 00218 virtual void Replace(const Position& pos, const std::string& rFileName); 00219 00220 00221 /** 00222 * Erase entry by position. 00223 * If the vector owns the entry, it will be destructed. 00224 * 00225 * @param pos 00226 * Specify entry to erase 00227 * @exception Exception 00228 * - Position out of range (id 69) 00229 */ 00230 virtual void Erase(const Position& pos); 00231 00232 /** 00233 * Insert specified entry. 00234 * This method takes a copy of the entry to replace and the 00235 * vector becomes the owner of the copy. 00236 * 00237 * @param pos 00238 * Position at which to insert 00239 * @param rElem 00240 * Element to insert 00241 * @exception Exception 00242 * - Position out of range (id 69) 00243 */ 00244 virtual void Insert(const Position& pos, const T& rElem); 00245 00246 /** 00247 * Insert specified entry. 00248 * This method avoids to copy the entry to replace and only records the reference. 00249 * The vector does not take ownership of the new entry. I.e., when the vector is destroyed, or the 00250 * entry is deleted from the vector, the entry itself remains allocated. 00251 * 00252 * 00253 * @param pos 00254 * Position at which to insert 00255 * @param rElem 00256 * Element to insert 00257 * @exception Exception 00258 * - Position out of range (id 69) 00259 */ 00260 virtual void Insert(const Position& pos, T* rElem); 00261 00262 /** 00263 * Insert specified entry. 00264 * This method reads the sepcified entry from file and the 00265 * vector becomes the owner of the new entry. 00266 * 00267 * @param pos 00268 * Position at which to insert 00269 * @param rFileName 00270 * Element to insert 00271 * @exception Exception 00272 * - Position out of range (id 69) 00273 */ 00274 virtual void Insert(const Position& pos, const std::string& rFileName); 00275 00276 /** 00277 * Append specified entry. 00278 * This method takes a copy of the entry to replace and the 00279 * vector becomes the owner of the copy. 00280 * 00281 * @param rElem 00282 * Element to append 00283 */ 00284 virtual void PushBack(const T& rElem); 00285 00286 /** 00287 * Append specified entry. 00288 * This method avoids to copy the entry to replace and only records the reference. 00289 * The vector does not take ownership of the new entry. I.e., when the vector is destroyed, or the 00290 * entry is deleted from the vector, the entry itself remains allocated. 00291 * 00292 * 00293 * @param rElem 00294 * Element to insert 00295 */ 00296 virtual void PushBack(T* rElem); 00297 00298 /** 00299 * Append specified entry. 00300 * This method reads the sepcified entry from file and the 00301 * vector becomes the owner of the new entry. 00302 * 00303 * @param rFileName 00304 * Element to insert 00305 */ 00306 virtual void PushBack(const std::string& rFileName); 00307 00308 /** 00309 * Append specified entry. 00310 * Synonymous for PushBack. 00311 * This method takes a copy of the entry to replace and the 00312 * vector becomes the owner of the copy. 00313 * 00314 * @param rElem 00315 * Element to append 00316 */ 00317 virtual void Append(const T& rElem); 00318 00319 /** 00320 * Append specified entry. 00321 * Synonymous for PushBack. 00322 * This method avoids to copy the entry to replace and only records the reference. 00323 * The vector does not take ownership of the new entry. I.e., when the vector is destroyed, or the 00324 * entry is deleted from the vector, the entry itself remains allocated. 00325 * 00326 * 00327 * @param rElem 00328 * Element to insert 00329 */ 00330 virtual void Append(T* rElem); 00331 00332 /** 00333 * Append specified entry. 00334 * Synonymous for PushBack. 00335 * This method reads the sepcified entry from file and the 00336 * vector becomes the owner of the new entry. 00337 * 00338 * @param rFileName 00339 * Element to insert 00340 */ 00341 virtual void Append(const std::string& rFileName); 00342 00343 /** 00344 * Specify a filename. 00345 * When each entry has a filenam specified, 00346 * file io of the vector will be to indivudual files. 00347 * 00348 * @param pos 00349 * Position of entry 00350 * @param rFileName 00351 * Filename relative to vector file 00352 * @exception Exception 00353 * - Position out of range (id 69) 00354 */ 00355 void FilenameAt(const Position& pos, const std::string& rFileName); 00356 00357 /** 00358 * Get filename of entry. 00359 * 00360 * @param pos 00361 * Position of entry 00362 * @return 00363 * Filename assoiated with entry 00364 * @exception Exception 00365 * - Position out of range (id 69) 00366 */ 00367 const std::string& FilenameAt(const Position& pos) const; 00368 00369 /** 00370 * Take ownership of all entries. 00371 * Thsi method will take ownership of all entries, including those, that 00372 * have been set by pointer reference. When the vector is destructed, all 00373 * entries will be destructed, too. However, write access may invalidate 00374 * element pointers. 00375 * 00376 */ 00377 void TakeOwnership(void); 00378 00379 /** 00380 * Take local copies of all entries. 00381 * This method will construct local copies of all entries not previously 00382 * owned. 00383 */ 00384 void TakeCopies(void); 00385 00386 protected: 00387 00388 00389 /** 00390 * Token output, debugging see Type::DWrite for public wrappers. 00391 * The method assumes that the type parameter is a faudes type and uses 00392 * the provide write method per entry. Reimplement this function in derived 00393 * classes for non-faudes type vectors. 00394 * @param rTw 00395 * Reference to TokenWriter 00396 * @param rLabel 00397 * Label of section to write, defaults to name of set 00398 * @param pContext 00399 * Write context to provide contextual information 00400 */ 00401 virtual void DoDWrite(TokenWriter& rTw,const std::string& rLabel="", const Type* pContext=0) const; 00402 00403 /** 00404 * Token output, see Type::SWrite for public wrappers. 00405 * The method assumes that the type parameter is a faudes type and uses 00406 * the provide write method per entry. Reimplement this function in derived 00407 * classes for non-faudes type vectors. 00408 * 00409 * @param rTw 00410 * Reference to TokenWriter 00411 * 00412 * @exception Exception 00413 * - IO errors (id 2) 00414 */ 00415 virtual void DoSWrite(TokenWriter& rTw) const; 00416 00417 /** 00418 * Token output, see Type::Write for public wrappers. 00419 * The method assumes that the type parameter is a faudes type and uses 00420 * the provide write method per entry. Reimplement this function in derived 00421 * classes for non-faudes type vectors. 00422 * 00423 * @param rTw 00424 * Reference to TokenWriter 00425 * @param rLabel 00426 * Label of section to write, defaults to name of set 00427 * @param pContext 00428 * Write context to provide contextual information 00429 */ 00430 virtual void DoWrite(TokenWriter& rTw, const std::string& rLabel="", const Type* pContext=0) const; 00431 00432 /** 00433 * Token input, see Type::Read for public wrappers. 00434 * The method assumes that the type parameter is a faudes type and uses 00435 * the provide read method per entry. Reimplement this function in derived 00436 * classes for non-faudes type vectors. 00437 * By convention, the default label "" should be translated to some meaningful default, 00438 * eg "GeneratorVector" for a vector of generators. The pContext pointer can be type-checked 00439 * and interpreted, ie as a symboltable to provide symbolic names. It is also 00440 * passed on to vector entries. 00441 * 00442 * @param rTr 00443 * Reference to TokenReader 00444 * @param rLabel 00445 * Label of section to read, defaults to name of set 00446 * @param pContext 00447 * Read context to provide contextual information 00448 */ 00449 virtual void DoRead(TokenReader& rTr, const std::string& rLabel = "", const Type* pContext=0); 00450 00451 /** Assignment method */ 00452 virtual TBaseVector& DoAssign(const TBaseVector<T>& rSourceVector); 00453 00454 /** Internal entry data type */ 00455 class Element { 00456 public: 00457 T* pElement; 00458 std::string mFileName; 00459 bool mMine; 00460 }; 00461 00462 /** STL vector of element */ 00463 std::vector<Element> mVector; 00464 00465 /** convenience typedef */ 00466 typedef typename std::vector<Element>::iterator iterator; 00467 00468 private: 00469 00470 /** Name of TBaseVector */ 00471 std::string mMyName; 00472 00473 }; 00474 00475 00476 /** @} doxygen group */ 00477 00478 00479 00480 /* 00481 ****************************************************************************************** 00482 ****************************************************************************************** 00483 ****************************************************************************************** 00484 00485 Implementation of TBaseVector 00486 00487 ****************************************************************************************** 00488 ****************************************************************************************** 00489 ****************************************************************************************** 00490 */ 00491 00492 /* convenience access to relevant scopes */ 00493 #define THIS TBaseVector<T> 00494 #define TEMP template<class T> 00495 #define BASE Type 00496 00497 00498 // faudes type std 00499 FAUDES_TYPE_IMPLEMENTATION(THIS,Type,TEMP) 00500 00501 00502 // TBaseVector() 00503 TEMP THIS::TBaseVector(void) : 00504 Type() 00505 { 00506 FD_DC("TBaseVector(" << this << ")::TBaseVector()"); 00507 // my members 00508 mMyName="Vector"; 00509 } 00510 00511 00512 // TBaseVector(filename) 00513 TEMP THIS::TBaseVector(const std::string& rFileName, const std::string& rLabel) : 00514 Type() 00515 { 00516 FD_DC("TBaseVector(" << this << ")::TBaseVector()"); 00517 // other members 00518 mMyName="Vector"; 00519 // do read; 00520 Read(rFileName,rLabel); 00521 } 00522 00523 00524 // TBaseVector(rOtherSet) 00525 TEMP THIS::TBaseVector(const TBaseVector& rOtherVector) : 00526 Type(rOtherVector) 00527 { 00528 FD_DC("TBaseVector(" << this << ")::TBaseVector(rOtherVector " << &rOtherVector << "): fake copy construct"); 00529 DoAssign(rOtherVector); 00530 } 00531 00532 // destructor 00533 TEMP THIS::~TBaseVector(void) { 00534 FD_DC("TBaseVector(" << this << ")::~TBaseVector()"); 00535 // delete entries 00536 for(Position pos=0; pos<mVector.size(); pos++) 00537 if(mVector[pos].mMine) delete mVector[pos].pElement; 00538 // done 00539 FD_DC("TBaseVector(" << this << ")::~TBaseVector(): done"); 00540 } 00541 00542 00543 // assignment 00544 TEMP THIS& THIS::DoAssign(const THIS& rSourceVector) { 00545 FD_DC("TBaseVector(" << this << ")::DoAssign(rOtherVector " << &rSourceVector << ")"); 00546 // bail out on selfref 00547 if(this==&rSourceVector) return *this; 00548 // virtual clear 00549 Clear(); 00550 // allocate 00551 mVector.resize(rSourceVector.Size()); 00552 // copy entries 00553 for(Position pos=0; pos<mVector.size(); pos++) { 00554 mVector[pos].pElement = rSourceVector.mVector[pos].pElement->Copy(); 00555 mVector[pos].mMine=true; 00556 mVector[pos].mFileName=""; 00557 } 00558 // done 00559 FD_DC("TBaseVector(" << this << ")::DoAssign(rOtherVector " << &rSourceVector << "): done"); 00560 return *this; 00561 } 00562 00563 // clear 00564 TEMP void THIS::Clear(void) { 00565 // delete entries 00566 for(Position pos=0; pos<mVector.size(); pos++) { 00567 if(mVector[pos].mMine) delete mVector[pos].pElement; 00568 } 00569 // resize 00570 mVector.resize(0); 00571 } 00572 00573 00574 // Name 00575 TEMP const std::string& THIS::Name(void) const { 00576 return mMyName; 00577 } 00578 00579 // Name 00580 TEMP void THIS::Name(const std::string& rName) { 00581 mMyName = rName; 00582 } 00583 00584 // Size() 00585 TEMP Idx THIS::Size(void) const { 00586 return (Idx) mVector.size(); 00587 } 00588 00589 // Size(idx) 00590 TEMP void THIS::Size(Idx len) { 00591 FD_DC("TBaseVector(" << this << ")::Size(..): from " << mVector.size() << " to " << len); 00592 // record 00593 Position olen=mVector.size(); 00594 // delete 00595 for(Position pos=len; pos<olen; pos++) 00596 if(mVector[pos].mMine) delete mVector[pos].pElement; 00597 // allocate 00598 mVector.resize(len); 00599 // initialize 00600 for(Position pos=olen; pos < len; pos++) { 00601 mVector[pos].pElement = new T(); 00602 mVector[pos].mMine=true; 00603 mVector[pos].mFileName = ""; 00604 } 00605 // done 00606 FD_DC("TBaseVector(" << this << ")::Size(..): done"); 00607 } 00608 00609 00610 // Empty() 00611 TEMP bool THIS::Empty(void) const { 00612 return mVector.empty(); 00613 } 00614 00615 00616 // At() 00617 TEMP const T& THIS::At(const Position& pos) const { 00618 #ifdef FAUDES_CHECKED 00619 if(pos<0 || pos >= mVector.size()) { 00620 std::stringstream errstr; 00621 errstr << "index out of range" << std::endl; 00622 throw Exception("TBaseVector::At", errstr.str(), 62); 00623 } 00624 #endif 00625 return *mVector[pos].pElement; 00626 } 00627 00628 // At() 00629 TEMP T& THIS::At(const Position& pos) { 00630 #ifdef FAUDES_CHECKED 00631 if(pos<0 || pos >= mVector.size()) { 00632 std::stringstream errstr; 00633 errstr << "index out of range" << std::endl; 00634 throw Exception("TBaseVector::At", errstr.str(), 62); 00635 } 00636 #endif 00637 return *mVector[pos].pElement; 00638 } 00639 00640 00641 00642 // replace (copy) 00643 TEMP void THIS::Replace(const Position& pos, const T& rElem) { 00644 #ifdef FAUDES_CHECKED 00645 if(pos<0 || pos >= mVector.size()) { 00646 std::stringstream errstr; 00647 errstr << "index out of range" << std::endl; 00648 throw Exception("TBaseVector::At", errstr.str(), 62); 00649 } 00650 #endif 00651 iterator pit=mVector.begin()+pos; 00652 if(pit->mMine) delete pit->pElement; 00653 pit->pElement=rElem.Copy(); 00654 pit->mMine=true; 00655 pit->mFileName=""; 00656 } 00657 00658 // replace (take) 00659 TEMP void THIS::Replace(const Position& pos, T* pElem) { 00660 #ifdef FAUDES_CHECKED 00661 if(pos<0 || pos >= mVector.size()) { 00662 std::stringstream errstr; 00663 errstr << "index out of range" << std::endl; 00664 throw Exception("TBaseVector::At", errstr.str(), 62); 00665 } 00666 #endif 00667 iterator pit=mVector.begin()+pos; 00668 if(pit->mMine) delete pit->pElement; 00669 pit->pElement=pElem; 00670 pit->mMine=false; 00671 pit->mFileName=""; 00672 } 00673 00674 // replace (file) 00675 TEMP void THIS::Replace(const Position& pos, const std::string& rFileName) { 00676 #ifdef FAUDES_CHECKED 00677 if(pos<0 || pos >= mVector.size()) { 00678 std::stringstream errstr; 00679 errstr << "index out of range" << std::endl; 00680 throw Exception("TBaseVector::At", errstr.str(), 62); 00681 } 00682 #endif 00683 iterator pit=mVector.begin()+pos; 00684 if(pit->mMine) delete pit->pElement; 00685 pit->pElement = new T(); 00686 pit->mMine=true; 00687 pit->pElement->Read(rFileName); 00688 pit->mFileName=rFileName; 00689 } 00690 00691 // erase 00692 TEMP void THIS::Erase(const Position& pos) { 00693 #ifdef FAUDES_CHECKED 00694 if(pos<0 || pos >= mVector.size()) { 00695 std::stringstream errstr; 00696 errstr << "index out of range" << std::endl; 00697 throw Exception("TBaseVector::At", errstr.str(), 62); 00698 } 00699 #endif 00700 iterator pit=mVector.begin()+pos; 00701 if(pit->mMine) delete pit->pElement; 00702 mVector.erase(pit); 00703 } 00704 00705 00706 // insert (copy) 00707 TEMP void THIS::Insert(const Position& pos, const T& rElem) { 00708 #ifdef FAUDES_CHECKED 00709 if(pos<0 || pos > mVector.size()) { 00710 std::stringstream errstr; 00711 errstr << "index out of range" << std::endl; 00712 throw Exception("TBaseVector::At", errstr.str(), 62); 00713 } 00714 #endif 00715 Element elem; 00716 elem.pElement = rElem.Copy(); 00717 elem.mMine=true; 00718 elem.mFileName=""; 00719 iterator pit=mVector.begin()+pos; 00720 mVector.insert(pit,elem); 00721 } 00722 00723 // insert (take) 00724 TEMP void THIS::Insert(const Position& pos, T* pElem) { 00725 #ifdef FAUDES_CHECKED 00726 if(pos<0 || pos > mVector.size()) { 00727 std::stringstream errstr; 00728 errstr << "index out of range" << std::endl; 00729 throw Exception("TBaseVector::At", errstr.str(), 62); 00730 } 00731 #endif 00732 Element elem; 00733 elem.pElement = pElem; 00734 elem.mMine=false; 00735 elem.mFileName=""; 00736 iterator pit=mVector.begin()+pos; 00737 mVector.insert(pit,elem); 00738 } 00739 00740 // insert (file) 00741 TEMP void THIS::Insert(const Position& pos, const std::string& rFileName) { 00742 #ifdef FAUDES_CHECKED 00743 if(pos<0 || pos > mVector.size()) { 00744 std::stringstream errstr; 00745 errstr << "index out of range" << std::endl; 00746 throw Exception("TBaseVector::At", errstr.str(), 62); 00747 } 00748 #endif 00749 Element elem; 00750 elem.pElement = new T(); 00751 elem.mMine=true; 00752 elem.pElement->Read(rFileName); 00753 elem.mFileName=rFileName; 00754 iterator pit=mVector.begin()+pos; 00755 mVector.insert(pit,elem); 00756 } 00757 00758 00759 // append (copy) 00760 TEMP void THIS::PushBack(const T& rElem) { 00761 Element elem; 00762 elem.pElement = rElem.Copy(); 00763 elem.mMine=true; 00764 elem.mFileName=""; 00765 mVector.push_back(elem); 00766 } 00767 00768 // append (take) 00769 TEMP void THIS::PushBack(T* pElem) { 00770 Element elem; 00771 elem.pElement = pElem; 00772 elem.mMine=false; 00773 elem.mFileName=""; 00774 mVector.push_back(elem); 00775 } 00776 00777 // append (file) 00778 TEMP void THIS::PushBack(const std::string& rFileName) { 00779 Element elem; 00780 elem.pElement = new T(); 00781 elem.mMine=true; 00782 elem.pElement->Read(rFileName); 00783 elem.mFileName=rFileName; 00784 mVector.push_back(elem); 00785 } 00786 00787 00788 // append (copy) 00789 TEMP void THIS::Append(const T& rElem) { 00790 PushBack(rElem); 00791 } 00792 00793 // append (take) 00794 TEMP void THIS::Append(T* pElem) { 00795 PushBack(pElem); 00796 } 00797 00798 // append (file) 00799 TEMP void THIS::Append(const std::string& rFileName) { 00800 PushBack(rFileName); 00801 } 00802 00803 00804 // FilenameAt() 00805 TEMP const std::string& THIS::FilenameAt(const Position& pos) const { 00806 #ifdef FAUDES_CHECKED 00807 if(pos<0 || pos >= mVector.size()) { 00808 std::stringstream errstr; 00809 errstr << "index out of range" << std::endl; 00810 throw Exception("TBaseVector::FilenameAt", errstr.str(), 62); 00811 } 00812 #endif 00813 return mVector[pos].mFileName; 00814 } 00815 00816 // FilenameAt() 00817 TEMP void THIS::FilenameAt(const Position& pos, const std::string& rFileName) { 00818 #ifdef FAUDES_CHECKED 00819 if(pos<0 || pos >= mVector.size()) { 00820 std::stringstream errstr; 00821 errstr << "index out of range" << std::endl; 00822 throw Exception("TBaseVector::FilenameAt", errstr.str(), 62); 00823 } 00824 #endif 00825 mVector[pos].mFileName = rFileName; 00826 } 00827 00828 // take ownership 00829 TEMP void THIS::TakeCopies(void) { 00830 iterator pit=mVector.begin(); 00831 for(;pit!=mVector.end();++pit) { 00832 if(pit->mMine) continue; 00833 pit->pElement=pit->pElement->Copy(); 00834 pit->mMine=true; 00835 } 00836 } 00837 00838 // take ownership 00839 TEMP void THIS::TakeOwnership(void) { 00840 iterator pit=mVector.begin(); 00841 for(;pit!=mVector.end();++pit) 00842 pit->mMine=true; 00843 } 00844 00845 // DoWrite(tw, label, context) 00846 TEMP void THIS::DoWrite(TokenWriter& rTw, const std::string& rLabel, const Type* pContext) const { 00847 // figure whether we write individual files 00848 bool ifiles=true; 00849 if(rTw.DestMode()!=TokenWriter::File) ifiles=false; 00850 for(Position pos=0; pos<mVector.size() && ifiles; pos++) 00851 if(mVector[pos].mFileName=="") ifiles=false; 00852 // extract base directory 00853 std::string dirname=""; 00854 if(rTw.DestMode()==TokenWriter::File) 00855 dirname = ExtractDirectory(rTw.FileName()); 00856 // have a section 00857 std::string label=rLabel; 00858 if(label=="") label="Vector"; 00859 FD_DC("TBaseVector(" << this << ")::DoWrite(..): section " << label << " #" << Size()); 00860 rTw.WriteBegin(label); 00861 for(Position pos=0; pos<mVector.size(); pos++) { 00862 // just stream tokens 00863 if(!ifiles) { 00864 mVector[pos].pElement->Write(rTw,"",pContext); 00865 continue; 00866 } 00867 // write individual files 00868 std::string filename= ExtractFilename(mVector[pos].mFileName); 00869 rTw.WriteString(filename); 00870 mVector[pos].pElement->Write(PrependDirectory(dirname,filename),"",pContext); 00871 } 00872 rTw.WriteEnd(label); 00873 } 00874 00875 00876 // DoDWrite(tw,rLabel,context) 00877 TEMP void THIS::DoDWrite(TokenWriter& rTw,const std::string& rLabel, const Type* pContext) const { 00878 std::string label=rLabel; 00879 if(label=="") label="Vector"; 00880 FD_DC("TBaseVector(" << this << ")::DoDWrite(..): section " << label << " #" << Size()); 00881 rTw.WriteBegin(label); 00882 for(Position pos=0; pos<mVector.size(); pos++) { 00883 mVector[pos].pElement->DWrite(rTw,"",pContext); 00884 } 00885 rTw.WriteEnd(label); 00886 } 00887 00888 00889 // DoSWrite(tw) 00890 TEMP void THIS::DoSWrite(TokenWriter& rTw) const { 00891 FD_DC("TBaseVector(" << this << ")::DoSWrite(..)"); 00892 rTw.WriteComment(" Vector Size: "+ ToStringInteger(Size())); 00893 for(Position pos=0; pos<mVector.size(); pos++) { 00894 rTw.WriteComment(" Vector Entry " + ToStringInteger(pos)); 00895 mVector[pos].pElement->SWrite(rTw); 00896 } 00897 rTw.WriteComment(""); 00898 } 00899 00900 00901 // DoRead(rTr, rLabel, pContext) 00902 TEMP void THIS::DoRead(TokenReader& rTr, const std::string& rLabel, const Type* pContext) { 00903 //prepare token 00904 Token token; 00905 //prepare string 00906 std::string filename = ""; 00907 std::string dirname = ""; 00908 std::string path; 00909 // have default section 00910 std::string label=rLabel; 00911 if(label=="") label="Vector"; 00912 Name(label); 00913 rTr.SeekBegin(label); 00914 // fill my entries from token stream 00915 while(!rTr.Eos(label)){ 00916 //peek token 00917 rTr.Peek(token); 00918 // if Token is a String we assume its the name of a file containing a device 00919 if(token.Type()==Token::String) { 00920 //read Token 00921 rTr.Get(token); 00922 // read relative filename 00923 filename = token.StringValue(); 00924 // build up path to base-file 00925 if(rTr.SourceMode()==TokenReader::File) dirname = ExtractDirectory(rTr.FileName()); 00926 //build up path to specified file 00927 path = dirname.append(filename); 00928 //insert device 00929 Insert(mVector.size(),path); 00930 continue; 00931 } 00932 // if its not a file it has to be an entry 00933 else if(token.Type()==Token::Begin) { 00934 // prepare 00935 T* elemp = new T(); 00936 // read entry 00937 elemp->Read(rTr); 00938 // insert device mDevices 00939 Insert(mVector.size(),elemp); 00940 // fix ownership 00941 (--mVector.end())->mMine=true; 00942 continue; 00943 } 00944 // token mismatch 00945 std::stringstream errstr; 00946 errstr << "token mismatch" << std::endl; 00947 throw Exception("TBaseVector::At", errstr.str(), 50); 00948 } 00949 // done 00950 rTr.SeekEnd(label); 00951 } 00952 00953 00954 00955 00956 00957 /* undefine local shortcuts */ 00958 #undef THIS 00959 #undef TEMP 00960 #undef BASE 00961 00962 /** @} doxygen group */ 00963 00964 } // namespace faudes 00965 00966 #endif |
libFAUDES 2.14g --- 2009-12-3 --- c++ source docu by doxygen 1.5.6