NamedTable.h 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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. #ifndef NamedTable_INCLUDED
  27. #define NamedTable_INCLUDED 1
  28. #include "Hash.h"
  29. #include "StringC.h"
  30. #include "Named.h"
  31. #include "OwnerTable.h"
  32. #ifdef SP_NAMESPACE
  33. namespace SP_NAMESPACE {
  34. #endif
  35. class NamedTableKeyFunction {
  36. public:
  37. static inline const StringC &key(const Named &obj) { return obj.name(); }
  38. };
  39. template<class T> class NamedTableIter;
  40. template<class T> class ConstNamedTableIter;
  41. template<class T>
  42. class NamedTable {
  43. public:
  44. NamedTable() { }
  45. T *insert(T *p) { return (T *)table_.insert(p); }
  46. T *lookup(const StringC &str) const { return (T *)table_.lookup(str); }
  47. T *remove(const StringC &str) { return (T *)table_.remove(str); }
  48. size_t count() const { return table_.count(); }
  49. void clear() { table_.clear(); }
  50. void swap(NamedTable<T> &to) { table_.swap(to.table_); }
  51. private:
  52. NamedTable(const NamedTable<T> &) {}
  53. void operator=(const NamedTable<T> &) {}
  54. OwnerTable<Named, StringC, Hash, NamedTableKeyFunction>
  55. table_;
  56. friend class NamedTableIter<T>;
  57. friend class ConstNamedTableIter<T>;
  58. };
  59. template<class T>
  60. class NamedTableIter {
  61. public:
  62. NamedTableIter(const NamedTable<T> &table) : iter_(table.table_) { }
  63. T *next() { return (T *)iter_.next(); }
  64. private:
  65. OwnerTableIter<Named, StringC, Hash, NamedTableKeyFunction> iter_;
  66. };
  67. template<class T>
  68. class ConstNamedTableIter {
  69. public:
  70. ConstNamedTableIter(const NamedTable<T> &table) : iter_(table.table_) { }
  71. const T *next() { return (T *)iter_.next(); }
  72. private:
  73. OwnerTableIter<Named, StringC, Hash, NamedTableKeyFunction> iter_;
  74. };
  75. #ifdef SP_NAMESPACE
  76. }
  77. #endif
  78. #endif /* not NamedTable_INCLUDED */