/*
* 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 .
*
*/
/*! \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
#include
#include
#include
namespace MTS {
template class SubscriberQueue: public Subscriber {
public:
explicit SubscriberQueue(const std::string& name, uint32_t capacity = UINT32_MAX);
virtual ~SubscriberQueue();
Queue& getQueue();
void clear();
virtual void update(const T& object);
private:
Queue m_oQueue;
//Prevent Copy and Assignment operators from being called
SubscriberQueue(const SubscriberQueue&);
SubscriberQueue& operator=(const SubscriberQueue&);
};
template SubscriberQueue::SubscriberQueue(
const std::string& name, uint32_t capacity)
: Subscriber(name), m_oQueue(capacity) {
}
template SubscriberQueue::~SubscriberQueue() {
clear();
}
template Queue& SubscriberQueue::getQueue() {
return m_oQueue;
}
template void SubscriberQueue::clear() {
while (!m_oQueue.empty()) {
T* object = 0;
m_oQueue.poll(object, 0);
delete object;
}
}
template void SubscriberQueue::update(const T& object) {
T* clone = object.clone();
if (clone == 0) {
printWarning("SubQ| %s failed to clone object, out of memory", Subscriber::getName().c_str());
return;
}
bool ok = m_oQueue.offer(clone, 0);
if (!ok) {
delete clone;
printWarning("SubQ| %s queue full, dropping object", Subscriber::getName().c_str());
return;
}
}
}
#endif