Merge branch 'cde-fixups-1' of ssh://git.code.sf.net/p/cdesktopenv/code into cde...
[oweals/cde.git] / cde / programs / nsgmls / NamedTable.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 libraries 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: NamedTable.h /main/2 1996/08/13 10:09:04 mgreess $ */
24 // Copyright (c) 1994 James Clark
25 // See the file COPYING for copying permission.
26
27 #ifndef NamedTable_INCLUDED
28 #define NamedTable_INCLUDED 1
29
30 #include "Hash.h"
31 #include "StringC.h"
32 #include "Named.h"
33 #include "OwnerTable.h"
34
35 #ifdef SP_NAMESPACE
36 namespace SP_NAMESPACE {
37 #endif
38
39 class NamedTableKeyFunction {
40 public:
41   static inline const StringC &key(const Named &obj) { return obj.name(); }
42 };
43
44 template<class T> class NamedTableIter;
45 template<class T> class ConstNamedTableIter;
46
47 template<class T>
48 class NamedTable {
49 public:
50   NamedTable() { }
51   T *insert(T *p) { return (T *)table_.insert(p); }
52   T *lookup(const StringC &str) const { return (T *)table_.lookup(str); }
53   T *remove(const StringC &str) { return (T *)table_.remove(str); }
54   size_t count() const { return table_.count(); }
55   void clear() { table_.clear(); }
56   void swap(NamedTable<T> &to) { table_.swap(to.table_); }
57 private:
58   NamedTable(const NamedTable<T> &) {}
59   void operator=(const NamedTable<T> &) {}
60   OwnerTable<Named, StringC, Hash, NamedTableKeyFunction>
61     table_;
62   friend class NamedTableIter<T>;
63   friend class ConstNamedTableIter<T>;
64 };
65
66 template<class T>
67 class NamedTableIter {
68 public:
69   NamedTableIter(const NamedTable<T> &table) : iter_(table.table_) { }
70   T *next() { return (T *)iter_.next(); }
71 private:
72   OwnerTableIter<Named, StringC, Hash, NamedTableKeyFunction> iter_;
73 };
74
75 template<class T>
76 class ConstNamedTableIter {
77 public:
78   ConstNamedTableIter(const NamedTable<T> &table) : iter_(table.table_) { }
79   const T *next() { return (T *)iter_.next(); }
80 private:
81   OwnerTableIter<Named, StringC, Hash, NamedTableKeyFunction> iter_;
82 };
83
84 #ifdef SP_NAMESPACE
85 }
86 #endif
87
88 #endif /* not NamedTable_INCLUDED */