| 1 | /* |
| 2 | * Descent 3 |
| 3 | * Copyright (C) 2024 Parallax Software |
| 4 | * |
| 5 | * This program is free software: you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License as published by |
| 7 | * the Free Software Foundation, either version 3 of the License, or |
| 8 | * (at your option) any later version. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License |
| 16 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 17 | |
| 18 | --- HISTORICAL COMMENTS FOLLOW --- |
| 19 | |
| 20 | * $Logfile: /DescentIII/Main/lib/psclass.h $ |
| 21 | * $Revision: 8 $ |
| 22 | * $Date: 4/16/99 4:26a $ |
| 23 | * $Author: Jeff $ |
| 24 | * |
| 25 | * New Class Library |
| 26 | * |
| 27 | * $Log: /DescentIII/Main/lib/psclass.h $ |
| 28 | * |
| 29 | * 8 4/16/99 4:26a Jeff |
| 30 | * removed ifdef of linux since new compiler compiles it |
| 31 | * |
| 32 | * 7 4/15/99 1:44a Jeff |
| 33 | * changes for linux compile |
| 34 | * |
| 35 | * 6 12/16/98 1:57p Samir |
| 36 | * Replaced CleanupString2 with CleanupStr |
| 37 | * |
| 38 | * 5 10/02/98 11:16a Jeff |
| 39 | * removed 'struct' from the template, generates an error in VC6 that |
| 40 | * keeps it from compiling |
| 41 | * |
| 42 | * 4 9/17/98 7:05p Samir |
| 43 | * added string token class. |
| 44 | * |
| 45 | * 3 7/28/98 5:42p Samir |
| 46 | * added queue class. |
| 47 | * |
| 48 | * 2 7/23/98 11:36a Samir |
| 49 | * Added class library header. |
| 50 | * |
| 51 | * $NoKeywords: $ |
| 52 | */ |
| 53 | |
| 54 | #ifndef PSCLASS_H |
| 55 | #define PSCLASS_Hcxx . . $ `lib/psclass.h:55:9`! |
| 56 | |
| 57 | // a universal list node to use with the list type |
| 58 | template <class T> struct tListNodecxx . . $ tListNode# { |
| 59 | T tcxx . . $ tListNode#t.; |
| 60 | tListNodecxx . . $ tListNode#<T> *nextcxx . . $ tListNode#next.; |
| 61 | }; |
| 62 | |
| 63 | // LIST CLASSES |
| 64 | // |
| 65 | |
| 66 | // tList |
| 67 | // this is a VERY BASIC list class that encapsulates a lot |
| 68 | // of list operations into one class. this class handles list NODES ONLY. |
| 69 | // a more advanced form is to come, but this is for those hardcode programmers. |
| 70 | |
| 71 | // to declare say, a list that will contain integers, do this: |
| 72 | // tList<int> list; |
| 73 | // tListNode<int> *node; |
| 74 | // node = new tListNode<int>; |
| 75 | // node->data = 1; |
| 76 | // list.Link(node); |
| 77 | // it is the programmers responsibility to create the nodes DYNAMICALLY |
| 78 | |
| 79 | // universal single-linked-list type |
| 80 | template <class T> class tListcxx . . $ tList# { |
| 81 | tListNodecxx . . $ tListNode#<T> *m_linkcxx . . $ tList#m_link., *m_markcxx . . $ tList#m_mark.; |
| 82 | int m_lengthcxx . . $ tList#m_length.; |
| 83 | |
| 84 | public: |
| 85 | tListcxx . . $ tList#`tList<T>`(49f6e7a06ebc5aa8).() { |
| 86 | m_linkcxx . . $ tList#m_link. = m_markcxx . . $ tList#m_mark. = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:416:9`!; |
| 87 | m_lengthcxx . . $ tList#m_length. = 0; |
| 88 | }; |
| 89 | ~cxx . . $ tList#`~tList<T>`(49f6e7a06ebc5aa8).tList() { tListcxx . . $ tList#::freecxx . . $ tList#free(49f6e7a06ebc5aa8).(); }; |
| 90 | |
| 91 | // moves to start of list. used for iteration |
| 92 | tListNodecxx . . $ tListNode#<T> *startcxx . . $ tList#start(fe6b4b23a955992d).() { |
| 93 | m_markcxx . . $ tList#m_mark. = m_linkcxx . . $ tList#m_link.; |
| 94 | return m_markcxx . . $ tList#m_mark.; |
| 95 | } |
| 96 | |
| 97 | // gets next list item in iteration. if at end of list, last item will be returned. |
| 98 | tListNodecxx . . $ tListNode#<T> *nextcxx . . $ tList#next(fe6b4b23a955992d).() { |
| 99 | m_markcxx . . $ tList#m_mark. = (m_markcxx . . $ tList#m_mark.->nextcxx . . $ tListNode#next.) ? m_markcxx . . $ tList#m_mark.->nextcxx . . $ tListNode#next. : m_markcxx . . $ tList#m_mark.; |
| 100 | return getcxx . . $ tList#get(95d857469b62da74).(); |
| 101 | }; |
| 102 | |
| 103 | // returns the node at the current link location in iteration. |
| 104 | tListNodecxx . . $ tListNode#<T> *getcxx . . $ tList#get(95d857469b62da74).() const { return m_markcxx . . $ tList#m_mark. ? m_markcxx . . $ tList#m_mark. : NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:416:9`!; }; |
| 105 | |
| 106 | // length |
| 107 | int lengthcxx . . $ tList#length(455f465bc33b4cdf).() const { return m_lengthcxx . . $ tList#m_length.; }; |
| 108 | |
| 109 | // frees list |
| 110 | void freecxx . . $ tList#free(49f6e7a06ebc5aa8).() { |
| 111 | m_linkcxx . . $ tList#m_link. = m_markcxx . . $ tList#m_mark. = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:416:9`!; |
| 112 | m_lengthcxx . . $ tList#m_length. = 0; |
| 113 | }; |
| 114 | |
| 115 | // make a link at the current mark location. |
| 116 | void linkcxx . . $ tList#link(fb0c6bc9adb09be2).(tListNodecxx . . $ tListNode#<T> *node) { |
| 117 | if (m_linkcxx . . $ tList#m_link.) { |
| 118 | node->nextcxx . . $ tListNode#next. = m_markcxx . . $ tList#m_mark.->nextcxx . . $ tListNode#next.; |
| 119 | m_markcxx . . $ tList#m_mark.->nextcxx . . $ tListNode#next. = node; |
| 120 | } else { |
| 121 | m_linkcxx . . $ tList#m_link. = node; |
| 122 | node->nextcxx . . $ tListNode#next. = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:416:9`!; |
| 123 | } |
| 124 | m_markcxx . . $ tList#m_mark. = node; |
| 125 | m_lengthcxx . . $ tList#m_length.++; |
| 126 | }; |
| 127 | |
| 128 | // unlink a node on the list at the current mark, returning the unlinked node. |
| 129 | tListNodecxx . . $ tListNode#<T> *unlinkcxx . . $ tList#unlink(fe6b4b23a955992d).() { |
| 130 | tListNodecxx . . $ tListNode#<T> *freenode, *node; |
| 131 | if (!m_linkcxx . . $ tList#m_link.) |
| 132 | return NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:416:9`!; |
| 133 | if (m_linkcxx . . $ tList#m_link. == m_markcxx . . $ tList#m_mark.) { |
| 134 | freenode = m_markcxx . . $ tList#m_mark.; |
| 135 | m_markcxx . . $ tList#m_mark. = m_linkcxx . . $ tList#m_link. = m_linkcxx . . $ tList#m_link.->nextcxx . . $ tListNode#next.; |
| 136 | } else { |
| 137 | node = m_linkcxx . . $ tList#m_link.; |
| 138 | while (node->nextcxx . . $ tListNode#next. != m_markcxx . . $ tList#m_mark.) |
| 139 | node = node->nextcxx . . $ tListNode#next.; |
| 140 | freenode = m_markcxx . . $ tList#m_mark.; |
| 141 | node->nextcxx . . $ tListNode#next. = m_markcxx . . $ tList#m_mark.->nextcxx . . $ tListNode#next.; |
| 142 | } |
| 143 | freenode->nextcxx . . $ tListNode#next. = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:416:9`!; |
| 144 | return freenode; |
| 145 | }; |
| 146 | }; |
| 147 | |
| 148 | // tQueue |
| 149 | // a circular queue implementation |
| 150 | |
| 151 | template <class T, int t_LEN> class tQueuecxx . . $ tQueue# { |
| 152 | T m_itemscxx . . $ tQueue#m_items.[t_LEN]; |
| 153 | int16_t m_headcxx . . $ tQueue#m_head., m_tailcxx . . $ tQueue#m_tail.; |
| 154 | |
| 155 | public: |
| 156 | tQueuecxx . . $ tQueue#`tQueue<T, t_LEN>`(49f6e7a06ebc5aa8).() { m_headcxx . . $ tQueue#m_head. = m_tailcxx . . $ tQueue#m_tail. = 0; }; |
| 157 | ~cxx . . $ tQueue#`~tQueue<T, t_LEN>`(49f6e7a06ebc5aa8).tQueue(){}; |
| 158 | |
| 159 | void sendcxx . . $ tQueue#send(8e935d9c2e576e3a).(T &item) { // sends an item onto the queue |
| 160 | int16_t temp = m_tailcxx . . $ tQueue#m_tail. + 1; |
| 161 | if (temp == t_LEN) |
| 162 | temp = 0; |
| 163 | if (temp != m_headcxx . . $ tQueue#m_head.) { |
| 164 | m_itemscxx . . $ tQueue#m_items.[m_tailcxx . . $ tQueue#m_tail.] = item; |
| 165 | m_tailcxx . . $ tQueue#m_tail. = temp; |
| 166 | } |
| 167 | }; |
| 168 | bool recvcxx . . $ tQueue#recv(4a2ab88644057ef5).(T *item) { // returns an item from the queue, false if no item. |
| 169 | if (m_headcxx . . $ tQueue#m_head. == m_tailcxx . . $ tQueue#m_tail.) |
| 170 | return false; |
| 171 | *item = m_itemscxx . . $ tQueue#m_items.[m_headcxx . . $ tQueue#m_head.++]; |
| 172 | if (m_headcxx . . $ tQueue#m_head. == t_LEN) |
| 173 | m_headcxx . . $ tQueue#m_head. = 0; |
| 174 | return true; |
| 175 | }; |
| 176 | |
| 177 | void flushcxx . . $ tQueue#flush(49f6e7a06ebc5aa8).() { // flush queue entries. |
| 178 | m_headcxx . . $ tQueue#m_head. = m_tailcxx . . $ tQueue#m_tail. = 0; |
| 179 | }; |
| 180 | }; |
| 181 | |
| 182 | #endif |