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 NAMESET_H
00024 
00025 #include "definitions.h"
00026 #include "exception.h"
00027 #include "indexset.h"
00028 #include "symboltable.h"
00029 #include "tokenreader.h"
00030 #include "tokenwriter.h"
00031 #include <fstream>
00032 #include <sstream>
00033 #include <iostream>
00034 #include <set>
00035 #include <assert.h>
00036 
00037 namespace faudes {
00038 
00042 // Forward declaration for the attributed version of nameset
00043 template<class Attr> class TaNameSet;
00044 
00045 
00073 class NameSet : public virtual IndexSet {
00074 
00079   template<class Attr> friend class TaNameSet;
00080 
00081  public:
00085   NameSet(void);
00086          
00095   NameSet(const NameSet& rOtherSet);
00096 
00097 
00109   NameSet(const std::string& rFilename, const std::string& rLabel = "NameSet");
00110 
00117   NameSet NewN() const;
00118 
00126   NameSet* NewP() const;
00127 
00134   SymbolTable* SymbolTablep(void) const;
00135 
00144   void SymbolTablep(SymbolTable* pSymTab);
00145 
00157   bool Insert(Idx index);
00158 
00170   Idx Insert(const std::string& rName);
00171 
00180    void InsertSet(const NameSet& rOtherSet);
00181 
00190   bool Erase(Idx index);
00191 
00202   bool Erase(const std::string& rName);
00203 
00214   Iterator Erase(const Iterator& pos);
00215 
00224    void EraseSet(const NameSet& rOtherSet);
00225 
00240   void SymbolicName(Idx index, const std::string& rName);
00241 
00242         
00256   void SymbolicName(const std::string& rName, const std::string& rNewName);
00257 
00267   std::string SymbolicName(Idx index) const;
00268 
00278   Idx Index(const std::string& rName) const;
00279 
00289   bool Exists(Idx index) const;
00290 
00300   bool Exists(const std::string& rName) const;
00301 
00312   NameSet::Iterator Find(Idx index) const;
00313 
00324   NameSet::Iterator Find(const std::string& rName) const;
00325 
00326 
00336   NameSet operator + (const NameSet& rOtherSet) const;
00337 
00347   NameSet operator - (const NameSet& rOtherSet) const;
00348 
00358    NameSet operator * (const NameSet& rOtherSet) const;
00359 
00360 
00362   bool operator == (const NameSet& rOtherSet) const;
00363 
00365   bool operator != (const NameSet& rOtherSet) const;
00366 
00368   bool operator <= (const NameSet& rOtherSet) const;
00369 
00371   bool operator >= (const NameSet& rOtherSet) const;
00372 
00373 
00384   std::string Str(Idx index) const;
00385 
00386 
00387  protected:
00388 
00390   SymbolTable* mpSymbolTable;
00391 
00403   void DoWrite(TokenWriter& tw, const std::string& rLabel) const;
00404         
00414   void DoDWrite(TokenWriter& tw) const;
00415 
00430   void DoRead(TokenReader& tr, const std::string& rLabel = "NameSet");
00431 
00432 
00433 };
00434 
00435 
00436 /* convenience typedef for eventset */
00437 typedef  NameSet EventSet;  
00438 
00439 
00462 template<class Attr>
00463 class TaNameSet : public NameSet , public TaIndexSet<Attr> {
00464 
00469   template<class A> friend class TaNameSet;
00470 
00471 
00472  public:
00476   TaNameSet(void);
00477          
00484   TaNameSet(const TaNameSet& rOtherSet);
00485 
00492   TaNameSet(const NameSet& rOtherSet);
00493 
00504   TaNameSet(const std::string& rFilename, const std::string& rLabel = "NameSet");
00505 
00509    virtual ~TaNameSet(void) {}
00510 
00517   TaNameSet NewN() const;
00518 
00525   TaNameSet* NewP() const;
00526 
00533   template<class OtherAttr>
00534   void CopyWithoutAttributes(TaNameSet<OtherAttr>& rOtherSet) const;
00535 
00536 
00550   bool Insert(Idx index);
00551 
00552 
00566   bool Insert(Idx index, const Attr& rAttr);
00567 
00581   Idx Insert(const std::string& rName);
00582 
00597   Idx Insert(const std::string& rName, const Attr& rAttr);
00598 
00608    void InsertSet(const NameSet& rOtherSet);
00609 
00617    void InsertSet(const TaNameSet& rOtherSet);
00618 
00628   bool Erase(Idx index);
00629 
00640   bool Erase(const std::string& rName);
00641 
00652   Iterator Erase(const Iterator& pos);
00653 
00663    void EraseSet(const NameSet& rOtherSet);
00664 
00674    void EraseSet(const TaNameSet& rOtherSet);
00675 
00686   std::string Str(Idx index) const;
00687 
00688  protected:
00689 
00701   void DoWrite(TokenWriter& tw, const std::string& rLabel) const;
00702         
00703 
00714   void DoDWrite(TokenWriter& tw) const;
00715 
00729   void DoRead(TokenReader& tr, const std::string& rLabel = "NameSet");
00730 
00731 
00732 };
00733 
00734 
00736 #define TaEventSet TaNameSet  
00737 
00740 /*
00741 *************************************************************************************
00742 *************************************************************************************
00743  Implementation
00744 *************************************************************************************
00745 *************************************************************************************
00746 */
00747 
00748 
00749 
00750 // empty constructor
00751 template<class Attr>
00752 TaNameSet<Attr>::TaNameSet(void) {
00753   FD_DC("TaNameSet(" << this << ")::TaNameSet()");
00754   mpSymbolTable = SymbolTable::GlobalEventSymbolTablep();
00755   IndexSet::Name("NameSet");
00756 }
00757 
00758 // constructor form other nameset
00759 template<class Attr>
00760 TaNameSet<Attr>::TaNameSet(const TaNameSet& rOtherSet) :
00761   IndexSet(),                     // todo: init in diamond shape inheritance
00762   NameSet(rOtherSet),
00763   TaIndexSet<Attr>(rOtherSet)
00764 {
00765   FD_DC("TaNameSet(" << this << ")::TaNameSet(rOtherSet " << &rOtherSet << ")");
00766   TaIndexSet<Attr>::mAttributeMap=rOtherSet.mAttributeMap;
00767 }
00768 
00769 // constructor form other nameset
00770 template<class Attr>
00771 TaNameSet<Attr>::TaNameSet(const NameSet& rOtherSet) :
00772   NameSet(rOtherSet),
00773   TaIndexSet<Attr>(rOtherSet)
00774 {
00775   FD_DC("TaNameSet(" << this << ")::TaNameSet(rOtherSet " << &rOtherSet << ")");
00776 }
00777 
00778 
00779 // read file constructor
00780 template<class Attr>
00781 TaNameSet<Attr>::TaNameSet(const std::string& rFilename, const std::string& rLabel) {
00782   FD_DC("TaNameSet(" << this << ")::TaNameSet(" << rFilename << ")");
00783   mpSymbolTable = SymbolTable::GlobalEventSymbolTablep();
00784   Read(rFilename, rLabel);
00785 }
00786 
00787 // NewN()
00788 template<class Attr>
00789 TaNameSet<Attr> TaNameSet<Attr>::NewN(void) const {
00790   TaNameSet res;
00791   res.mpSymbolTable=mpSymbolTable;
00792   return res;
00793 }
00794 
00795 // NewP()
00796 template<class Attr>
00797 TaNameSet<Attr>* TaNameSet<Attr>::NewP(void) const {
00798   TaNameSet* res = new TaNameSet();
00799   res.mpSymbolTable=mpSymbolTable;
00800   return res;
00801 }
00802 
00803 // CopyWithoutAttributes(rOtherSet)
00804 template<class Attr>
00805 template<class OtherAttr>
00806 void TaNameSet<Attr>::CopyWithoutAttributes(TaNameSet<OtherAttr>& rOtherSet) const {
00807   rOtherSet.Clear();
00808   rOtherSet.mMyName=TaIndexSet<Attr>::mMyName;
00809   rOtherSet.mIndexSet=TaIndexSet<Attr>::mIndexSet;
00810   rOtherSet.mpSymbolTable=mpSymbolTable;
00811 }  
00812 
00813 // DoWrite(tw, rLabel)
00814 template<class Attr>
00815 void TaNameSet<Attr>::DoWrite(TokenWriter& tw, const std::string& rLabel) const {
00816   std::string label=rLabel;
00817   if(label=="") label=Name(); 
00818   tw.WriteBegin(label);
00819   Token token;
00820   Iterator it;
00821   for (it = TaIndexSet<Attr>::Begin(); it != TaIndexSet<Attr>::End(); ++it) {
00822 #ifdef FAUDES_CHECKED
00823     if (SymbolicName(*it) == "") {
00824       FD_ERR("TaNameSet::Write(): "
00825      << "index " << *it << " not in SymbolTable. aborting...");
00826       abort();
00827     }
00828 #endif
00829     token.SetString(SymbolicName(*it));
00830     tw << token;
00831     TaIndexSet<Attr>::Attribute(*it).Write(tw);
00832   }
00833   tw.WriteEnd(label);
00834 }
00835 
00836 
00837 // DoDWrite(tw)
00838 template<class Attr>
00839 void TaNameSet<Attr>::DoDWrite(TokenWriter& tw) const {
00840   Token token;
00841   Iterator it;
00842   tw.WriteBegin(IndexSet::Name());
00843   for (it = TaIndexSet<Attr>::Begin(); it != TaIndexSet<Attr>::End(); ++it) {
00844     tw << Str(*it);
00845     TaIndexSet<Attr>::Attribute(*it).Write(tw);
00846   }
00847   tw.WriteEnd(IndexSet::Name());
00848 }
00849 
00850 
00851 // DoRead(rTr, rLabel)
00852 template<class Attr>
00853 void TaNameSet<Attr>::DoRead(TokenReader& rTr, const std::string& rLabel) {
00854   FD_DC("TaNameSet(" << this << ")::DoRead(tr," << rLabel << ") with symtab " << NameSet::mpSymbolTable);
00855   std::string name;
00856   Attr attribute;
00857   AttributeVoid vattr;
00858   IndexSet::Name(rLabel);
00859   Token token;
00860   TaIndexSet<Attr>::Clear();
00861 
00862   rTr.SeekBegin(rLabel); 
00863   while(rTr.Peek(token)) {
00864     // break on end
00865     if (token.Type() == Token::End) {
00866       break;
00867     }
00868     // read by name
00869     if (token.Type() == Token::String) {
00870       rTr >> token;
00871       FD_DC("TaNameSet(" << this << ")::DoRead(..): inserting element \"" 
00872       << token.StringValue() << "\"");
00873       name=token.StringValue();
00874       // read my attribute
00875       attribute.Read(rTr);
00876       // skip other attributes
00877       vattr.Read(rTr);
00878       // insert element
00879       Insert(name,attribute);
00880       continue;
00881     }
00882     // cannot process token
00883     std::stringstream errstr;
00884     errstr << "Invalid token" << rTr.FileLine();
00885     throw Exception("TaNameSet::DoRead", errstr.str(), 50);
00886   }
00887   rTr.SeekEnd(rLabel);
00888 }
00889 
00890 
00891 
00892 // Insert(index)
00893 template<class Attr>
00894 bool TaNameSet<Attr>::Insert(Idx index) { 
00895 #ifdef FAUDES_CHECKED
00896   if(!mpSymbolTable->Exists(index)) {
00897     std::stringstream errstr;
00898     errstr << "index " << index << " has no symbolic name" << std::endl;
00899     throw Exception("TaNameSet::Insert", errstr.str(), 65);
00900   }
00901 #endif
00902   return TaIndexSet<Attr>::Insert(index);
00903 }
00904 
00905         
00906 // Insert(index,attr)
00907 template<class Attr>
00908 bool TaNameSet<Attr>::Insert(Idx index, const Attr& attr) { 
00909 #ifdef FAUDES_CHECKED
00910   if(!mpSymbolTable->Exists(index)) {
00911     std::stringstream errstr;
00912     errstr << "index " << index << " has no symbolic name" << std::endl;
00913     throw Exception("TaNameSet::Insert", errstr.str(), 65);
00914   }
00915 #endif
00916   return TaIndexSet<Attr>::Insert(index,attr);
00917 }
00918 
00919         
00920 // Insert(rName)
00921 template<class Attr>
00922 Idx TaNameSet<Attr>::Insert(const std::string& rName) {
00923   FD_DC("TaNameSet(" << this << ")::Insert(" << rName <<")");
00924   Idx index= NameSet::Insert(rName); // automatic: keep attribute if exists
00925   return index;
00926 }
00927 
00928 // Insert(rName, attr)
00929 template<class Attr>
00930 Idx TaNameSet<Attr>::Insert(const std::string& rName, const Attr& attr) {
00931   FD_DC("TaNameSet(" << this << ")::Insert(" << rName <<")");
00932   Idx index= NameSet::Insert(rName);
00933   TaIndexSet<Attr>::Attribute(index,attr);
00934   return index;
00935 }
00936 
00937 // InsertSet(set)
00938 template<class Attr>
00939 void TaNameSet<Attr>::InsertSet(const NameSet& rOtherSet) {
00940   FD_DC("TaNameSet(" << this << ")::InsertSet(" << rOtherSet.ToString() << ")");
00941 #ifdef FAUDES_CHECKED
00942   if(rOtherSet.mpSymbolTable!=mpSymbolTable) {
00943     std::stringstream errstr;
00944     errstr << "symboltable mismach aka not implemented" << std::endl;
00945     throw Exception("TaNameSet::InsertSet", errstr.str(), 67);
00946   }
00947 #endif
00948   NameSet::InsertSet(rOtherSet);
00949 }
00950 
00951 // InsertSet(set)
00952 template<class Attr>
00953 void TaNameSet<Attr>::InsertSet(const TaNameSet& rOtherSet) {
00954   FD_DC("TaNameSet(" << this << ")::InsertSet(" << rOtherSet.ToString() << ")");
00955 #ifdef FAUDES_CHECKED
00956   if(rOtherSet.mpSymbolTable!=mpSymbolTable) {
00957     std::stringstream errstr;
00958     errstr << "symboltable mismach aka not implemented" << std::endl;
00959     throw Exception("TaNameSet::InsertSet", errstr.str(), 200);
00960   }
00961 #endif
00962   TaIndexSet<Attr>::InsertSet(rOtherSet);
00963 }
00964     
00965 // Erase(index)
00966 template<class Attr>
00967 bool TaNameSet<Attr>::Erase(Idx index) {
00968   FD_DC("TaNameSet(" << this << ")::Erase(" << index <<")");
00969   return TaIndexSet<Attr>::Erase(index);
00970 }
00971 
00972 // Erase(rName)
00973 template<class Attr>
00974 bool TaNameSet<Attr>::Erase(const std::string& rName) {
00975   FD_DC("TaNameSet(" << this << ")::Erase(" << rName <<")");
00976   Idx index = mpSymbolTable->Index(rName);
00977 #ifdef FAUDES_CHECKED
00978   if (index == 0) {
00979     std::stringstream errstr;
00980     errstr << "name \"" << rName << "\" not found in TaNameSet" << std::endl;
00981     throw Exception("TaNameSet::Erase", errstr.str(), 60);
00982   }
00983 #endif
00984   return TaIndexSet<Attr>::Erase(index);
00985 }
00986 
00987 // Erase(pos)
00988 template<class Attr>
00989 typename TaNameSet<Attr>::Iterator TaNameSet<Attr>::Erase(const Iterator& pos) {
00990   return TaIndexSet<Attr>::Erase(pos);
00991 }
00992 
00993 // EraseSet(set)
00994 template<class Attr>
00995 void TaNameSet<Attr>::EraseSet(const NameSet& rOtherSet) {
00996   FD_DC("TaNameSet(" << this << ")::EraseSet(" << rOtherSet.ToString() << ")");
00997 #ifdef FAUDES_CHECKED
00998   if(rOtherSet.mpSymbolTable!=mpSymbolTable) {
00999     std::stringstream errstr;
01000     errstr << "symboltable mismach aka not implemented" << std::endl;
01001     throw Exception("TaNameSet::EraseSet", errstr.str(), 67);
01002   }
01003 #endif
01004   TaIndexSet<Attr>::EraseSet(rOtherSet);
01005 }
01006 
01007 // EraseSet(set)
01008 template<class Attr>
01009 void TaNameSet<Attr>::EraseSet(const TaNameSet& rOtherSet) {
01010   FD_DC("TaNameSet(" << this << ")::EraseSet(" << rOtherSet.ToString() << ")");
01011 #ifdef FAUDES_CHECKED
01012   if(rOtherSet.mpSymbolTable!=mpSymbolTable) {
01013     std::stringstream errstr;
01014     errstr << "symboltable mismatch aka not implemented" << std::endl;
01015     throw Exception("TaNameSet::EraseSet", errstr.str(), 67);
01016   }
01017 #endif
01018   TaIndexSet<Attr>::EraseSet(rOtherSet);
01019 }
01020     
01021 // Str()
01022 template<class Attr>
01023 std::string TaNameSet<Attr>::Str(Idx index) const {
01024   return NameSet::Str(index);
01025 }
01026 
01027 
01028 
01029 } // namespace faudes
01030 
01031 #define NAMESET_H
01032 #endif 
01033 

Generated on Fri May 9 11:26:47 2008 for libFAUDES 2.09b by  doxygen 1.4.4