nameset.h

Go to the documentation of this file.
00001 
00003 /* FAU Discrete Event Systems Library (libfaudes)
00004 
00005    Copyright (C) 2006  Bernd Opitz
00006    Exclusive copyright is granted to Klaus Schmidt
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 #ifndef FAUDES_NAMESET_H
00024 #define FAUDES_NAMESET_H
00025 
00026 #include "definitions.h"
00027 #include "exception.h"
00028 #include "indexset.h"
00029 #include "symboltable.h"
00030 #include "tokenreader.h"
00031 #include "tokenwriter.h"
00032 #include <fstream>
00033 #include <sstream>
00034 #include <iostream>
00035 #include <set>
00036 #include <assert.h>
00037 
00038 namespace faudes {
00039 
00043 // Forward declaration for the attributed version of nameset
00044 template<class Attr> class TaNameSet;
00045 
00046 
00074 class NameSet : public virtual IndexSet {
00075 
00080   template<class Attr> friend class TaNameSet;
00081 
00082  public:
00086   NameSet(void);
00087          
00096   NameSet(const NameSet& rOtherSet);
00097 
00098 
00110   NameSet(const std::string& rFilename, const std::string& rLabel = "NameSet");
00111 
00118   NameSet NewN() const;
00119 
00127   NameSet* NewP() const;
00128 
00135   SymbolTable* SymbolTablep(void) const;
00136 
00145   void SymbolTablep(SymbolTable* pSymTab);
00146 
00158   bool Insert(Idx index);
00159 
00171   Idx Insert(const std::string& rName);
00172 
00181    void InsertSet(const NameSet& rOtherSet);
00182 
00191   bool Erase(Idx index);
00192 
00203   bool Erase(const std::string& rName);
00204 
00215   Iterator Erase(const Iterator& pos);
00216 
00225    void EraseSet(const NameSet& rOtherSet);
00226 
00241   void SymbolicName(Idx index, const std::string& rName);
00242 
00243         
00257   void SymbolicName(const std::string& rName, const std::string& rNewName);
00258 
00268   std::string SymbolicName(Idx index) const;
00269 
00279   Idx Index(const std::string& rName) const;
00280 
00290   bool Exists(Idx index) const;
00291 
00301   bool Exists(const std::string& rName) const;
00302 
00313   NameSet::Iterator Find(Idx index) const;
00314 
00325   NameSet::Iterator Find(const std::string& rName) const;
00326 
00327 
00337   NameSet operator + (const NameSet& rOtherSet) const;
00338 
00348   NameSet operator - (const NameSet& rOtherSet) const;
00349 
00359    NameSet operator * (const NameSet& rOtherSet) const;
00360 
00361 
00363   bool operator == (const NameSet& rOtherSet) const;
00364 
00366   bool operator != (const NameSet& rOtherSet) const;
00367 
00369   bool operator <= (const NameSet& rOtherSet) const;
00370 
00372   bool operator >= (const NameSet& rOtherSet) const;
00373 
00374 
00385   std::string Str(Idx index) const;
00386 
00387 
00388  protected:
00389 
00391   SymbolTable* mpSymbolTable;
00392 
00406   virtual void DoWrite(TokenWriter& tw, const std::string& rLabel="", const Type* pContext=0) const;
00407         
00421   virtual void DoDWrite(TokenWriter& tw, const std::string& rLabel="", const Type* pContext=0) const;
00422 
00439   virtual void DoRead(TokenReader& tr, const std::string& rLabel = "", const Type* pContext=0);
00440 
00441 
00442 };
00443 
00444 
00445 /* convenience typedef for eventset */
00446 typedef  NameSet EventSet;  
00447 
00448 
00471 template<class Attr>
00472 class TaNameSet : public NameSet , public TaIndexSet<Attr> {
00473 
00478   friend class NameSet;
00479 
00480 
00481  public:
00485   TaNameSet(void);
00486          
00493   TaNameSet(const TaNameSet& rOtherSet);
00494 
00501   TaNameSet(const NameSet& rOtherSet);
00502 
00513   TaNameSet(const std::string& rFilename, const std::string& rLabel = "NameSet");
00514 
00518   virtual ~TaNameSet(void) {};
00519 
00526   TaNameSet NewN() const;
00527 
00534   TaNameSet* NewP() const;
00535 
00542   template<class OtherAttr>
00543   void CopyWithoutAttributes(TaNameSet<OtherAttr>& rOtherSet) const;
00544 
00545 
00559   bool Insert(Idx index);
00560 
00561 
00575   bool Insert(Idx index, const Attr& rAttr);
00576 
00590   Idx Insert(const std::string& rName);
00591 
00606   Idx Insert(const std::string& rName, const Attr& rAttr);
00607 
00617    void InsertSet(const NameSet& rOtherSet);
00618 
00626    void InsertSet(const TaNameSet& rOtherSet);
00627 
00637   bool Erase(Idx index);
00638 
00649   bool Erase(const std::string& rName);
00650 
00661   Iterator Erase(const Iterator& pos);
00662 
00672    void EraseSet(const NameSet& rOtherSet);
00673 
00683    void EraseSet(const TaNameSet& rOtherSet);
00684 
00695   std::string Str(Idx index) const;
00696 
00697  protected:
00698 
00712   virtual void DoWrite(TokenWriter& tw, const std::string& rLabel="", const Type* pContext=0) const;
00713         
00714 
00729   virtual void DoDWrite(TokenWriter& tw, const std::string& rLabel="", const Type* pContext=0) const;
00730 
00746   virtual void DoRead(TokenReader& tr, const std::string& rLabel = "NameSet", const Type* pContext=0);
00747 
00748 
00749 };
00750 
00751 
00753 #define TaEventSet TaNameSet  
00754 
00757 /*
00758 *************************************************************************************
00759 *************************************************************************************
00760  Implementation
00761 *************************************************************************************
00762 *************************************************************************************
00763 */
00764 
00765 
00766 
00767 // empty constructor // todo: investigate base contructor/initialisation
00768 template<class Attr>
00769 TaNameSet<Attr>::TaNameSet(void) {
00770   FD_DC("TaNameSet(" << this << ")::TaNameSet()");
00771   mpSymbolTable = SymbolTable::GlobalEventSymbolTablep();
00772   IndexSet::Name("NameSet");
00773 }
00774 
00775 // constructor form other nameset
00776 template<class Attr>
00777 TaNameSet<Attr>::TaNameSet(const TaNameSet& rOtherSet) :
00778   IndexSet(),                     // todo: init in diamond shape inheritance
00779   NameSet(rOtherSet),
00780   TaIndexSet<Attr>(rOtherSet)
00781 {
00782   FD_DC("TaNameSet(" << this << ")::TaNameSet(rOtherSet " << &rOtherSet << ")");
00783   TaIndexSet<Attr>::mAttributeMap=rOtherSet.mAttributeMap;
00784 }
00785 
00786 // constructor form other nameset
00787 template<class Attr>
00788 TaNameSet<Attr>::TaNameSet(const NameSet& rOtherSet) :
00789   NameSet(rOtherSet),
00790   TaIndexSet<Attr>(rOtherSet)
00791 {
00792   FD_DC("TaNameSet(" << this << ")::TaNameSet(rOtherSet " << &rOtherSet << ")");
00793 }
00794 
00795 
00796 // read file constructor
00797 template<class Attr>
00798 TaNameSet<Attr>::TaNameSet(const std::string& rFilename, const std::string& rLabel) {
00799   FD_DC("TaNameSet(" << this << ")::TaNameSet(" << rFilename << ")");
00800   mpSymbolTable = SymbolTable::GlobalEventSymbolTablep();
00801   Read(rFilename, rLabel);
00802 }
00803 
00804 // NewN()
00805 template<class Attr>
00806 TaNameSet<Attr> TaNameSet<Attr>::NewN(void) const {
00807   TaNameSet res;
00808   res.mpSymbolTable=mpSymbolTable;
00809   return res;
00810 }
00811 
00812 // NewP()
00813 template<class Attr>
00814 TaNameSet<Attr>* TaNameSet<Attr>::NewP(void) const {
00815   TaNameSet* res = new TaNameSet();
00816   res.mpSymbolTable=mpSymbolTable;
00817   return res;
00818 }
00819 
00820 // CopyWithoutAttributes(rOtherSet)
00821 template<class Attr>
00822 template<class OtherAttr>
00823 void TaNameSet<Attr>::CopyWithoutAttributes(TaNameSet<OtherAttr>& rOtherSet) const {
00824   rOtherSet.Clear();
00825   rOtherSet.mMyName=TaIndexSet<Attr>::mMyName;
00826   rOtherSet.mIndexSet=TaIndexSet<Attr>::mIndexSet;
00827   rOtherSet.mpSymbolTable=mpSymbolTable;
00828 }  
00829 
00830 // DoWrite(tw, rLabel)
00831 template<class Attr>
00832 void TaNameSet<Attr>::DoWrite(TokenWriter& tw, const std::string& rLabel, const Type* pContext) const {
00833   std::string label=rLabel;
00834   if(label=="") label=Name(); 
00835   tw.WriteBegin(label);
00836   Token token;
00837   Iterator it;
00838   for (it = TaIndexSet<Attr>::Begin(); it != TaIndexSet<Attr>::End(); ++it) {
00839 #ifdef FAUDES_CHECKED
00840     if (SymbolicName(*it) == "") {
00841       FD_ERR("TaNameSet::Write(): "
00842      << "index " << *it << " not in SymbolTable. aborting...");
00843       abort();
00844     }
00845 #endif
00846     token.SetString(SymbolicName(*it));
00847     tw << token;
00848     TaIndexSet<Attr>::Attribute(*it).Write(tw,"",pContext);
00849   }
00850   tw.WriteEnd(label);
00851 }
00852 
00853 // DoDWrite(tw)
00854 template<class Attr>
00855 void TaNameSet<Attr>::DoDWrite(TokenWriter& tw, const std::string& rLabel, const Type* pContext) const {
00856   std::string label=rLabel;
00857   if(label=="") label=Name(); 
00858   tw.WriteBegin(label);
00859   Token token;
00860   Iterator it;
00861   for (it = TaIndexSet<Attr>::Begin(); it != TaIndexSet<Attr>::End(); ++it) {
00862     tw << Str(*it);
00863     TaIndexSet<Attr>::Attribute(*it).Write(tw,"",pContext);
00864   }
00865   tw.WriteEnd(IndexSet::Name());
00866 }
00867 
00868 
00869 // DoRead(rTr, rLabel)
00870 template<class Attr>
00871 void TaNameSet<Attr>::DoRead(TokenReader& rTr, const std::string& rLabel, const Type* pContext) {
00872   std::string label=rLabel;
00873   if(label=="") label ="NameSet";
00874   FD_DC("TaNameSet(" << this << ")::DoRead(tr," << label << ", " << pContext << 
00875     ") with symtab " << NameSet::mpSymbolTable);
00876   std::string name;
00877   Attr attribute;
00878   IndexSet::Name(label);
00879   Token token;
00880 
00881   rTr.SeekBegin(label); 
00882   while(rTr.Peek(token)) {
00883     // break on end
00884     if (token.Type() == Token::End) {
00885       break;
00886     }
00887     // read by name
00888     if (token.Type() == Token::String) {
00889       rTr >> token;
00890       FD_DC("TaNameSet(" << this << ")::DoRead(..): inserting element \"" 
00891       << token.StringValue() << "\"");
00892       name=token.StringValue();
00893       // read my attribute
00894       attribute.Read(rTr,"",pContext);
00895       // insert element
00896       Insert(name,attribute);
00897       continue;
00898     }
00899     // cannot process token
00900     std::stringstream errstr;
00901     errstr << "Invalid token of type " << token.Type() << " at " << rTr.FileLine();
00902     throw Exception("TaNameSet::DoRead", errstr.str(), 50);
00903   }
00904   rTr.SeekEnd(label);
00905   FD_DC("TaNameSet(" << this << ")::DoRead(tr," << label << ", " << pContext << "): done");
00906 }
00907 
00908 
00909 
00910 // Insert(index)
00911 template<class Attr>
00912 bool TaNameSet<Attr>::Insert(Idx index) { 
00913 #ifdef FAUDES_CHECKED
00914   if(!mpSymbolTable->Exists(index)) {
00915     std::stringstream errstr;
00916     errstr << "index " << index << " has no symbolic name" << std::endl;
00917     throw Exception("TaNameSet::Insert", errstr.str(), 65);
00918   }
00919 #endif
00920   return TaIndexSet<Attr>::Insert(index);
00921 }
00922 
00923         
00924 // Insert(index,attr)
00925 template<class Attr>
00926 bool TaNameSet<Attr>::Insert(Idx index, const Attr& attr) { 
00927 #ifdef FAUDES_CHECKED
00928   if(!mpSymbolTable->Exists(index)) {
00929     std::stringstream errstr;
00930     errstr << "index " << index << " has no symbolic name" << std::endl;
00931     throw Exception("TaNameSet::Insert", errstr.str(), 65);
00932   }
00933 #endif
00934   return TaIndexSet<Attr>::Insert(index,attr);
00935 }
00936 
00937         
00938 // Insert(rName)
00939 template<class Attr>
00940 Idx TaNameSet<Attr>::Insert(const std::string& rName) {
00941   FD_DC("TaNameSet(" << this << ")::Insert(" << rName <<")");
00942   Idx index= NameSet::Insert(rName); // automatic: keep attribute if exists
00943   return index;
00944 }
00945 
00946 // Insert(rName, attr)
00947 template<class Attr>
00948 Idx TaNameSet<Attr>::Insert(const std::string& rName, const Attr& attr) {
00949   FD_DC("TaNameSet(" << this << ")::Insert(" << rName <<")");
00950   Idx index= NameSet::Insert(rName);
00951   TaIndexSet<Attr>::Attribute(index,attr);
00952   return index;
00953 }
00954 
00955 // InsertSet(set)
00956 template<class Attr>
00957 void TaNameSet<Attr>::InsertSet(const NameSet& rOtherSet) {
00958   FD_DC("TaNameSet(" << this << ")::InsertSet(" << rOtherSet.ToString() << ")");
00959 #ifdef FAUDES_CHECKED
00960   if(rOtherSet.mpSymbolTable!=mpSymbolTable) {
00961     std::stringstream errstr;
00962     errstr << "symboltable mismach aka not implemented" << std::endl;
00963     throw Exception("TaNameSet::InsertSet", errstr.str(), 67);
00964   }
00965 #endif
00966   NameSet::InsertSet(rOtherSet);
00967 }
00968 
00969 // InsertSet(set)
00970 template<class Attr>
00971 void TaNameSet<Attr>::InsertSet(const TaNameSet& rOtherSet) {
00972   FD_DC("TaNameSet(" << this << ")::InsertSet(" << rOtherSet.ToString() << ")");
00973 #ifdef FAUDES_CHECKED
00974   if(rOtherSet.mpSymbolTable!=mpSymbolTable) {
00975     std::stringstream errstr;
00976     errstr << "symboltable mismach aka not implemented" << std::endl;
00977     throw Exception("TaNameSet::InsertSet", errstr.str(), 200);
00978   }
00979 #endif
00980   TaIndexSet<Attr>::InsertSet(rOtherSet);
00981 }
00982     
00983 // Erase(index)
00984 template<class Attr>
00985 bool TaNameSet<Attr>::Erase(Idx index) {
00986   FD_DC("TaNameSet(" << this << ")::Erase(" << index <<")");
00987   return TaIndexSet<Attr>::Erase(index);
00988 }
00989 
00990 // Erase(rName)
00991 template<class Attr>
00992 bool TaNameSet<Attr>::Erase(const std::string& rName) {
00993   FD_DC("TaNameSet(" << this << ")::Erase(" << rName <<")");
00994   Idx index = mpSymbolTable->Index(rName);
00995 #ifdef FAUDES_CHECKED
00996   if (index == 0) {
00997     std::stringstream errstr;
00998     errstr << "name \"" << rName << "\" not found in TaNameSet" << std::endl;
00999     throw Exception("TaNameSet::Erase", errstr.str(), 60);
01000   }
01001 #endif
01002   return TaIndexSet<Attr>::Erase(index);
01003 }
01004 
01005 // Erase(pos)
01006 template<class Attr>
01007 typename TaNameSet<Attr>::Iterator TaNameSet<Attr>::Erase(const Iterator& pos) {
01008   return TaIndexSet<Attr>::Erase(pos);
01009 }
01010 
01011 // EraseSet(set)
01012 template<class Attr>
01013 void TaNameSet<Attr>::EraseSet(const NameSet& rOtherSet) {
01014   FD_DC("TaNameSet(" << this << ")::EraseSet(" << rOtherSet.ToString() << ")");
01015 #ifdef FAUDES_CHECKED
01016   if(rOtherSet.mpSymbolTable!=mpSymbolTable) {
01017     std::stringstream errstr;
01018     errstr << "symboltable mismach aka not implemented" << std::endl;
01019     throw Exception("TaNameSet::EraseSet", errstr.str(), 67);
01020   }
01021 #endif
01022   TaIndexSet<Attr>::EraseSet(rOtherSet);
01023 }
01024 
01025 // EraseSet(set)
01026 template<class Attr>
01027 void TaNameSet<Attr>::EraseSet(const TaNameSet& rOtherSet) {
01028   FD_DC("TaNameSet(" << this << ")::EraseSet(" << rOtherSet.ToString() << ")");
01029 #ifdef FAUDES_CHECKED
01030   if(rOtherSet.mpSymbolTable!=mpSymbolTable) {
01031     std::stringstream errstr;
01032     errstr << "symboltable mismatch aka not implemented" << std::endl;
01033     throw Exception("TaNameSet::EraseSet", errstr.str(), 67);
01034   }
01035 #endif
01036   TaIndexSet<Attr>::EraseSet(rOtherSet);
01037 }
01038     
01039 // Str()
01040 template<class Attr>
01041 std::string TaNameSet<Attr>::Str(Idx index) const {
01042   return NameSet::Str(index);
01043 }
01044 
01045 
01046 
01047 } // namespace faudes
01048 
01049 #endif 
01050 

Generated on Mon Nov 10 08:13:14 2008 for libFAUDES 2.11v by  doxygen 1.4.4