diff options
Diffstat (limited to 'src/mt/stackque.h')
| -rw-r--r-- | src/mt/stackque.h | 64 |
1 files changed, 64 insertions, 0 deletions
diff --git a/src/mt/stackque.h b/src/mt/stackque.h new file mode 100644 index 0000000..349cb4f --- /dev/null +++ b/src/mt/stackque.h | |||
| @@ -0,0 +1,64 @@ | |||
| 1 | /************************************************************************* | ||
| 2 | * | ||
| 3 | * HTCd - Copyright (C) 1998-2006 Henrik Rydberg | ||
| 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 2 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, write to the Free Software | ||
| 17 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA | ||
| 18 | */ | ||
| 19 | |||
| 20 | #ifndef STACKQUEH | ||
| 21 | #define STACKQUEH | ||
| 22 | |||
| 23 | #include <mt/mstring.h> | ||
| 24 | #include <mt/mlock.h> | ||
| 25 | |||
| 26 | template <class T> class stackque { | ||
| 27 | public: | ||
| 28 | stackque(int m) { | ||
| 29 | put=get=n=0; | ||
| 30 | base=new T[caps=m]; | ||
| 31 | memset(&mutex,0,sizeof(mutex_t)); | ||
| 32 | } | ||
| 33 | ~stackque() { delete base; } | ||
| 34 | |||
| 35 | int push(const T& a) throw(merror_t) { | ||
| 36 | MLOCK(mutex); | ||
| 37 | if(n>=caps) THROW("stackque: Full"); | ||
| 38 | base[put++]=a; | ||
| 39 | if(put==caps) put=0; | ||
| 40 | return ++n; | ||
| 41 | } | ||
| 42 | |||
| 43 | int pop(T& a) { | ||
| 44 | MLOCK(mutex); | ||
| 45 | if(n) { | ||
| 46 | a=base[get++]; | ||
| 47 | if(get==caps) get=0; | ||
| 48 | return n--; | ||
| 49 | } | ||
| 50 | else return 0; | ||
| 51 | } | ||
| 52 | |||
| 53 | int capacity() const { return caps; } | ||
| 54 | int size() const { return n; } | ||
| 55 | int empty() const { return n==0; } | ||
| 56 | int full() const { return n==caps; } | ||
| 57 | void clear() { put=get=n=0; } | ||
| 58 | private: | ||
| 59 | int put,get,n,caps; | ||
| 60 | T* base; | ||
| 61 | mutex_t mutex; | ||
| 62 | }; | ||
| 63 | |||
| 64 | #endif | ||
