summaryrefslogtreecommitdiff
path: root/include/mts/MTS_SubscriberQueue.h
blob: d070841a42603637e1fb2c25fa6c50e521811741 (plain)
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
90
91
92
93
94
/*
 * Copyright (C) 2015 by Multi-Tech Systems
 *
 * This file is part of libmts.
 *
 * libmts is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.
 *
 * libmts is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with libmts.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

/*! \file   MTS_SubscriberQueue.h
 \brief  An template subscriber queue
 \date   28MAR12
 \author Sean Godinez

 A subscriber queue.
 */

#ifndef _MTS_SUBSCRIBERQUEUE_H_
#define _MTS_SUBSCRIBERQUEUE_H_

#include <mts/MTS_Subscriber.h>
#include <mts/MTS_Queue.h>
#include <mts/MTS_Stdint.h>
#include <new>

namespace MTS {

    template<class T> class SubscriberQueue: public Subscriber<T> {

        public:
            explicit SubscriberQueue(const std::string& name, uint32_t capacity = UINT32_MAX);
            virtual ~SubscriberQueue();

            Queue<T*>& getQueue();
            void clear();

            virtual void update(const T& object);

        private:
            Queue<T*> m_oQueue;

            //Prevent Copy and Assignment operators from being called
            SubscriberQueue(const SubscriberQueue&);
            SubscriberQueue& operator=(const SubscriberQueue&);
    };

    template<class T> SubscriberQueue<T>::SubscriberQueue(
    const std::string& name, uint32_t capacity)
    : Subscriber<T>(name), m_oQueue(capacity) {
    }

    template<class T> SubscriberQueue<T>::~SubscriberQueue() {
        clear();
    }

    template<class T> Queue<T*>& SubscriberQueue<T>::getQueue() {
        return m_oQueue;
    }

    template<class T> void SubscriberQueue<T>::clear() {
        while (!m_oQueue.empty()) {
            T* object = 0;
            m_oQueue.poll(object, 0);
            delete object;
        }
    }

    template<class T> void SubscriberQueue<T>::update(const T& object) {
        T* clone = object.clone();
        if (clone == 0) {
            printWarning("SubQ| %s failed to clone object, out of memory", Subscriber<T>::getName().c_str());
            return;
        }
        bool ok = m_oQueue.offer(clone, 0);
        if (!ok) {
            delete clone;
            printWarning("SubQ| %s queue full, dropping object", Subscriber<T>::getName().c_str());
            return;
        }
    }
}

#endif