1 /* 2 * FTGL - OpenGL font library 3 * 4 * Copyright (c) 2001-2004 Henry Maddocks <ftgl@opengl.geek.nz> 5 * 6 * Permission is hereby granted, free of charge, to any person obtaining 7 * a copy of this software and associated documentation files (the 8 * "Software"), to deal in the Software without restriction, including 9 * without limitation the rights to use, copy, modify, merge, publish, 10 * distribute, sublicense, and/or sell copies of the Software, and to 11 * permit persons to whom the Software is furnished to do so, subject to 12 * the following conditions: 13 * 14 * The above copyright notice and this permission notice shall be 15 * included in all copies or substantial portions of the Software. 16 * 17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 18 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 19 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 20 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY 21 * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, 22 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 23 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 24 */ 25 26 #ifndef __FTList__ 27 #define __FTList__ 28 29 #include "FTGL/ftgl.h" 30 31 /** 32 * Provides a non-STL alternative to the STL list 33 */ 34 template <typename FT_LIST_ITEM_TYPE> 35 class FTList 36 { 37 public: 38 typedef FT_LIST_ITEM_TYPE value_type; 39 typedef value_type& reference; 40 typedef const value_type& const_reference; 41 typedef size_t size_type; 42 43 /** 44 * Constructor 45 */ FTList()46 FTList() 47 : listSize(0), 48 tail(0) 49 { 50 tail = NULL; 51 head = new Node; 52 } 53 54 /** 55 * Destructor 56 */ ~FTList()57 ~FTList() 58 { 59 Node* next; 60 61 for(Node *walk = head; walk; walk = next) 62 { 63 next = walk->next; 64 delete walk; 65 } 66 } 67 68 /** 69 * Get the number of items in the list 70 */ size()71 size_type size() const 72 { 73 return listSize; 74 } 75 76 /** 77 * Add an item to the end of the list 78 */ push_back(const value_type & item)79 void push_back(const value_type& item) 80 { 81 Node* node = new Node(item); 82 83 if(head->next == NULL) 84 { 85 head->next = node; 86 } 87 88 if(tail) 89 { 90 tail->next = node; 91 } 92 tail = node; 93 ++listSize; 94 } 95 96 /** 97 * Get the item at the front of the list 98 */ front()99 reference front() const 100 { 101 return head->next->payload; 102 } 103 104 /** 105 * Get the item at the end of the list 106 */ back()107 reference back() const 108 { 109 return tail->payload; 110 } 111 112 private: 113 struct Node 114 { NodeNode115 Node() 116 : next(NULL) 117 {} 118 NodeNode119 Node(const value_type& item) 120 : next(NULL) 121 { 122 payload = item; 123 } 124 125 Node* next; 126 127 value_type payload; 128 }; 129 130 size_type listSize; 131 132 Node* head; 133 Node* tail; 134 }; 135 136 #endif // __FTList__ 137 138