Introduction of BSDArchitecture
[oweals/cde.git] / cde / programs / nsgmls / NamedResourceTable.h
1 /*
2  * CDE - Common Desktop Environment
3  *
4  * Copyright (c) 1993-2012, The Open Group. All rights reserved.
5  *
6  * These libraries and programs are free software; you can
7  * redistribute them and/or modify them under the terms of the GNU
8  * Lesser General Public License as published by the Free Software
9  * Foundation; either version 2 of the License, or (at your option)
10  * any later version.
11  *
12  * These libraries and programs are distributed in the hope that
13  * they will be useful, but WITHOUT ANY WARRANTY; without even the
14  * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15  * PURPOSE. See the GNU Lesser General Public License for more
16  * details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with these librararies and programs; if not, write
20  * to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
21  * Floor, Boston, MA 02110-1301 USA
22  */
23 /* $XConsortium: NamedResourceTable.h /main/1 1996/07/29 16:58:33 cde-hp $ */
24 // Copyright (c) 1994 James Clark
25 // See the file COPYING for copying permission.
26
27 #ifndef NamedResourceTable_INCLUDED
28 #define NamedResourceTable_INCLUDED 1
29
30 #include "NamedResource.h"
31 #include "PointerTable.h"
32 #include "StringC.h"
33 #include "Hash.h"
34 #include "Ptr.h"
35
36 #ifdef SP_NAMESPACE
37 namespace SP_NAMESPACE {
38 #endif
39
40 struct NamedResourceKeyFunction {
41   static inline
42     const StringC &key(const NamedResource &p) {
43       return p.name();
44     }
45 };
46
47 template<class T> class NamedResourceTableIter;
48 template<class T> class ConstNamedResourceTableIter;
49
50 template<class T>
51 class NamedResourceTable {
52 #ifdef __lucid
53   struct X {
54     Ptr<T> _X; // work around lcc bug
55   };
56 #endif
57 public:
58   NamedResourceTable() { }
59   Ptr<T> insert(const Ptr<T> &p, Boolean replace = 0) {
60     return (T *)table_.insert((NamedResource *)p.pointer(), replace).pointer();
61   }
62   Ptr<T> lookup(const StringC &str) const {
63     return (T *)table_.lookup(str).pointer();
64   }
65   ConstPtr<T> lookupConst(const StringC &str) const {
66     return (T *)table_.lookup(str).pointer();
67   }
68   const T *lookupTemp(const StringC &str) const {
69     return (const T *)table_.lookup(str).pointer();
70   }
71   Ptr<T> remove(const StringC &str) {
72     return (T *)table_.remove(str).pointer();
73   }
74   size_t count() const { return table_.count(); }
75   void clear() { table_.clear(); }
76   void swap(NamedResourceTable<T> &to) { table_.swap(to.table_); }
77 private:
78   PointerTable<Ptr<NamedResource>, StringC, Hash,
79                NamedResourceKeyFunction> table_;
80   friend class NamedResourceTableIter<T>;
81   friend class ConstNamedResourceTableIter<T>;
82 };
83
84 template<class T>
85 class NamedResourceTableIter {
86 public:
87   NamedResourceTableIter(const NamedResourceTable<T> &table)
88   : iter_(table.table_) { }
89   Ptr<T> next() {
90     return (T *)iter_.next().pointer();
91   }
92 private:
93   PointerTableIter<Ptr<NamedResource>, StringC, Hash,
94                    NamedResourceKeyFunction> iter_;
95 };
96
97 template<class T>
98 class ConstNamedResourceTableIter {
99 public:
100   ConstNamedResourceTableIter(const NamedResourceTable<T> &table)
101   : iter_(table.table_) { }
102   ConstPtr<T> next() {
103     return (T *)iter_.next().pointer();
104   }
105   const T *nextTemp() {
106     return (const T *)iter_.next().pointer();
107   }
108 private:
109   PointerTableIter<Ptr<NamedResource>, StringC, Hash,
110                    NamedResourceKeyFunction> iter_;
111 };
112   
113 #ifdef SP_NAMESPACE
114 }
115 #endif
116
117 #endif /* not NamedResourceTable_INCLUDED */