File size: 2,383 Bytes
7fc5a59
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#ifndef OPENPOSE_THREAD_PRIORITY_QUEUE_HPP
#define OPENPOSE_THREAD_PRIORITY_QUEUE_HPP

#include <queue> // std::priority_queue
#include <openpose/core/common.hpp>
#include <openpose/thread/queueBase.hpp>

namespace op
{
    template<typename TDatums, typename TQueue = std::priority_queue<TDatums, std::vector<TDatums>, std::greater<TDatums>>>
    class PriorityQueue : public QueueBase<TDatums, TQueue>
    {
    public:
        explicit PriorityQueue(const long long maxSize = 256);

        virtual ~PriorityQueue();

        TDatums front() const;

    private:
        bool pop(TDatums& tDatums);

        DELETE_COPY(PriorityQueue);
    };
}





// Implementation
#include <type_traits> // std::is_same
namespace op
{
    template<typename TDatums, typename TQueue>
    PriorityQueue<TDatums, TQueue>::PriorityQueue(const long long maxSize) :
        QueueBase<TDatums, TQueue>{maxSize}
    {
        // Check TDatums = underlying value type of TQueue
        typedef typename TQueue::value_type underlyingValueType;
        static_assert(std::is_same<TDatums, underlyingValueType>::value,
                      "Error: The type of the queue must be the same as the type of the container");
    }

    template<typename TDatums, typename TQueue>
    PriorityQueue<TDatums, TQueue>::~PriorityQueue()
    {
    }

    template<typename TDatums, typename TQueue>
    TDatums PriorityQueue<TDatums, TQueue>::front() const
    {
        try
        {
            const std::lock_guard<std::mutex> lock{this->mMutex};
            return this->mTQueue.top();
        }
        catch (const std::exception& e)
        {
            error(e.what(), __LINE__, __FUNCTION__, __FILE__);
            return TDatums{};
        }
    }

    template<typename TDatums, typename TQueue>
    bool PriorityQueue<TDatums, TQueue>::pop(TDatums& tDatums)
    {
        try
        {
            if (this->mPopIsStopped || this->mTQueue.empty())
                return false;

            tDatums = {std::move(this->mTQueue.top())};
            this->mTQueue.pop();
            this->mConditionVariable.notify_one();
            return true;
        }
        catch (const std::exception& e)
        {
            error(e.what(), __LINE__, __FUNCTION__, __FILE__);
            return false;
        }
    }

    COMPILE_TEMPLATE_DATUM(PriorityQueue);
}

#endif // OPENPOSE_THREAD_PRIORITY_QUEUE_HPP