CC_Slist.h 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  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: CC_Slist.h /main/6 1996/08/21 15:48:46 drk $ */
  24. #ifndef __CC_Slist_h
  25. #define __CC_Slist_h
  26. #include "CC_Listbase.h"
  27. #include "cc_exceptions.h"
  28. template <class T> class CC_TPtrSlist;
  29. template <class T> class CC_TPtrSlistIterator;
  30. template <class T> class CC_TPtrDlist;
  31. template <class T> class CC_TPtrDlistIterator;
  32. template <class T> class CC_TValSlist;
  33. template <class T> class CC_TValSlistIterator;
  34. template <class T> class Stack;
  35. template <class T>
  36. class CC_Link : private CC_Link_base
  37. {
  38. friend class CC_TPtrSlist<T>;
  39. friend class CC_TPtrSlistIterator<T>;
  40. friend class CC_TPtrDlist<T>;
  41. friend class CC_TPtrDlistIterator<T>;
  42. friend class CC_TValSlist<T>;
  43. friend class CC_TValSlistIterator<T>;
  44. friend class Stack<T>;
  45. private:
  46. CC_Link (T *element)
  47. : f_element (element)
  48. { }
  49. T* f_element;
  50. };
  51. template <class T> class CC_List_Iterator;
  52. template <class T>
  53. class CC_TPtrSlist : public CC_Listbase
  54. {
  55. //template <class T> friend class CC_List_Iterator;
  56. friend class CC_List_Iterator<T>;
  57. protected:
  58. CC_Boolean destructed;
  59. // Inherit public members from CC_Listbase
  60. /*
  61. * insert
  62. * append
  63. * prepend
  64. * entries
  65. * first, last
  66. * removeLast, removeFirst
  67. */
  68. public:
  69. CC_TPtrSlist(const CC_TPtrSlist<T> &);
  70. CC_TPtrSlist() { destructed = FALSE; }
  71. virtual ~CC_TPtrSlist();
  72. virtual void clearAndDestroy();
  73. virtual void clear(); /* clear only removes item, but not calling
  74. * individual item's destructor
  75. */
  76. void prepend(T* element)
  77. { CC_Listbase::prepend (new CC_Link<T> (element)); }
  78. void append(T* element)
  79. { CC_Listbase::append (new CC_Link<T> (element)); }
  80. void insert(T* element)
  81. { CC_Listbase::append (new CC_Link<T> (element)); }
  82. T* at(size_t pos) const /* throw boundaryException
  83. * if list size is smaller than pos
  84. */
  85. {
  86. // Hack to get it passed to iter
  87. CC_TPtrSlistIterator<T> iter( *(CC_TPtrSlist<T> *)this );
  88. for ( size_t i = 0; i <=pos; i++ ) {
  89. if ( !(++iter) ) {
  90. throw(CASTCCBEXCEPT ccBoundaryException(0,0,i));
  91. }
  92. }
  93. return( iter.key() );
  94. }
  95. T* removeAt(size_t pos); /* throw boundaryException
  96. * if list size is smaller than pos
  97. */
  98. T* removeLast() {
  99. CC_Link<T> *t = (CC_Link<T> *)(CC_Listbase::removeLast());
  100. if ( t ) {
  101. T * ret = t->f_element;
  102. delete t;
  103. return(ret);
  104. }
  105. else return(NULL);
  106. }
  107. T* removeFirst() {
  108. CC_Link<T> *t = (CC_Link<T> *)(CC_Listbase::removeFirst());
  109. if ( t ) {
  110. T *ret = t->f_element;
  111. delete t;
  112. return (ret);
  113. }
  114. else return(NULL);
  115. }
  116. T* first() const
  117. {
  118. CC_Link<T> *t = (CC_Link<T> *)(CC_Listbase::first());
  119. if (t) { return( t->f_element ); }
  120. else return(NULL);
  121. }
  122. T* last() const
  123. {
  124. CC_Link<T> *t = (CC_Link<T> *)(CC_Listbase::last());
  125. if (t) { return( t->f_element ); }
  126. else return(NULL);
  127. }
  128. T* find(const T*) const;
  129. T* find(CC_Boolean (*)(T*, void*), void*) const;
  130. CC_Boolean contains(const T*) const;
  131. T* remove(const T*);
  132. operator CC_Listbase *() { return(this); }
  133. CC_Boolean get_destructed() const
  134. { return (destructed); }
  135. void set_destructed(CC_Boolean what)
  136. { destructed = what; }
  137. };
  138. template <class T>
  139. class CC_TPtrSlistIterator : public CC_List_Iterator_base
  140. {
  141. friend class CC_TPtrSlist<T>;
  142. /*
  143. Inherit all the public/protected member from CC_List_Iterator_base
  144. reset;
  145. operator++
  146. */
  147. public:
  148. CC_TPtrSlistIterator (CC_TPtrSlist<T> &list)
  149. : CC_List_Iterator_base ( (CC_Listbase *)&list)
  150. { }
  151. T* key() const
  152. {
  153. CC_Link<T> *link_item = (CC_Link<T> *) CC_List_Iterator_base::item();
  154. if ( link_item ) {
  155. return ( link_item->f_element );
  156. }
  157. else {
  158. return(NULL);
  159. }
  160. }
  161. T *operator()()
  162. {
  163. if ( ++(*this) ) { return( key() ); }
  164. else { return(NULL); }
  165. }
  166. };
  167. template <class T>
  168. class CC_TValSlist : public CC_Listbase
  169. {
  170. // inherit entries from CC_Listbase
  171. public:
  172. CC_TValSlist(const CC_TValSlist<T>&);
  173. CC_TValSlist() {}
  174. ~CC_TValSlist();
  175. void append( const T &t) { /* copies the content of t, also
  176. * assumes the copy constructor for type T
  177. * exists
  178. */
  179. T *new_element = new T( t );
  180. CC_Listbase::append(new CC_Link<T>((T *)new_element));
  181. }
  182. };
  183. template <class T>
  184. class CC_TValSlistIterator:public CC_List_Iterator_base
  185. {
  186. /* inherit public member from CC_List_Iterator_base
  187. * Boolean operator++()
  188. */
  189. public:
  190. CC_TValSlistIterator (CC_TValSlist<T> &list)
  191. : CC_List_Iterator_base ( (CC_Listbase *)&list)
  192. {}
  193. T key() const; // Throw ccException if link is undefined
  194. };
  195. #ifdef EXPAND_TEMPLATES
  196. #include "CC_Slist.C"
  197. #endif
  198. #endif /* __CC_Slist_h */
  199. /* DO NOT ADD ANY LINES AFTER THIS #endif */