85c19e6c54dbaebda6a2d82c43d7000330c2f041
[libcds.git] / tests / unit / pqueue / std_pqueue.h
1 //$$CDS-header$$
2
3 #ifndef __CDSUNIT_STD_PQUEUE_H
4 #define __CDSUNIT_STD_PQUEUE_H
5
6 #include <queue>
7
8 namespace pqueue {
9
10     struct dummy_stat {};
11
12     template <typename T, typename Container, typename Lock, typename Less=std::less<typename Container::value_type> >
13     class StdPQueue
14     {
15         typedef T value_type;
16         typedef std::priority_queue<value_type, Container, Less> pqueue_type;
17
18         pqueue_type     m_PQueue;
19         mutable Lock    m_Lock;
20
21         struct scoped_lock
22         {
23             Lock&   m_lock;
24
25             scoped_lock( Lock& l )
26                 : m_lock(l)
27             {
28                 l.lock();
29             }
30
31             ~scoped_lock()
32             {
33                 m_lock.unlock();
34             }
35         };
36
37     public:
38         bool push( value_type const& val )
39         {
40             scoped_lock l( m_Lock );
41             m_PQueue.push( val );
42             return true;
43         }
44
45         bool pop( value_type& dest )
46         {
47             scoped_lock l( m_Lock );
48             if ( !m_PQueue.empty() ) {
49                 dest = m_PQueue.top();
50                 m_PQueue.pop();
51                 return true;
52             }
53             return false;
54         }
55
56         template <typename Q, typename MoveFunc>
57         bool pop_with( Q& dest, MoveFunc f )
58         {
59             scoped_lock l( m_Lock );
60             if ( !m_PQueue.empty() ) {
61                 f( dest, m_PQueue.top());
62                 m_PQueue.pop();
63                 return true;
64             }
65             return false;
66         }
67
68         void clear()
69         {
70             scoped_lock l( m_Lock );
71             while ( !m_PQueue.empty() )
72                 m_PQueue.pop();
73         }
74
75         template <typename Func>
76         void clear_with( Func f )
77         {
78             scoped_lock l( m_Lock );
79             while ( !m_PQueue.empty() ) {
80                 f( m_PQueue.top() );
81                 m_PQueue.pop();
82             }
83         }
84
85         bool empty() const
86         {
87             return m_PQueue.empty();
88         }
89
90         size_t size() const
91         {
92             return m_PQueue.size();
93         }
94
95         dummy_stat statistics() const
96         {
97             return dummy_stat();
98         }
99     };
100
101 } // namespace pqueue
102
103 namespace std {
104     static inline ostream& operator <<( ostream& o, pqueue::dummy_stat )
105     {
106         return o;
107     }
108 }
109
110 #endif // #ifndef __CDSUNIT_STD_PQUEUE_H