00001
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
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
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
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
00744
00745
00746
00747
00748
00749
00750
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
00759 template<class Attr>
00760 TaNameSet<Attr>::TaNameSet(const TaNameSet& rOtherSet) :
00761 IndexSet(),
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
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
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
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
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
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
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
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
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
00865 if (token.Type() == Token::End) {
00866 break;
00867 }
00868
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
00875 attribute.Read(rTr);
00876
00877 vattr.Read(rTr);
00878
00879 Insert(name,attribute);
00880 continue;
00881 }
00882
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
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
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
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);
00925 return index;
00926 }
00927
00928
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
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
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
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
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
00988 template<class Attr>
00989 typename TaNameSet<Attr>::Iterator TaNameSet<Attr>::Erase(const Iterator& pos) {
00990 return TaIndexSet<Attr>::Erase(pos);
00991 }
00992
00993
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
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
01022 template<class Attr>
01023 std::string TaNameSet<Attr>::Str(Idx index) const {
01024 return NameSet::Str(index);
01025 }
01026
01027
01028
01029 }
01030
01031 #define NAMESET_H
01032 #endif
01033