diff options
Diffstat (limited to 'include/mts/MTS_SubscriberQueue.h')
-rw-r--r-- | include/mts/MTS_SubscriberQueue.h | 94 |
1 files changed, 94 insertions, 0 deletions
diff --git a/include/mts/MTS_SubscriberQueue.h b/include/mts/MTS_SubscriberQueue.h new file mode 100644 index 0000000..d070841 --- /dev/null +++ b/include/mts/MTS_SubscriberQueue.h @@ -0,0 +1,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 |