cfl_basevector.hGo to the documentation of this file.00001 /** @file cfl_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 "cfl_definitions.h" 00029 #include "cfl_tokenwriter.h" 00030 #include "cfl_tokenreader.h" 00031 #include "cfl_types.h" 00032 #include "cfl_attributes.h" 00033 #include <vector> 00034 #include <algorithm> 00035 00036 namespace faudes { 00037 00038 /** @addtogroup ContainerClasses */ 00039 /** @{*/ 00040 00041 /** 00042 * Vector bass class. 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. As with other faudes containers, 00048 * vBaseVector is the universal base class for all faudes vector data types. The latter 00049 * explicitely refer to the element data type and are implemented as templates. 00050 * 00051 * Internally, the vBaseVector template uses STL vector of pointers to the individual entries. 00052 * When adding an entry, you may either do so be refernce or by pointer. When setting 00053 * by reference, the vector takes a copy and owns the new entry. When setting by a pointer, 00054 * the vector only records the reference. The vector tracks ownership of each entry in order 00055 * to properly destruct entries. 00056 * 00057 * vBaseVector serves as a base class for all libFaudes vectors: 00058 * - GeneratorVector (vector or generators) 00059 * - EventSetVector (vector of event sets) 00060 * 00061 * Token io assumes that the type parameter is a faudes type, ie. entries of the vector 00062 * provide token io themselfs. To derive a vector class with non-faudes-type entries, 00063 * you will need to reimplement token io. As a convenience feature, the vector keeps a record 00064 * of filenames associated with individual entries. You can inspect and edit this record via 00065 * the FilenameAt members. When writing the vector to a file and all entries have an associated filename, 00066 * output will be to the individual files. 00067 * 00068 */ 00069 00070 class vBaseVector : public Type { 00071 00072 FAUDES_TYPE_DECLARATION(Void,vBaseVector,Type) 00073 00074 public: 00075 00076 /** 00077 * Constructor. 00078 */ 00079 vBaseVector(void); 00080 00081 /** 00082 * Copy-constructor. 00083 * 00084 * @param rOtherVector 00085 * Source to copy from 00086 */ 00087 vBaseVector(const vBaseVector& rOtherVector); 00088 00089 /** 00090 * Constructor from file. 00091 * 00092 * @param rFilename 00093 * Name of File 00094 * @param rLabel 00095 * Section for the set in the file; 00096 */ 00097 vBaseVector(const std::string& rFilename, const std::string& rLabel = "BaseVector"); 00098 00099 /** 00100 * Virtual destructor 00101 */ 00102 virtual ~vBaseVector(void); 00103 00104 /** 00105 * Prototype for vector entries. 00106 * The virtual base vBaseVector will provide a plain Type object. 00107 * Derived vector classes are meant to reimplement this function. 00108 * 00109 * @return 00110 * Element protoype 00111 */ 00112 virtual const Type* Elementp(void) const; 00113 00114 00115 /** 00116 * Prototype for vector entries. 00117 * This is a convenience wrapper for Elementp. 00118 * 00119 * @return 00120 * Element protoype 00121 */ 00122 virtual const Type& Element(void) const; 00123 00124 00125 /** 00126 * Factory method for vector entries. 00127 * This is a convenience wrapper using Elementp. 00128 * 00129 * @return 00130 * New element allocated on heap 00131 */ 00132 virtual Type* NewElement(void); 00133 00134 /** 00135 * Test whether the specified element is acceptebla for this vector. 00136 * This is a convenience wrapper for Elementp. 00137 * 00138 * @param rElement 00139 * Element to type check. 00140 * @return 00141 * True, if provided element is accepted by this vector. 00142 */ 00143 virtual bool ElementTry(const Type& rElement) const; 00144 00145 00146 /** 00147 * Return name of vBaseVector 00148 * 00149 * @return 00150 * Name of vBaseVector 00151 */ 00152 const std::string& Name(void) const; 00153 00154 /** 00155 * Set name of vBaseVector 00156 * 00157 * @param rName 00158 * Name to set 00159 */ 00160 void Name(const std::string& rName); 00161 00162 /** 00163 * Clear all vector 00164 */ 00165 virtual void Clear(void); 00166 00167 /** 00168 * Get size of vector. 00169 * 00170 * @return 00171 * Number of entries. 00172 */ 00173 Idx Size(void) const; 00174 00175 /** 00176 * Set size of vector. 00177 * If the new size is smaller than the current size, 00178 * the vector is truncated. If it is larger, default members 00179 * are inserted at the end. 00180 * 00181 * @param len 00182 * Number of entries in vector 00183 */ 00184 void Size(Idx len); 00185 00186 /** 00187 * Check if the vBaseVector ist Empty 00188 * 00189 * @return 00190 * True if empty 00191 */ 00192 bool Empty(void) const; 00193 00194 /** 00195 * convenience typedef for positions 00196 */ 00197 typedef std::vector<int>::size_type Position; 00198 00199 /** 00200 * Access element. 00201 * 00202 * @param pos 00203 * Specify entry to access 00204 * @exception Exception 00205 * - Position out of range (id 69) 00206 */ 00207 virtual const Type& At(const Position& pos) const; 00208 00209 /** 00210 * Access element. 00211 * 00212 * @param pos 00213 * Specify entry to access 00214 * @exception Exception 00215 * - Position out of range (id 69) 00216 */ 00217 virtual Type& At(const Position& pos); 00218 00219 00220 /** 00221 * Replace specified entry. 00222 * This method takes a copy of the entry to replace and the 00223 * vector becomes the owner of the copy. 00224 * 00225 * @param pos 00226 * Position to replace 00227 * @param rElem 00228 * New entry 00229 * @exception Exception 00230 * - Position out of range (id 69) 00231 * - Cannot cast element type (63) 00232 */ 00233 virtual void Replace(const Position& pos, const Type& rElem); 00234 00235 /** 00236 * Replace specified entry. 00237 * This method avoids to copy the entry to replace and only records the reference. 00238 * The vector does not take ownership of the new entry. I.e., when the vector is destroyed, or the 00239 * entry is deleted from the vector, the entry itself remains allocated. 00240 * 00241 * @param pos 00242 * Position to replace 00243 * @param pElem 00244 * New entry 00245 * @exception Exception 00246 * - Position out of range (id 69) 00247 * - Cannot cast element type (63) 00248 */ 00249 virtual void Replace(const Position& pos, Type* pElem); 00250 00251 /** 00252 * Replace specified entry. 00253 * This method reads the sepcified entry from file and the 00254 * vector becomes the owner of the new entry. 00255 * 00256 * @param pos 00257 * Position to replace 00258 * @param rFileName 00259 * New entry to be read from file 00260 * @exception Exception 00261 * - Position out of range (id 69) 00262 */ 00263 virtual void Replace(const Position& pos, const std::string& rFileName); 00264 00265 00266 /** 00267 * Erase entry by position. 00268 * If the vector owns the entry, it will be destructed. 00269 * 00270 * @param pos 00271 * Specify entry to erase 00272 * @exception Exception 00273 * - Position out of range (id 69) 00274 */ 00275 virtual void Erase(const Position& pos); 00276 00277 /** 00278 * Insert specified entry. 00279 * This method takes a copy of the entry to replace and the 00280 * vector becomes the owner of the copy. 00281 * 00282 * @param pos 00283 * Position at which to insert 00284 * @param rElem 00285 * Element to insert 00286 * @exception Exception 00287 * - Position out of range (id 69) 00288 * - Cannot cast element type (63) 00289 */ 00290 virtual void Insert(const Position& pos, const Type& rElem); 00291 00292 /** 00293 * Insert specified entry. 00294 * This method avoids to copy the entry to replace and only records the reference. 00295 * The vector does not take ownership of the new entry. I.e., when the vector is destroyed, or the 00296 * entry is deleted from the vector, the entry itself remains allocated. 00297 * 00298 * 00299 * @param pos 00300 * Position at which to insert 00301 * @param rElem 00302 * Element to insert 00303 * @exception Exception 00304 * - Position out of range (id 69) 00305 * - Cannot cast element type (63) 00306 */ 00307 virtual void Insert(const Position& pos, Type* rElem); 00308 00309 /** 00310 * Insert specified entry. 00311 * This method reads the sepcified entry from file and the 00312 * vector becomes the owner of the new entry. 00313 * 00314 * @param pos 00315 * Position at which to insert 00316 * @param rFileName 00317 * Element to insert 00318 * @exception Exception 00319 * - Position out of range (id 69) 00320 */ 00321 virtual void Insert(const Position& pos, const std::string& rFileName); 00322 00323 /** 00324 * Append specified entry. 00325 * This method takes a copy of the entry to replace and the 00326 * vector becomes the owner of the copy. 00327 * 00328 * @param rElem 00329 * Element to append 00330 * @exception Exception 00331 * - Cannot cast element type (63) 00332 */ 00333 virtual void PushBack(const Type& rElem); 00334 00335 /** 00336 * Append specified entry. 00337 * This method avoids to copy the entry to replace and only records the reference. 00338 * The vector does not take ownership of the new entry. I.e., when the vector is destroyed, or the 00339 * entry is deleted from the vector, the entry itself remains allocated. 00340 * 00341 * 00342 * @param rElem 00343 * Element to insert 00344 * @exception Exception 00345 * - Cannot cast element type (63) 00346 */ 00347 virtual void PushBack(Type* rElem); 00348 00349 /** 00350 * Append specified entry. 00351 * This method reads the sepcified entry from file and the 00352 * vector becomes the owner of the new entry. 00353 * 00354 * @param rFileName 00355 * Element to insert 00356 */ 00357 virtual void PushBack(const std::string& rFileName); 00358 00359 /** 00360 * Append specified entry. 00361 * Synonymous for PushBack. 00362 * This method takes a copy of the entry to replace and the 00363 * vector becomes the owner of the copy. 00364 * 00365 * @param rElem 00366 * Element to append 00367 * @exception Exception 00368 * - Cannot cast element type (63) 00369 */ 00370 virtual void Append(const Type& rElem); 00371 00372 /** 00373 * Append specified entry. 00374 * Synonymous for PushBack. 00375 * This method avoids to copy the entry to replace and only records the reference. 00376 * The vector does not take ownership of the new entry. I.e., when the vector is destroyed, or the 00377 * entry is deleted from the vector, the entry itself remains allocated. 00378 * 00379 * 00380 * @param rElem 00381 * Element to insert 00382 * @exception Exception 00383 * - Cannot cast element type (63) 00384 */ 00385 virtual void Append(Type* rElem); 00386 00387 /** 00388 * Append specified entry. 00389 * Synonymous for PushBack. 00390 * This method reads the sepcified entry from file and the 00391 * vector becomes the owner of the new entry. 00392 * 00393 * @param rFileName 00394 * Element to insert 00395 */ 00396 virtual void Append(const std::string& rFileName); 00397 00398 /** 00399 * Specify a filename. 00400 * When each entry has a filenam specified, 00401 * file io of the vector will be to indivudual files. 00402 * 00403 * @param pos 00404 * Position of entry 00405 * @param rFileName 00406 * Filename relative to vector file 00407 * @exception Exception 00408 * - Position out of range (id 69) 00409 */ 00410 void FilenameAt(const Position& pos, const std::string& rFileName); 00411 00412 /** 00413 * Get filename of entry. 00414 * 00415 * @param pos 00416 * Position of entry 00417 * @return 00418 * Filename assoiated with entry 00419 * @exception Exception 00420 * - Position out of range (id 69) 00421 */ 00422 const std::string& FilenameAt(const Position& pos) const; 00423 00424 /** 00425 * Take ownership of all entries. 00426 * Thsi method will take ownership of all entries, including those, that 00427 * have been set by pointer reference. When the vector is destructed, all 00428 * entries will be destructed, too. However, write access may invalidate 00429 * element pointers. 00430 * 00431 */ 00432 void TakeOwnership(void); 00433 00434 /** 00435 * Take local copies of all entries. 00436 * This method will construct local copies of all entries not previously 00437 * owned. 00438 */ 00439 void TakeCopies(void); 00440 00441 protected: 00442 00443 00444 /** 00445 * Token output, debugging see Type::DWrite for public wrappers. 00446 * The method assumes that the type parameter is a faudes type and uses 00447 * the provide write method per entry. Reimplement this function in derived 00448 * classes for non-faudes type vectors. 00449 * @param rTw 00450 * Reference to TokenWriter 00451 * @param rLabel 00452 * Label of section to write, defaults to name of set 00453 * @param pContext 00454 * Write context to provide contextual information 00455 */ 00456 virtual void DoDWrite(TokenWriter& rTw,const std::string& rLabel="", const Type* pContext=0) const; 00457 00458 /** 00459 * Token output, see Type::SWrite for public wrappers. 00460 * The method assumes that the type parameter is a faudes type and uses 00461 * the provide write method per entry. Reimplement this function in derived 00462 * classes for non-faudes type vectors. 00463 * 00464 * @param rTw 00465 * Reference to TokenWriter 00466 * 00467 * @exception Exception 00468 * - IO errors (id 2) 00469 */ 00470 virtual void DoSWrite(TokenWriter& rTw) const; 00471 00472 /** 00473 * Token output, see Type::Write for public wrappers. 00474 * The method assumes that the type parameter is a faudes type and uses 00475 * the provide write method per entry. Reimplement this function in derived 00476 * classes for non-faudes type vectors. 00477 * 00478 * @param rTw 00479 * Reference to TokenWriter 00480 * @param rLabel 00481 * Label of section to write, defaults to name of set 00482 * @param pContext 00483 * Write context to provide contextual information 00484 */ 00485 virtual void DoWrite(TokenWriter& rTw, const std::string& rLabel="", const Type* pContext=0) const; 00486 00487 /** 00488 * Token input, see Type::Read for public wrappers. 00489 * The method assumes that the type parameter is a faudes type and uses 00490 * the provide read method per entry. Reimplement this function in derived 00491 * classes for non-faudes type vectors. 00492 * By convention, the default label "" should be translated to some meaningful default, 00493 * eg "GeneratorVector" for a vector of generators. The pContext pointer can be type-checked 00494 * and interpreted, ie as a symboltable to provide symbolic names. It is also 00495 * passed on to vector entries. 00496 * 00497 * @param rTr 00498 * Reference to TokenReader 00499 * @param rLabel 00500 * Label of section to read, defaults to name of set 00501 * @param pContext 00502 * Read context to provide contextual information 00503 */ 00504 virtual void DoRead(TokenReader& rTr, const std::string& rLabel = "", const Type* pContext=0); 00505 00506 /** Assignment method */ 00507 virtual void DoAssign(const vBaseVector& rSourceVector); 00508 00509 /** Internal entry data type */ 00510 class ElementRecord { 00511 public: 00512 Type* pElement; 00513 std::string mFileName; 00514 bool mMine; 00515 }; 00516 00517 /** STL vector of element */ 00518 std::vector<ElementRecord> mVector; 00519 00520 /** convenience typedef */ 00521 typedef std::vector<ElementRecord>::iterator iterator; 00522 00523 private: 00524 00525 /** Name of TBaseVector */ 00526 std::string mMyName; 00527 00528 }; 00529 00530 00531 /** @} doxygen group */ 00532 00533 00534 /** @addtogroup ContainerClasses */ 00535 /** @{*/ 00536 00537 /** 00538 * Vector template. 00539 * 00540 * The vector templates specializes the bass vBaseVector in that it uses the template 00541 * paremeter to specify the type of its entries. See vBaseVector for element access 00542 * methods. 00543 * 00544 * TVectorSet serves is used to implement the libFaudes vectors 00545 * - GeneratorVector (vector or generators) 00546 * - SytemVector (vector or generators) 00547 * - EventSetVector (vector of event sets) 00548 * - AlphabetVector (vector of event sets) 00549 * 00550 * 00551 */ 00552 00553 template<class T> 00554 class TBaseVector : public vBaseVector { 00555 00556 FAUDES_TYPE_TDECLARATION(Void,TBaseVector,vBaseVector) 00557 00558 public: 00559 00560 /** 00561 * Constructor. 00562 */ 00563 TBaseVector(void); 00564 00565 /** 00566 * Copy-constructor. 00567 * 00568 * @param rOtherSet 00569 * Source to copy from 00570 */ 00571 TBaseVector(const TBaseVector& rOtherSet); 00572 00573 /** 00574 * Copy-constructor. This version takes any vector as source, 00575 * but throughs an exception, if element types dont match. 00576 * 00577 * @param rOtherSet 00578 * Source to copy from 00579 * @exception Exception 00580 * - Cannot cast elements (63) 00581 */ 00582 TBaseVector(const vBaseVector& rOtherSet); 00583 00584 /** 00585 * Constructor from file. 00586 * 00587 * @param rFilename 00588 * Name of File 00589 * @param rLabel 00590 * Section for the set in the file; 00591 */ 00592 TBaseVector(const std::string& rFilename, const std::string& rLabel = "BaseVector"); 00593 00594 /** 00595 * Virtual destructor 00596 */ 00597 virtual ~TBaseVector(void); 00598 00599 00600 /** 00601 * Prototype for vector entries. 00602 * This template class uses the virtual function to know its element type. 00603 * 00604 * @return 00605 * Element protoype 00606 */ 00607 virtual const T* Elementp(void) const; 00608 00609 /** 00610 * Test whether the specified element is acceptebla for this vector. 00611 * This is a convenience wrapper for Elementp. 00612 * 00613 * @param rElement 00614 * Element to type check. 00615 * @return 00616 * True, if provided element is accepted by this vector. 00617 */ 00618 virtual bool ElementTry(const Type& rElement) const; 00619 00620 /** 00621 * convenience typedef for positions 00622 */ 00623 typedef std::vector<int>::size_type Position; 00624 00625 /** 00626 * Access element. 00627 * 00628 * @param pos 00629 * Specify entry to access 00630 * @exception Exception 00631 * - Position out of range (id 69) 00632 */ 00633 virtual const T& At(const Position& pos) const; 00634 00635 /** 00636 * Access element. 00637 * 00638 * @param pos 00639 * Specify entry to access 00640 * @exception Exception 00641 * - Position out of range (id 69) 00642 */ 00643 virtual T& At(const Position& pos); 00644 00645 protected: 00646 00647 /** Assignment method */ 00648 virtual void DoAssign(const TBaseVector<T>& rSourceVector); 00649 00650 00651 }; 00652 00653 00654 /** @} doxygen group */ 00655 00656 00657 00658 /* 00659 ****************************************************************************************** 00660 ****************************************************************************************** 00661 ****************************************************************************************** 00662 00663 Implementation of TBaseVector 00664 00665 ****************************************************************************************** 00666 ****************************************************************************************** 00667 ****************************************************************************************** 00668 */ 00669 00670 /* convenience access to relevant scopes */ 00671 #define THIS TBaseVector<T> 00672 #define TEMP template<class T> 00673 #define BASE vBaseVector 00674 00675 00676 // faudes type std 00677 FAUDES_TYPE_TIMPLEMENTATION(Void,THIS,vBaseVector,TEMP) 00678 00679 // TBaseVector() 00680 TEMP THIS::TBaseVector(void) : 00681 vBaseVector() 00682 { 00683 FD_DC("TBaseVector(" << this << ")::TBaseVector()"); 00684 } 00685 00686 00687 // TBaseVector(filename) 00688 TEMP THIS::TBaseVector(const std::string& rFileName, const std::string& rLabel) : 00689 vBaseVector() 00690 { 00691 FD_DC("TBaseVector(" << this << ")::TBaseVector()"); 00692 // do read; 00693 Read(rFileName,rLabel); 00694 } 00695 00696 00697 // TBaseVector(rOtherSet) 00698 TEMP THIS::TBaseVector(const TBaseVector& rOtherVector) : 00699 vBaseVector() 00700 { 00701 FD_DC("TBaseVector(" << this << ")::TBaseVector(rOtherVector " << &rOtherVector << "): copy construct"); 00702 DoAssign(rOtherVector); 00703 } 00704 00705 // TBaseVector(rOtherSet) 00706 TEMP THIS::TBaseVector(const vBaseVector& rOtherVector) : 00707 vBaseVector() 00708 { 00709 FD_DC("TBaseVector(" << this << ")::TBaseVector([v] rOtherVector " << &rOtherVector << "): copy construct"); 00710 Assign(rOtherVector); 00711 } 00712 00713 // destructor 00714 TEMP THIS::~TBaseVector(void) { 00715 FD_DC("TBaseVector(" << this << ")::~TBaseVector()"); 00716 } 00717 00718 00719 // element prototype 00720 TEMP const T* THIS::Elementp(void) const { 00721 static T tproto; 00722 return &tproto; 00723 } 00724 00725 // test element type 00726 TEMP bool THIS::ElementTry(const Type& rElement) const { 00727 FD_DC("TBaseVector::ElementTry(): casting from " << typeid(rElement).name() << " to " << typeid(*Elementp()).name()); 00728 return Elementp()->Cast(&rElement)!=NULL; 00729 } 00730 00731 00732 // assignment 00733 TEMP void THIS::DoAssign(const THIS& rSourceVector) { 00734 FD_DC("TBaseVector(" << this << ")::DoAssign(rOtherVector " << &rSourceVector << ")"); 00735 // base can do it 00736 BASE::DoAssign(rSourceVector); 00737 // done 00738 FD_DC("TBaseVector(" << this << ")::DoAssign(rOtherVector " << &rSourceVector << "): done"); 00739 } 00740 00741 // At() 00742 TEMP const T& THIS::At(const Position& pos) const { 00743 #ifdef FAUDES_CHECKED 00744 if(pos<0 || pos >= mVector.size()) { 00745 std::stringstream errstr; 00746 errstr << "index out of range" << std::endl; 00747 throw Exception("TBaseVector::At", errstr.str(), 62); 00748 } 00749 #endif 00750 #ifdef FAUDES_DEBUG_CODE 00751 if(!dynamic_cast<T*>(mVector[pos].pElement)){ 00752 std::stringstream errstr; 00753 errstr << "internal type error" << std::endl; 00754 throw Exception("TBaseVector::At", errstr.str(), 63); 00755 } 00756 #endif 00757 return *dynamic_cast<T*>(mVector[pos].pElement); 00758 } 00759 00760 // At() 00761 TEMP T& THIS::At(const Position& pos) { 00762 #ifdef FAUDES_CHECKED 00763 if(pos<0 || pos >= mVector.size()) { 00764 std::stringstream errstr; 00765 errstr << "index out of range" << std::endl; 00766 throw Exception("TBaseVector::At", errstr.str(), 62); 00767 } 00768 #endif 00769 #ifdef FAUDES_DEBUG_CODE 00770 if(!dynamic_cast<T*>(mVector[pos].pElement)){ 00771 std::stringstream errstr; 00772 errstr << "internal type error" << std::endl; 00773 throw Exception("TBaseVector::At", errstr.str(), 63); 00774 } 00775 #endif 00776 return *dynamic_cast<T*>(mVector[pos].pElement); 00777 } 00778 00779 00780 00781 00782 00783 /* undefine local shortcuts */ 00784 #undef THIS 00785 #undef TEMP 00786 #undef BASE 00787 00788 /** @} doxygen group */ 00789 00790 } // namespace faudes 00791 00792 #endif libFAUDES 2.23h --- 2014.04.03 --- c++ api documentaion by doxygen |