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