desktop: CalEditDlg: fixed dialog title bar
[barry.git] / src / dataqueue.h
blob7489fc1db9cdda8d91a5646180031bfaeca7a154
1 ///
2 /// \file dataqueue.h
3 /// FIFO queue of Data objects
4 ///
6 /*
7 Copyright (C) 2007-2012, Net Direct Inc. (http://www.netdirect.ca/)
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
18 See the GNU General Public License in the COPYING file at the
19 root directory of this project for more details.
22 #ifndef __BARRY_DATAQUEUE_H__
23 #define __BARRY_DATAQUEUE_H__
25 #include <list>
26 #include <pthread.h>
27 #include <iosfwd>
29 namespace Barry {
31 class Data;
34 // DataQueue class
36 /// This class provides a thread aware fifo queue for Data objects,
37 /// providing memory management for all Data object pointers it contains.
38 ///
39 /// It uses similar member names as std::queue<>, for consistency.
40 ///
41 class DataQueue
43 // always use the raw_push() and raw_pop() functions
44 typedef std::list<Data*> queue_type;
46 pthread_mutex_t m_waitMutex;
47 pthread_cond_t m_waitCond;
49 mutable pthread_mutex_t m_accessMutex; // locked for each access of m_queue
51 queue_type m_queue;
53 protected:
54 void raw_push(Data *data);
55 Data* raw_pop();
57 public:
58 DataQueue();
59 ~DataQueue(); // frees all data in the queue
61 // Pushes data into the end of the queue.
62 // The queue owns this pointer as soon as the function is
63 // called. In the case of an exception, it will be freed.
64 // Performs a thread broadcast once new data has been added.
65 void push(Data *data);
67 // Pops the next element off the front of the queue.
68 // Returns 0 if empty.
69 // The queue no longer owns this pointer upon return.
70 Data* pop();
72 // Pops the next element off the front of the queue, and
73 // waits until one exists if empty. If still no data
74 // on timeout, returns null.
75 // Timeout specified in milliseconds. Default is wait forever.
76 Data* wait_pop(int timeout = -1);
78 // Pops all data from other and appends it to this.
79 // After calling this function, other will be empty, and
80 // this will contain all its data.
81 // In the case of an exception, any uncopied data will
82 // remain in other.
83 void append_from(DataQueue &other);
85 bool empty() const; // return true if empty
86 size_t size() const;
88 void DumpAll(std::ostream &os) const;
91 inline std::ostream& operator<< (std::ostream &os, const DataQueue &dq)
93 dq.DumpAll(os);
94 return os;
97 } // namespace Barry
99 #endif