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 NamedResourceTable_INCLUDED
6 #define NamedResourceTable_INCLUDED 1
7 
8 #include "NamedResource.h"
9 #include "PointerTable.h"
10 #include "StringC.h"
11 #include "Hash.h"
12 #include "Ptr.h"
13 
14 #ifdef SP_NAMESPACE
15 namespace SP_NAMESPACE {
16 #endif
17 
18 struct NamedResourceKeyFunction {
19   static inline
keyNamedResourceKeyFunction20     const StringC &key(const NamedResource &p) {
21       return p.name();
22     }
23 };
24 
25 template<class T> class NamedResourceTableIter;
26 template<class T> class ConstNamedResourceTableIter;
27 
28 template<class T>
29 class NamedResourceTable {
30 #ifdef __lucid
31   struct X {
32     Ptr<T> _X; // work around lcc bug
33   };
34 #endif
35 public:
NamedResourceTable()36   NamedResourceTable() { }
37   Ptr<T> insert(const Ptr<T> &p, Boolean replace = 0) {
38     return (T *)table_.insert((NamedResource *)p.pointer(), replace).pointer();
39   }
lookup(const StringC & str)40   Ptr<T> lookup(const StringC &str) const {
41     return (T *)table_.lookup(str).pointer();
42   }
lookupConst(const StringC & str)43   ConstPtr<T> lookupConst(const StringC &str) const {
44     return (T *)table_.lookup(str).pointer();
45   }
lookupTemp(const StringC & str)46   const T *lookupTemp(const StringC &str) const {
47     return (const T *)table_.lookup(str).pointer();
48   }
remove(const StringC & str)49   Ptr<T> remove(const StringC &str) {
50     return (T *)table_.remove(str).pointer();
51   }
count()52   size_t count() const { return table_.count(); }
clear()53   void clear() { table_.clear(); }
swap(NamedResourceTable<T> & to)54   void swap(NamedResourceTable<T> &to) { table_.swap(to.table_); }
55 private:
56   PointerTable<Ptr<NamedResource>, StringC, Hash,
57 	       NamedResourceKeyFunction> table_;
58   friend class NamedResourceTableIter<T>;
59   friend class ConstNamedResourceTableIter<T>;
60 };
61 
62 template<class T>
63 class NamedResourceTableIter {
64 public:
NamedResourceTableIter(const NamedResourceTable<T> & table)65   NamedResourceTableIter(const NamedResourceTable<T> &table)
66   : iter_(table.table_) { }
next()67   Ptr<T> next() {
68     return (T *)iter_.next().pointer();
69   }
70 private:
71   PointerTableIter<Ptr<NamedResource>, StringC, Hash,
72                    NamedResourceKeyFunction> iter_;
73 };
74 
75 template<class T>
76 class ConstNamedResourceTableIter {
77 public:
ConstNamedResourceTableIter(const NamedResourceTable<T> & table)78   ConstNamedResourceTableIter(const NamedResourceTable<T> &table)
79   : iter_(table.table_) { }
next()80   ConstPtr<T> next() {
81     return (T *)iter_.next().pointer();
82   }
nextTemp()83   const T *nextTemp() {
84     return (const T *)iter_.next().pointer();
85   }
86 private:
87   PointerTableIter<Ptr<NamedResource>, StringC, Hash,
88                    NamedResourceKeyFunction> iter_;
89 };
90 
91 #ifdef SP_NAMESPACE
92 }
93 #endif
94 
95 #endif /* not NamedResourceTable_INCLUDED */
96