1 // Copyright (c) 1994 James Clark 2 // See the file COPYING for copying permission. 3 #pragma ident "%Z%%M% %I% %E% SMI" 4 5 #ifndef NamedTable_INCLUDED 6 #define NamedTable_INCLUDED 1 7 8 #include "Hash.h" 9 #include "StringC.h" 10 #include "Named.h" 11 #include "OwnerTable.h" 12 13 #ifdef SP_NAMESPACE 14 namespace SP_NAMESPACE { 15 #endif 16 17 class NamedTableKeyFunction { 18 public: key(const Named & obj)19 static inline const StringC &key(const Named &obj) { return obj.name(); } 20 }; 21 22 template<class T> class NamedTableIter; 23 template<class T> class ConstNamedTableIter; 24 25 template<class T> 26 class NamedTable { 27 public: NamedTable()28 NamedTable() { } insert(T * p)29 T *insert(T *p) { return (T *)table_.insert(p); } lookup(const StringC & str)30 T *lookup(const StringC &str) const { return (T *)table_.lookup(str); } remove(const StringC & str)31 T *remove(const StringC &str) { return (T *)table_.remove(str); } count()32 size_t count() const { return table_.count(); } clear()33 void clear() { table_.clear(); } swap(NamedTable<T> & to)34 void swap(NamedTable<T> &to) { table_.swap(to.table_); } 35 private: 36 NamedTable(const NamedTable<T> &); // undefined 37 void operator=(const NamedTable<T> &); // undefined 38 OwnerTable<Named, StringC, Hash, NamedTableKeyFunction> 39 table_; 40 friend class NamedTableIter<T>; 41 friend class ConstNamedTableIter<T>; 42 }; 43 44 template<class T> 45 class NamedTableIter { 46 public: NamedTableIter(const NamedTable<T> & table)47 NamedTableIter(const NamedTable<T> &table) : iter_(table.table_) { } next()48 T *next() { return (T *)iter_.next(); } 49 private: 50 OwnerTableIter<Named, StringC, Hash, NamedTableKeyFunction> iter_; 51 }; 52 53 template<class T> 54 class ConstNamedTableIter { 55 public: ConstNamedTableIter(const NamedTable<T> & table)56 ConstNamedTableIter(const NamedTable<T> &table) : iter_(table.table_) { } next()57 const T *next() { return (T *)iter_.next(); } 58 private: 59 OwnerTableIter<Named, StringC, Hash, NamedTableKeyFunction> iter_; 60 }; 61 62 #ifdef SP_NAMESPACE 63 } 64 #endif 65 66 #endif /* not NamedTable_INCLUDED */ 67