cfl_basevector.h
Go to the documentation of this file.
1 /** @file cfl_basevector.h @brief Class TBaseVector */
2 
3 
4 /* FAU Discrete Event Systems Library (libfaudes)
5 
6  Copyright (C) 2009 Thomas Moor
7 
8  This library is free software; you can redistribute it and/or
9  modify it under the terms of the GNU Lesser General Public
10  License as published by the Free Software Foundation; either
11  version 2.1 of the License, or (at your option) any later version.
12 
13  This library is distributed in the hope that it will be useful,
14  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  Lesser General Public License for more details.
17 
18  You should have received a copy of the GNU Lesser General Public
19  License along with this library; if not, write to the Free Software
20  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
21 */
22 
23 
24 
25 #ifndef FAUDES_BASEVECTOR_H
26 #define FAUDES_BASEVECTOR_H
27 
28 #include "cfl_definitions.h"
29 #include "cfl_tokenwriter.h"
30 #include "cfl_tokenreader.h"
31 #include "cfl_types.h"
32 #include "cfl_attributes.h"
33 #include <vector>
34 #include <algorithm>
35 
36 namespace faudes {
37 
38 /** @addtogroup ContainerClasses */
39 /** @{*/
40 
41 /**
42  * Vector bass class.
43  *
44  * This class is designed as a random access container for a small number of
45  * comperatively large objects, eg a vector of generators to model a decentralized DES.
46  * The API is restricted to simple positional access and there are no explicit
47  * iterators nor is there a deferred copy mechanism. As with other faudes containers,
48  * vBaseVector is the universal base class for all faudes vector data types. The latter
49  * explicitely refer to the element data type and are implemented as templates.
50  *
51  * Internally, the vBaseVector template uses STL vector of pointers to the individual entries.
52  * When adding an entry, you may either do so be refernce or by pointer. When setting
53  * by reference, the vector takes a copy and owns the new entry. When setting by a pointer,
54  * the vector only records the reference. The vector tracks ownership of each entry in order
55  * to properly destruct entries.
56  *
57  * vBaseVector serves as a base class for all libFaudes vectors:
58  * - GeneratorVector (vector or generators)
59  * - EventSetVector (vector of event sets)
60  *
61  * Token io assumes that the type parameter is a faudes type, ie. entries of the vector
62  * provide token io themselfs. To derive a vector class with non-faudes-type entries,
63  * you will need to reimplement token io. As a convenience feature, the vector keeps a record
64  * of filenames associated with individual entries. You can inspect and edit this record via
65  * the FilenameAt members. When writing the vector to a file and all entries have an associated filename,
66  * output will be to the individual files.
67  *
68  */
69 
70 class FAUDES_API vBaseVector : public Type {
71 
73 
74 public:
75 
76  /**
77  * Constructor.
78  */
79  vBaseVector(void);
80 
81  /**
82  * Copy-constructor.
83  *
84  * @param rOtherVector
85  * Source to copy from
86  */
87  vBaseVector(const vBaseVector& rOtherVector);
88 
89  /**
90  * Constructor from file.
91  *
92  * @param rFilename
93  * Name of File
94  * @param rLabel
95  * Section for the set in the file;
96  */
97  vBaseVector(const std::string& rFilename, const std::string& rLabel = "BaseVector");
98 
99  /**
100  * Virtual destructor
101  */
102  virtual ~vBaseVector(void);
103 
104  /**
105  * Prototype for vector entries.
106  * The virtual base vBaseVector will provide a plain Type object.
107  * Derived vector classes are meant to reimplement this function.
108  *
109  * @return
110  * Element protoype
111  */
112  virtual const Type* Elementp(void) const;
113 
114 
115  /**
116  * Prototype for vector entries.
117  * This is a convenience wrapper for Elementp.
118  *
119  * @return
120  * Element protoype
121  */
122  virtual const Type& Element(void) const;
123 
124 
125  /**
126  * Factory method for vector entries.
127  * This is a convenience wrapper using Elementp.
128  *
129  * @return
130  * New element allocated on heap
131  */
132  virtual Type* NewElement(void);
133 
134  /**
135  * Test whether the specified element is acceptebla for this vector.
136  * This is a convenience wrapper for Elementp.
137  *
138  * @param rElement
139  * Element to type check.
140  * @return
141  * True, if provided element is accepted by this vector.
142  */
143  virtual bool ElementTry(const Type& rElement) const;
144 
145 
146  /**
147  * Return name of vBaseVector
148  *
149  * @return
150  * Name of vBaseVector
151  */
152  const std::string& Name(void) const;
153 
154  /**
155  * Set name of vBaseVector
156  *
157  * @param rName
158  * Name to set
159  */
160  void Name(const std::string& rName);
161 
162  /**
163  * Clear all vector
164  */
165  virtual void Clear(void);
166 
167  /**
168  * Get size of vector.
169  *
170  * @return
171  * Number of entries.
172  */
173  Idx Size(void) const;
174 
175  /**
176  * Set size of vector.
177  * If the new size is smaller than the current size,
178  * the vector is truncated. If it is larger, default members
179  * are inserted at the end.
180  *
181  * @param len
182  * Number of entries in vector
183  */
184  void Size(Idx len);
185 
186  /**
187  * Check if the vBaseVector ist Empty
188  *
189  * @return
190  * True if empty
191  */
192  bool Empty(void) const;
193 
194  /**
195  * convenience typedef for positions (must be unsigned)
196  */
197  typedef size_t Position;
198 
199  /**
200  * Access element.
201  *
202  * @param pos
203  * Specify entry to access
204  * @exception Exception
205  * - Position out of range (id 69)
206  */
207  virtual const Type& At(const Position& pos) const;
208 
209  /**
210  * Access element.
211  *
212  * @param pos
213  * Specify entry to access
214  * @exception Exception
215  * - Position out of range (id 69)
216  */
217  virtual Type& At(const Position& pos);
218 
219 
220  /**
221  * Replace specified entry.
222  * This method takes a copy of the entry to replace and the
223  * vector becomes the owner of the copy.
224  *
225  * @param pos
226  * Position to replace
227  * @param rElem
228  * New entry
229  * @exception Exception
230  * - Position out of range (id 69)
231  * - Cannot cast element type (63)
232  */
233  virtual void Replace(const Position& pos, const Type& rElem);
234 
235  /**
236  * Replace specified entry.
237  * This method avoids to copy the entry to replace and only records the reference.
238  * The vector does not take ownership of the new entry. I.e., when the vector is destroyed, or the
239  * entry is deleted from the vector, the entry itself remains allocated.
240  *
241  * @param pos
242  * Position to replace
243  * @param pElem
244  * New entry
245  * @exception Exception
246  * - Position out of range (id 69)
247  * - Cannot cast element type (63)
248  */
249  virtual void Replace(const Position& pos, Type* pElem);
250 
251  /**
252  * Replace specified entry.
253  * This method reads the sepcified entry from file and the
254  * vector becomes the owner of the new entry.
255  *
256  * @param pos
257  * Position to replace
258  * @param rFileName
259  * New entry to be read from file
260  * @exception Exception
261  * - Position out of range (id 69)
262  */
263  virtual void Replace(const Position& pos, const std::string& rFileName);
264 
265 
266  /**
267  * Erase entry by position.
268  * If the vector owns the entry, it will be destructed.
269  *
270  * @param pos
271  * Specify entry to erase
272  * @exception Exception
273  * - Position out of range (id 69)
274  */
275  virtual void Erase(const Position& pos);
276 
277  /**
278  * Insert specified entry.
279  * This method takes a copy of the entry to replace and the
280  * vector becomes the owner of the copy.
281  *
282  * @param pos
283  * Position at which to insert
284  * @param rElem
285  * Element to insert
286  * @exception Exception
287  * - Position out of range (id 69)
288  * - Cannot cast element type (63)
289  */
290  virtual void Insert(const Position& pos, const Type& rElem);
291 
292  /**
293  * Insert specified entry.
294  * This method avoids to copy the entry to replace and only records the reference.
295  * The vector does not take ownership of the new entry. I.e., when the vector is destroyed, or the
296  * entry is deleted from the vector, the entry itself remains allocated.
297  *
298  *
299  * @param pos
300  * Position at which to insert
301  * @param rElem
302  * Element to insert
303  * @exception Exception
304  * - Position out of range (id 69)
305  * - Cannot cast element type (63)
306  */
307  virtual void Insert(const Position& pos, Type* rElem);
308 
309  /**
310  * Insert specified entry.
311  * This method reads the sepcified entry from file and the
312  * vector becomes the owner of the new entry.
313  *
314  * @param pos
315  * Position at which to insert
316  * @param rFileName
317  * Element to insert
318  * @exception Exception
319  * - Position out of range (id 69)
320  */
321  virtual void Insert(const Position& pos, const std::string& rFileName);
322 
323  /**
324  * Append specified entry.
325  * This method takes a copy of the entry to replace and the
326  * vector becomes the owner of the copy.
327  *
328  * @param rElem
329  * Element to append
330  * @exception Exception
331  * - Cannot cast element type (63)
332  */
333  virtual void PushBack(const Type& rElem);
334 
335  /**
336  * Append specified entry.
337  * This method avoids to copy the entry to replace and only records the reference.
338  * The vector does not take ownership of the new entry. I.e., when the vector is destroyed, or the
339  * entry is deleted from the vector, the entry itself remains allocated.
340  *
341  *
342  * @param rElem
343  * Element to insert
344  * @exception Exception
345  * - Cannot cast element type (63)
346  */
347  virtual void PushBack(Type* rElem);
348 
349  /**
350  * Append specified entry.
351  * This method reads the sepcified entry from file and the
352  * vector becomes the owner of the new entry.
353  *
354  * @param rFileName
355  * Element to insert
356  */
357  virtual void PushBack(const std::string& rFileName);
358 
359  /**
360  * Append specified entry.
361  * Synonymous for PushBack.
362  * This method takes a copy of the entry to replace and the
363  * vector becomes the owner of the copy.
364  *
365  * @param rElem
366  * Element to append
367  * @exception Exception
368  * - Cannot cast element type (63)
369  */
370  virtual void Append(const Type& rElem);
371 
372  /**
373  * Append specified entry.
374  * Synonymous for PushBack.
375  * This method avoids to copy the entry to replace and only records the reference.
376  * The vector does not take ownership of the new entry. I.e., when the vector is destroyed, or the
377  * entry is deleted from the vector, the entry itself remains allocated.
378  *
379  *
380  * @param rElem
381  * Element to insert
382  * @exception Exception
383  * - Cannot cast element type (63)
384  */
385  virtual void Append(Type* rElem);
386 
387  /**
388  * Append specified entry.
389  * Synonymous for PushBack.
390  * This method reads the sepcified entry from file and the
391  * vector becomes the owner of the new entry.
392  *
393  * @param rFileName
394  * Element to insert
395  */
396  virtual void Append(const std::string& rFileName);
397 
398  /**
399  * Specify a filename.
400  * When each entry has a filenam specified,
401  * file io of the vector will be to indivudual files.
402  *
403  * @param pos
404  * Position of entry
405  * @param rFileName
406  * Filename relative to vector file
407  * @exception Exception
408  * - Position out of range (id 69)
409  */
410  void FilenameAt(const Position& pos, const std::string& rFileName);
411 
412  /**
413  * Get filename of entry.
414  *
415  * @param pos
416  * Position of entry
417  * @return
418  * Filename assoiated with entry
419  * @exception Exception
420  * - Position out of range (id 69)
421  */
422  const std::string& FilenameAt(const Position& pos) const;
423 
424  /**
425  * Take ownership of all entries.
426  * Thsi method will take ownership of all entries, including those, that
427  * have been set by pointer reference. When the vector is destructed, all
428  * entries will be destructed, too. However, write access may invalidate
429  * element pointers.
430  *
431  */
432  void TakeOwnership(void);
433 
434  /**
435  * Take local copies of all entries.
436  * This method will construct local copies of all entries not previously
437  * owned.
438  */
439  void TakeCopies(void);
440 
441 protected:
442 
443 
444  /**
445  * Token output, debugging see Type::DWrite for public wrappers.
446  * The method assumes that the type parameter is a faudes type and uses
447  * the provide write method per entry. Reimplement this function in derived
448  * classes for non-faudes type vectors.
449  * @param rTw
450  * Reference to TokenWriter
451  * @param rLabel
452  * Label of section to write, defaults to name of set
453  * @param pContext
454  * Write context to provide contextual information
455  */
456  virtual void DoDWrite(TokenWriter& rTw,const std::string& rLabel="", const Type* pContext=0) const;
457 
458  /**
459  * Token output, see Type::SWrite for public wrappers.
460  * The method assumes that the type parameter is a faudes type and uses
461  * the provide write method per entry. Reimplement this function in derived
462  * classes for non-faudes type vectors.
463  *
464  * @param rTw
465  * Reference to TokenWriter
466  *
467  * @exception Exception
468  * - IO errors (id 2)
469  */
470  virtual void DoSWrite(TokenWriter& rTw) const;
471 
472  /**
473  * Token output, see Type::Write for public wrappers.
474  * The method assumes that the type parameter is a faudes type and uses
475  * the provide write method per entry. Reimplement this function in derived
476  * classes for non-faudes type vectors.
477  *
478  * @param rTw
479  * Reference to TokenWriter
480  * @param rLabel
481  * Label of section to write, defaults to name of set
482  * @param pContext
483  * Write context to provide contextual information
484  */
485  virtual void DoWrite(TokenWriter& rTw, const std::string& rLabel="", const Type* pContext=0) const;
486 
487  /**
488  * Token input, see Type::Read for public wrappers.
489  * The method assumes that the type parameter is a faudes type and uses
490  * the provide read method per entry. Reimplement this function in derived
491  * classes for non-faudes type vectors.
492  * By convention, the default label "" should be translated to some meaningful default,
493  * eg "GeneratorVector" for a vector of generators. The pContext pointer can be type-checked
494  * and interpreted, ie as a symboltable to provide symbolic names. It is also
495  * passed on to vector entries.
496  *
497  * @param rTr
498  * Reference to TokenReader
499  * @param rLabel
500  * Label of section to read, defaults to name of set
501  * @param pContext
502  * Read context to provide contextual information
503  */
504  virtual void DoRead(TokenReader& rTr, const std::string& rLabel = "", const Type* pContext=0);
505 
506  /** Assignment method */
507  void DoAssign(const vBaseVector& rSourceVector);
508 
509  /** Internal entry data type */
511  public:
513  std::string mFileName;
514  bool mMine;
515  };
516 
517  /** STL vector of element */
518  std::vector<ElementRecord> mVector;
519 
520  /** convenience typedef */
521  typedef std::vector<ElementRecord>::iterator iterator;
522 
523 private:
524 
525  /** Name of TBaseVector */
526  std::string mMyName;
527 
528 };
529 
530 
531 /** @} doxygen group */
532 
533 
534 /** @addtogroup ContainerClasses */
535 /** @{*/
536 
537 /**
538  * Vector template.
539  *
540  * The vector templates specializes the bass vBaseVector in that it uses the template
541  * paremeter to specify the type of its entries. See vBaseVector for element access
542  * methods.
543  *
544  * TVectorSet serves is used to implement the libFaudes vectors
545  * - GeneratorVector (vector or generators)
546  * - SytemVector (vector or generators)
547  * - EventSetVector (vector of event sets)
548  * - AlphabetVector (vector of event sets)
549  *
550  *
551  */
552 
553 template<class T>
554 class TBaseVector : public vBaseVector {
555 
557 
558 public:
559 
560  /**
561  * Constructor.
562  */
563  TBaseVector(void);
564 
565  /**
566  * Copy-constructor.
567  *
568  * @param rOtherSet
569  * Source to copy from
570  */
571  TBaseVector(const TBaseVector& rOtherSet);
572 
573  /**
574  * Copy-constructor. This version takes any vector as source,
575  * but throughs an exception, if element types dont match.
576  *
577  * @param rOtherSet
578  * Source to copy from
579  * @exception Exception
580  * - Cannot cast elements (63)
581  */
582  TBaseVector(const vBaseVector& rOtherSet);
583 
584  /**
585  * Constructor from file.
586  *
587  * @param rFilename
588  * Name of File
589  * @param rLabel
590  * Section for the set in the file;
591  */
592  TBaseVector(const std::string& rFilename, const std::string& rLabel = "BaseVector");
593 
594  /**
595  * Virtual destructor
596  */
597  virtual ~TBaseVector(void);
598 
599 
600  /**
601  * Prototype for vector entries.
602  * This template class uses the virtual function to know its element type.
603  *
604  * @return
605  * Element protoype
606  */
607  virtual const T* Elementp(void) const;
608 
609  /**
610  * Test whether the specified element is acceptebla for this vector.
611  * This is a convenience wrapper for Elementp.
612  *
613  * @param rElement
614  * Element to type check.
615  * @return
616  * True, if provided element is accepted by this vector.
617  */
618  virtual bool ElementTry(const Type& rElement) const;
619 
620  /**
621  * convenience typedef for positions
622  */
623  typedef std::vector<int>::size_type Position;
624 
625  /**
626  * Access element.
627  *
628  * @param pos
629  * Specify entry to access
630  * @exception Exception
631  * - Position out of range (id 69)
632  */
633  virtual const T& At(const Position& pos) const;
634 
635  /**
636  * Access element.
637  *
638  * @param pos
639  * Specify entry to access
640  * @exception Exception
641  * - Position out of range (id 69)
642  */
643  virtual T& At(const Position& pos);
644 
645 protected:
646 
647  /** Assignment method */
648  void DoAssign(const TBaseVector<T>& rSourceVector);
649 
650 
651 };
652 
653 
654 /** @} doxygen group */
655 
656 
657 
658 /*
659 ******************************************************************************************
660 ******************************************************************************************
661 ******************************************************************************************
662 
663 Implementation of TBaseVector
664 
665 ******************************************************************************************
666 ******************************************************************************************
667 ******************************************************************************************
668 */
669 
670 /* convenience access to relevant scopes */
671 #define THIS TBaseVector<T>
672 #define TEMP template<class T>
673 #define BASE vBaseVector
674 
675 
676 // faudes type std
677 FAUDES_TYPE_TIMPLEMENTATION(Void,THIS,vBaseVector,TEMP)
678 
679 // TBaseVector()
681  vBaseVector()
682 {
683  FD_DC("TBaseVector(" << this << ")::TBaseVector()");
684 }
685 
686 
687 // TBaseVector(filename)
688 TEMP THIS::TBaseVector(const std::string& rFileName, const std::string& rLabel) :
689  vBaseVector()
690 {
691  FD_DC("TBaseVector(" << this << ")::TBaseVector()");
692  // do read;
693  Read(rFileName,rLabel);
694 }
695 
696 
697 // TBaseVector(rOtherSet)
698 TEMP THIS::TBaseVector(const TBaseVector& rOtherVector) :
699  vBaseVector()
700 {
701  FD_DC("TBaseVector(" << this << ")::TBaseVector(rOtherVector " << &rOtherVector << "): copy construct");
702  DoAssign(rOtherVector);
703 }
704 
705 // TBaseVector(rOtherSet)
706 TEMP THIS::TBaseVector(const vBaseVector& rOtherVector) :
707  vBaseVector()
708 {
709  FD_DC("TBaseVector(" << this << ")::TBaseVector([v] rOtherVector " << &rOtherVector << "): copy construct");
710  Assign(rOtherVector);
711 }
712 
713 // destructor
714 TEMP THIS::~TBaseVector(void) {
715  FD_DC("TBaseVector(" << this << ")::~TBaseVector()");
716 }
717 
718 
719 // element prototype
720 TEMP const T* THIS::Elementp(void) const {
721  static T tproto;
722  return &tproto;
723 }
724 
725 // test element type
726 TEMP bool THIS::ElementTry(const Type& rElement) const {
727  FD_DC("TBaseVector::ElementTry(): casting from " << typeid(rElement).name() << " to " << typeid(*Elementp()).name());
728  return Elementp()->Cast(&rElement)!=NULL;
729 }
730 
731 
732 // assignment
733 TEMP void THIS::DoAssign(const THIS& rSourceVector) {
734  FD_DC("TBaseVector(" << this << ")::DoAssign(rOtherVector " << &rSourceVector << ")");
735  // base can do it
736  BASE::DoAssign(rSourceVector);
737  // done
738  FD_DC("TBaseVector(" << this << ")::DoAssign(rOtherVector " << &rSourceVector << "): done");
739 }
740 
741 // At()
742 TEMP const T& THIS::At(const Position& pos) const {
743 #ifdef FAUDES_CHECKED
744  if(pos >= mVector.size()) {
745  std::stringstream errstr;
746  errstr << "index out of range" << std::endl;
747  throw Exception("TBaseVector::At", errstr.str(), 62);
748  }
749 #endif
750 #ifdef FAUDES_DEBUG_CODE
751  if(!dynamic_cast<T*>(mVector[pos].pElement)){
752  std::stringstream errstr;
753  errstr << "internal type error" << std::endl;
754  throw Exception("TBaseVector::At", errstr.str(), 63);
755  }
756 #endif
757  return *dynamic_cast<T*>(mVector[pos].pElement);
758 }
759 
760 // At()
761 TEMP T& THIS::At(const Position& pos) {
762 #ifdef FAUDES_CHECKED
763  if(pos >= mVector.size()) {
764  std::stringstream errstr;
765  errstr << "index out of range" << std::endl;
766  throw Exception("TBaseVector::At", errstr.str(), 62);
767  }
768 #endif
769 #ifdef FAUDES_DEBUG_CODE
770  if(!dynamic_cast<T*>(mVector[pos].pElement)){
771  std::stringstream errstr;
772  errstr << "internal type error" << std::endl;
773  throw Exception("TBaseVector::At", errstr.str(), 63);
774  }
775 #endif
776  return *dynamic_cast<T*>(mVector[pos].pElement);
777 }
778 
779 
780 
781 
782 
783 /* undefine local shortcuts */
784 #undef THIS
785 #undef TEMP
786 #undef BASE
787 
788 
789 } // namespace faudes
790 
791 #endif
Classes AttributeVoid and AttributeFlags
#define TEMP
#define THIS
Compiletime options.
#define FD_DC(message)
Debug: optional report on container operations.
#define FAUDES_API
Interface export/import symbols: windows.
Definition: cfl_platform.h:81
Class TokenReader.
Class TokenWriter.
Runtime interface, faudes types.
#define FAUDES_TYPE_TDECLARATION(ftype, ctype, cbase)
faudes type declaration macro, template version
Definition: cfl_types.h:878
#define FAUDES_TYPE_DECLARATION(ftype, ctype, cbase)
faudes type declaration macro
Definition: cfl_types.h:867
#define FAUDES_TYPE_TIMPLEMENTATION(ftype, ctype, cbase, ctemp)
faudes type implementation macros, overall
Definition: cfl_types.h:972
Faudes exception class.
Vector template.
std::vector< int >::size_type Position
convenience typedef for positions
TBaseVector(const std::string &rFilename, const std::string &rLabel="BaseVector")
Constructor from file.
virtual const T * Elementp(void) const
Prototype for vector entries.
virtual bool ElementTry(const Type &rElement) const
Test whether the specified element is acceptebla for this vector.
virtual const T & At(const Position &pos) const
Access element.
virtual ~TBaseVector(void)
Virtual destructor.
TBaseVector(const vBaseVector &rOtherSet)
Copy-constructor.
TBaseVector(void)
Constructor.
TBaseVector(const TBaseVector &rOtherSet)
Copy-constructor.
void DoAssign(const TBaseVector< T > &rSourceVector)
Assignment method
virtual T & At(const Position &pos)
Access element.
A TokenReader reads sequential tokens from a file or string.
A TokenWriter writes sequential tokens to a file, a string or stdout.
Base class of all libFAUDES objects that participate in the run-time interface.
Definition: cfl_types.h:239
void Read(const std::string &rFileName, const std::string &rLabel="", const Type *pContext=0)
Read configuration data from file with label specified.
Definition: cfl_types.cpp:261
virtual Type & Assign(const Type &rSrc)
Assign configuration data from other object.
Definition: cfl_types.cpp:77
Internal entry data type.
Vector bass class.
size_t Position
convenience typedef for positions (must be unsigned)
std::string mMyName
Name of TBaseVector.
std::vector< ElementRecord >::iterator iterator
convenience typedef
std::vector< ElementRecord > mVector
STL vector of element.
libFAUDES resides within the namespace faudes.
uint32_t Idx
Type definition for index type (allways 32bit)

libFAUDES 2.32b --- 2024.03.01 --- c++ api documentaion by doxygen