Team Fortress 2 Source Code as on 22/4/2020
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

184 lines
4.1 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. // $NoKeywords: $
  6. //
  7. //=============================================================================//
  8. /*
  9. *
  10. * Copyright (c) 1998-9
  11. * Dr John Maddock
  12. *
  13. * Permission to use, copy, modify, distribute and sell this software
  14. * and its documentation for any purpose is hereby granted without fee,
  15. * provided that the above copyright notice appear in all copies and
  16. * that both that copyright notice and this permission notice appear
  17. * in supporting documentation. Dr John Maddock makes no representations
  18. * about the suitability of this software for any purpose.
  19. * It is provided "as is" without express or implied warranty.
  20. *
  21. */
  22. /*
  23. * FILE re_lst.h
  24. * VERSION 2.12
  25. * This is an internal header file, do not include directly.
  26. * re_list support class, for regular
  27. * expression library.
  28. */
  29. #ifndef RE_LST_H
  30. #define RE_LST_H
  31. #ifndef JM_CFG_H
  32. #include <jm/jm_cfg.h>
  33. #endif
  34. #include <new.h>
  35. JM_NAMESPACE(__JM)
  36. template <class T, class Allocator>
  37. class re_list
  38. {
  39. public:
  40. struct node
  41. {
  42. node* next;
  43. T t;
  44. node(const T& o) : t(o) {}
  45. };
  46. public:
  47. class iterator
  48. {
  49. node* pos;
  50. public:
  51. iterator() { pos = 0; }
  52. ~iterator() {}
  53. iterator(const iterator& i) { pos = i.pos; }
  54. iterator(node* n) { pos = n; }
  55. iterator& operator=(const iterator& i)
  56. {
  57. pos = i.pos;
  58. return *this;
  59. }
  60. bool operator==(iterator& i)
  61. {
  62. return pos == i.pos;
  63. }
  64. bool operator!=(iterator& i)
  65. {
  66. return pos != i.pos;
  67. }
  68. T& operator*() { return pos->t; }
  69. iterator& operator++()
  70. {
  71. pos = pos->next;
  72. return *this;
  73. }
  74. iterator operator++(int)
  75. {
  76. iterator t(*this);
  77. pos = pos->next;
  78. return t;
  79. }
  80. const node* tell()const
  81. {
  82. return pos;
  83. }
  84. };
  85. class const_iterator
  86. {
  87. const node* pos;
  88. public:
  89. const_iterator() { pos = 0; }
  90. ~const_iterator() {}
  91. const_iterator(const const_iterator& i) { pos = i.pos; }
  92. const_iterator(const iterator& i) { pos = i.tell(); }
  93. const_iterator(const node* n) { pos = n; }
  94. const_iterator& operator=(const iterator& i)
  95. {
  96. pos = i.tell();
  97. return *this;
  98. }
  99. const_iterator& operator=(const const_iterator& i)
  100. {
  101. pos = i.pos;
  102. return *this;
  103. }
  104. bool operator==(const_iterator& i)
  105. {
  106. return pos == i.pos;
  107. }
  108. bool operator!=(const_iterator& i)
  109. {
  110. return pos != i.pos;
  111. }
  112. const T& operator*() { return pos->t; }
  113. const_iterator& operator++()
  114. {
  115. pos = pos->next;
  116. return *this;
  117. }
  118. const_iterator operator++(int)
  119. {
  120. const_iterator t(*this);
  121. pos = pos->next;
  122. return t;
  123. }
  124. };
  125. private:
  126. typedef JM_MAYBE_TYPENAME REBIND_TYPE(node, Allocator) node_alloc;
  127. struct data : public node_alloc
  128. {
  129. node* first;
  130. data(const Allocator& a) : node_alloc(a), first(0) {}
  131. };
  132. data alloc_inst;
  133. public:
  134. re_list(const Allocator& a = Allocator()) : alloc_inst(a) {}
  135. ~re_list() { clear(); }
  136. iterator RE_CALL begin() { return iterator(alloc_inst.first); }
  137. iterator RE_CALL end() { return iterator(0); }
  138. const_iterator RE_CALL begin()const { return const_iterator(alloc_inst.first); }
  139. const_iterator RE_CALL end()const { return const_iterator(0); }
  140. void RE_CALL add(const T& t)
  141. {
  142. node* temp;
  143. temp = alloc_inst.allocate(1);
  144. #ifndef JM_NO_EXCEPTIONS
  145. try{
  146. #endif
  147. alloc_inst.construct(temp, t);
  148. #ifndef JM_NO_EXCEPTIONS
  149. }catch(...){ alloc_inst.deallocate(temp, 1); throw; }
  150. #endif
  151. temp->next = alloc_inst.first;
  152. alloc_inst.first = temp;
  153. }
  154. void RE_CALL clear();
  155. };
  156. template <class T, class Allocator>
  157. void RE_CALL re_list<T, Allocator>::clear()
  158. {
  159. node* temp;
  160. while(alloc_inst.first)
  161. {
  162. temp = alloc_inst.first;
  163. alloc_inst.first = alloc_inst.first->next;
  164. alloc_inst.destroy(temp);
  165. alloc_inst.deallocate(temp, 1);
  166. }
  167. }
  168. JM_END_NAMESPACE
  169. #endif