[7833] Implement levelup spells for non-hunter pets.
[getmangos.git] / src / shared / LockedQueue.h
blobb085dd09b830861ff79d740344d4ffc761f9a8d4
1 /*
2 * Copyright (C) 2009 MaNGOS <http://getmangos.com/>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 #ifndef LOCKEDQUEUE_H
20 #define LOCKEDQUEUE_H
22 #include <ace/Guard_T.h>
23 #include <ace/Thread_Mutex.h>
24 #include <deque>
25 #include <assert.h>
26 #include "Errors.h"
28 namespace ACE_Based
31 template <class T, class LockType, typename StorageType=std::deque<T> >
32 class LockedQueue
35 //! Serialize access to the Queue
36 LockType _lock;
38 //! Storage backing the queue
39 StorageType _queue;
41 //! Cancellation flag
42 volatile bool _canceled;
44 public:
46 //! Create a LockedQueue
47 LockedQueue() : _canceled(false) {}
49 //! Destroy a LockedQueue
50 virtual ~LockedQueue() { }
52 /**
53 * @see Queue::add(const T& item)
55 void add(const T& item)
58 ACE_Guard<LockType> g(_lock);
60 ASSERT(!_canceled);
61 // throw Cancellation_Exception();
63 _queue.push_back(item);
67 /**
68 * @see Queue::next()
70 T next()
73 ACE_Guard<LockType> g(_lock);
75 ASSERT (!_queue.empty() || !_canceled);
76 // throw Cancellation_Exception();
78 T item = _queue.front();
79 _queue.pop_front();
81 return item;
85 T front()
87 ACE_Guard<LockType> g(_lock);
89 ASSERT (!_queue.empty());
90 // throw NoSuchElement_Exception();
92 return _queue.front();
95 /**
96 * @see Queue::cancel()
98 void cancel()
101 ACE_Guard<LockType> g(_lock);
103 _canceled = true;
108 * @see Queue::isCanceled()
110 bool isCanceled()
113 // Faster check since the queue will not become un-canceled
114 if(_canceled)
115 return true;
117 ACE_Guard<LockType> g(_lock);
119 return _canceled;
124 * @see Queue::size()
126 size_t size()
129 ACE_Guard<LockType> g(_lock);
130 return _queue.size();
134 bool empty()
137 ACE_Guard<LockType> g(_lock);
138 return _queue.empty();
144 #endif