diff options
author | Jesse Gilles <jgilles@multitech.com> | 2015-04-20 16:49:52 -0500 |
---|---|---|
committer | Jesse Gilles <jgilles@multitech.com> | 2015-04-20 16:49:52 -0500 |
commit | 17b117e73df71925d73ee026b4f54aa1867ce0a5 (patch) | |
tree | 382610c8e598a77a961c5ceb32b9b614ed00e757 /include/mts | |
download | libmts-17b117e73df71925d73ee026b4f54aa1867ce0a5.tar.gz libmts-17b117e73df71925d73ee026b4f54aa1867ce0a5.tar.bz2 libmts-17b117e73df71925d73ee026b4f54aa1867ce0a5.zip |
initial commit
Diffstat (limited to 'include/mts')
25 files changed, 2292 insertions, 0 deletions
diff --git a/include/mts/MTS_AutoLock.h b/include/mts/MTS_AutoLock.h new file mode 100644 index 0000000..e7a05bd --- /dev/null +++ b/include/mts/MTS_AutoLock.h @@ -0,0 +1,56 @@ +/* + * 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_AutoLock.h + \brief Auto lock + \date 08OCT14 + \author Pavlo Samko + + Auto lock. + */ + + +#ifndef _MTS_AUTOLOCK_H_ +#define _MTS_AUTOLOCK_H_ + +#include <mts/MTS_NonCopyable.h> +#include <mts/MTS_Lock.h> + +namespace MTS { + + //! Auto lock + /*! + Auto lock + + \sa Lock + */ + + class AutoLock: private NonCopyable { + + public: + AutoLock(Lock &lock): m_lock(lock) { m_lock.lock(); } //!< Constructs the AutoLock + ~AutoLock() { m_lock.unlock(); } //!< Destructs the AutoLock + + private: + Lock &m_lock; //!< Lock + }; +} + +#endif diff --git a/include/mts/MTS_AutoPtr.h b/include/mts/MTS_AutoPtr.h new file mode 100644 index 0000000..68b82cf --- /dev/null +++ b/include/mts/MTS_AutoPtr.h @@ -0,0 +1,115 @@ +/* + * 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_AutoPtr.h + \brief An autopointer + \date 15JUN11 + \author Sean Godinez + + An autopointer. + */ + +#ifndef MTS_AUTOPTR_H +#define MTS_AUTOPTR_H + +#include <mts/MTS_NonCopyable.h> +#include <mts/MTS_Stdint.h> + +namespace MTS { + + //! An autopointer + /*! + An autopointer cleans up after itself. + */ + template<class T> class AutoPtr: NonCopyable { + + public: + enum MallocType { + SINGLE, ARRAY + }; + + explicit AutoPtr(T* ptr = 0, MallocType type = SINGLE); //!< Explicitly Constructs the AutoPtr to reference ptr + ~AutoPtr(); //!< Destructs the AutoPtr deleting T + + T& operator*() const; //!< Returns T + T* operator->() const; //!< Returns address of T + T& operator[](const uint32_t& index) const; //!< Returns T at index + T* get() const; //!< Returns address of T + T* release(); //!< Removes reference to T (does NOT delete T) and returns address + void reset(T* ptr = 0, MallocType type = SINGLE); //!< Deletes old T and assigns internal reference to ptr + bool isNull() const; //!< Returns true if pointer is NULL + + private: + T* m_pObj; //!< Reference to class T + MallocType m_eType; //!< Describes memory allocated as a single T or block of Ts + + }; + + template<class T> AutoPtr<T>::AutoPtr(T* ptr, MallocType type) + : m_pObj(ptr), m_eType(type) { + } + + template<class T> AutoPtr<T>::~AutoPtr() { + if (m_eType == SINGLE) { + delete m_pObj; + } else { + delete[] m_pObj; + } + } + + template<class T> T& AutoPtr<T>::operator*() const { + return *m_pObj; + } + + template<class T> T* AutoPtr<T>::operator->() const { + return m_pObj; + } + + template<class T> T& AutoPtr<T>::operator[](const uint32_t& index) const { + return m_pObj[index]; + } + + template<class T> T* AutoPtr<T>::get() const { + return m_pObj; + } + + template<class T> T* AutoPtr<T>::release() { + T* temp = m_pObj; + m_pObj = 0; + return temp; + } + + template<class T> void AutoPtr<T>::reset(T* ptr, MallocType type) { + T* temp = m_pObj; + m_pObj = ptr; + if (m_eType == SINGLE) { + delete temp; + } else { + delete[] temp; + } + m_eType = type; + } + + template<class T> bool AutoPtr<T>::isNull() const { + return (m_pObj == 0); + } +} + +#endif diff --git a/include/mts/MTS_BasicPublisher.h b/include/mts/MTS_BasicPublisher.h new file mode 100644 index 0000000..d5749ec --- /dev/null +++ b/include/mts/MTS_BasicPublisher.h @@ -0,0 +1,63 @@ +/* + * 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_BasicPublisher.h + \brief A simple publisher + \date 28MAR12 + \author Sean Godinez + + A basic publisher. + */ + +#ifndef _MTS_BASICPUBLISHER_H_ +#define _MTS_BASICPUBLISHER_H_ + +#include <mts/MTS_Publisher.h> + +namespace MTS { + + //! A basic publisher + /*! + A basic publisher provides simple publishing functionality. + + \sa Publisher, Subscriber, BasicSubscriber + */ + template<class T> class BasicPublisher: public Publisher<T> { + + public: + BasicPublisher(); //!< Constructor + virtual ~BasicPublisher(); //!< Destructor + + virtual void publish(const T& object); //!< Publishes T to Subscribers + }; + + template<class T> BasicPublisher<T>::BasicPublisher() { + } + + template<class T> BasicPublisher<T>::~BasicPublisher() { + } + + template<class T> void BasicPublisher<T>::publish(const T& object) { + Publisher<T>::publish(object); + } + +} + +#endif diff --git a/include/mts/MTS_BasicSubscriber.h b/include/mts/MTS_BasicSubscriber.h new file mode 100644 index 0000000..908dde9 --- /dev/null +++ b/include/mts/MTS_BasicSubscriber.h @@ -0,0 +1,71 @@ +/* + * 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_BasicSubscriber.h + \brief A simple subscriber + \date 28MAR12 + \author Sean Godinez + + A subscriber. + */ +#ifndef _MTS_BASICSUBSCRIBER_H_ +#define _MTS_BASICSUBSCRIBER_H_ + +#include <mts/MTS_Subscriber.h> +#include <mts/MTS_Callback.h> + +namespace MTS { + + //! A basic subscriber + /*! + A basic subscriber provides simple subscribing functionality. + + \sa Publisher, Subscriber, BasicPublisher + */ + template<class T> class BasicSubscriber: public Subscriber<T> { + + public: + explicit BasicSubscriber(const Callback<T>& callback, const std::string& name = ""); //!< Constructor + virtual ~BasicSubscriber();//!< Destructor + + virtual void update(const T& object); //!< Invokes callback with published data T + + private: + Callback<T>* callback; + + //Prevent Copy and Assignment operators from being called + BasicSubscriber(const BasicSubscriber&); + BasicSubscriber& operator=(const BasicSubscriber&); + }; + + template<class T> BasicSubscriber<T>::BasicSubscriber(const Callback<T>& callback, const std::string& name) + : Subscriber<T>(name), callback(callback.clone()) { + } + + template<class T> BasicSubscriber<T>::~BasicSubscriber() { + delete callback; + } + + template<class T> void BasicSubscriber<T>::update(const T& object) { + callback->invoke(object); + } +} + +#endif diff --git a/include/mts/MTS_Buffer.h b/include/mts/MTS_Buffer.h new file mode 100644 index 0000000..96f25a9 --- /dev/null +++ b/include/mts/MTS_Buffer.h @@ -0,0 +1,87 @@ +/* + * 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_Buffer.h + \brief A buffer + \date 15JUN11 + \author Sean Godinez + + A buffer. + */ + +#ifndef MTS_BUFFER_H +#define MTS_BUFFER_H + +#include <mts/MTS_Stdint.h> +#include <vector> +#include <string> + +namespace MTS { + + class Buffer { + + public: + static const uint32_t DEFAULT_CAPACITY; + + explicit Buffer(uint32_t capacity = DEFAULT_CAPACITY); + Buffer(const uint8_t* bytes, uint32_t count); + Buffer(const Buffer& other); + virtual ~Buffer(); + + Buffer& operator=(const Buffer& other); + + const uint8_t* getBuffer() const; + std::string str() const; + + uint32_t getSize() const; + void setSize(uint32_t newSize); + + uint32_t getCapacity() const; + void setCapacity(uint32_t newCapacity); + + void clear(); + void compact(); + + uint8_t operator[](uint32_t index) const; + uint8_t operator[](uint32_t index); + + const uint8_t& at(uint32_t index) const; + uint8_t& at(uint32_t index); + + Buffer& append(uint8_t byte); + Buffer& append(const uint8_t* bytes, uint32_t count); + + Buffer& insert(uint32_t index, uint8_t byte); + Buffer& insert(uint32_t index, const uint8_t* bytes, + uint32_t count); + + Buffer& remove(uint32_t index); + Buffer& remove(uint32_t start, uint32_t end); + + Buffer& replace(uint32_t start, uint32_t end, const uint8_t* bytes, uint32_t count); + Buffer* clone() const; + + private: + std::vector<uint8_t> m_vBuffer; + + }; +} + +#endif diff --git a/include/mts/MTS_Component.h b/include/mts/MTS_Component.h new file mode 100644 index 0000000..215add5 --- /dev/null +++ b/include/mts/MTS_Component.h @@ -0,0 +1,84 @@ +/* + * 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_Component.h + \brief A threaded sub-pub object + \date 28MAR12 + \author Sean Godinez + + A threaded subscriber-publisher object. + */ + +#ifndef _MTS_COMPONENT_H_ +#define _MTS_COMPONENT_H_ + +#include <mts/MTS_Thread.h> +#include <mts/MTS_NonCopyable.h> +#include <mts/MTS_SubscriberQueue.h> +#include <mts/MTS_BasicPublisher.h> +#include <mts/MTS_Stdint.h> +#include <string> + +namespace MTS { + + template<class S, class P> class Component: public Thread { + + public: + virtual ~Component(); + + Subscriber<S>& getSubscriber(); + Publisher<P>& getPublisher(); + + virtual void stop(); + + protected: + explicit Component(const std::string& sName, uint32_t iCapacity = UINT32_MAX, bool bManaged = true); + + SubscriberQueue<S> m_oSubscriber; + BasicPublisher<P> m_oPublisher; + + private: + + }; + + template<class S, class P> Component<S, P>::Component( + const std::string& name, uint32_t capacity, bool managed) + : Thread(name, managed), m_oSubscriber(name, capacity) { + } + + template<class S, class P> Component<S, P>::~Component() { + Component::stop(); + } + + template<class S, class P> Subscriber<S>& Component<S, P>::getSubscriber() { + return m_oSubscriber; + } + + template<class S, class P> Publisher<P>& Component<S, P>::getPublisher() { + return m_oPublisher; + } + + template<class S, class P> void Component<S, P>::stop() { + Thread::stop(); + m_oSubscriber.clear(); + } +} + +#endif diff --git a/include/mts/MTS_Condition.h b/include/mts/MTS_Condition.h new file mode 100644 index 0000000..c15acd0 --- /dev/null +++ b/include/mts/MTS_Condition.h @@ -0,0 +1,75 @@ +/* + * 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_Condition.h + \brief A condition that can be waited on or signaled + \date 15JUN11 + \author Sean Godinez + + A platform independent condition that can be waited on or signaled. + */ + +#ifndef _MTS_CONDITION_H_ +#define _MTS_CONDITION_H_ + +#include <mts/MTS_Stdint.h> +#include <mts/MTS_AutoPtr.h> +#include <mts/MTS_NonCopyable.h> + +#ifdef WIN32 +#include <windows.h> +#else +#include <pthread.h> +#endif + +namespace MTS { + + class Lock; + + //! A condition + /*! + A platform independent condition that can be waited on or signaled. Useful in building semaphores. + + \sa Lock + */ + class Condition: private NonCopyable { + friend class Lock; + + public: + ~Condition(); //!< Destructs the Condition + + void wait(); //!< Waits on *this Condition for signal + void wait(uint32_t millis); //!< Waits on *this Condition for signal or given amount of time + void signal(); //!< Signals *this Condition + + private: + + Lock* m_pLock; //!< Lock that created *this Condition +#ifdef WIN32 + AutoPtr<HANDLE> m_apCondition; //!< Handle to WIN32 Condition +#else + AutoPtr<pthread_cond_t> m_apCondition; //!< Handle to PTHREAD Condition +#endif + + explicit Condition(Lock* lock); //!< Explicit Constructor! Must pass Lock as parent. + }; +} + +#endif diff --git a/include/mts/MTS_Lock.h b/include/mts/MTS_Lock.h new file mode 100644 index 0000000..c35b131 --- /dev/null +++ b/include/mts/MTS_Lock.h @@ -0,0 +1,76 @@ +/* + * 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_Lock.h + \brief A mutex lock + \date 15JUN11 + \author Sean Godinez + + A platform independent mutex lock. + */ + +#ifndef _MTS_LOCK_H_ +#define _MTS_LOCK_H_ + +#include <mts/MTS_NonCopyable.h> +#include <mts/MTS_Condition.h> +#include <mts/MTS_AutoPtr.h> + +#ifdef WIN32 +#include <windows.h> +#else +#include <pthread.h> +#endif + +namespace MTS { + + //! A mutex lock + /*! + A platform independent mutex lock + + \sa Condition + */ + + class Lock: private NonCopyable { + + public: + Lock(); //!< Constructs the Lock + ~Lock(); //!< Destructs the Lock + + void lock(); //!< Locks the mutex + void unlock(); //!< Unlocks the mutex + + bool isLocked() const; //!< Returns true if locked + + Condition* createCondition(); //!< Creates a Condition object associated with *this Lock + + private: + friend class Condition; + volatile bool m_bLocked; //!< Locks the mutex +#ifdef WIN32 + AutoPtr<HANDLE> m_apMutex; //!< WIN32 Mutex Handle +#else + AutoPtr<pthread_mutex_t> m_apMutex; //!< PTHREAD Mutex Handle + AutoPtr<pthread_mutexattr_t> m_apMutexAttr; //!< PTHREAD Mutex Attributes +#endif + }; +} + +#endif diff --git a/include/mts/MTS_Logger.h b/include/mts/MTS_Logger.h new file mode 100644 index 0000000..a1e88a2 --- /dev/null +++ b/include/mts/MTS_Logger.h @@ -0,0 +1,161 @@ +/* + * 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_Logger.h + \brief A logging utility + \date 15JUN11 + \author Sean Godinez + + A logging utility capable of writing to stdout, syslog, or file + */ +#ifndef MTS_LOGGER_H_ +#define MTS_LOGGER_H_ + +#include <mts/MTS_NonCopyable.h> +#include <mts/MTS_NonConstructable.h> +#include <mts/MTS_Lock.h> +#include <mts/MTS_Stdint.h> +#include <string> + +#ifdef DEBUG +#define printFatal(format, ...) \ + if (MTS::Logger::isPrintable(MTS::Logger::PrintLevel::FATAL_LEVEL)) \ + MTS::Logger::printfGeneric(MTS::Logger::PrintLevel::FATAL_LEVEL, MTS::Logger::PrintLevel::FATAL_LABEL, "%s:%s:%d| " format, __FILE__, __func__, __LINE__, ##__VA_ARGS__) +#define printError(format, ...) \ + if (MTS::Logger::isPrintable(MTS::Logger::PrintLevel::ERROR_LEVEL)) \ + MTS::Logger::printfGeneric(MTS::Logger::PrintLevel::ERROR_LEVEL, MTS::Logger::PrintLevel::ERROR_LABEL, "%s:%s:%d| " format, __FILE__, __func__, __LINE__, ##__VA_ARGS__) +#define printWarning(format, ...) \ + if (MTS::Logger::isPrintable(MTS::Logger::PrintLevel::WARNING_LEVEL)) \ + MTS::Logger::printfGeneric(MTS::Logger::PrintLevel::WARNING_LEVEL, MTS::Logger::PrintLevel::WARNING_LABEL, "%s:%s:%d| " format, __FILE__, __func__, __LINE__, ##__VA_ARGS__) +#define printInfo(format, ...) \ + if (MTS::Logger::isPrintable(MTS::Logger::PrintLevel::INFO_LEVEL)) \ + MTS::Logger::printfGeneric(MTS::Logger::PrintLevel::INFO_LEVEL, MTS::Logger::PrintLevel::INFO_LABEL, "%s:%s:%d| " format, __FILE__, __func__, __LINE__, ##__VA_ARGS__) +#define printConfig(format, ...) \ + if (MTS::Logger::isPrintable(MTS::Logger::PrintLevel::CONFIG_LEVEL)) \ + MTS::Logger::printfGeneric(MTS::Logger::PrintLevel::CONFIG_LEVEL, MTS::Logger::PrintLevel::CONFIG_LABEL, "%s:%s:%d| " format, __FILE__, __func__, __LINE__, ##__VA_ARGS__) +#define printDebug(format, ...) \ + if (MTS::Logger::isPrintable(MTS::Logger::PrintLevel::DEBUG_LEVEL)) \ + MTS::Logger::printfGeneric(MTS::Logger::PrintLevel::DEBUG_LEVEL, MTS::Logger::PrintLevel::DEBUG_LABEL, "%s:%s:%d| " format, __FILE__, __func__, __LINE__, ##__VA_ARGS__) +#define printTrace(format, ...) \ + if (MTS::Logger::isPrintable(MTS::Logger::PrintLevel::TRACE_LEVEL)) \ + MTS::Logger::printfGeneric(MTS::Logger::PrintLevel::TRACE_LEVEL, MTS::Logger::PrintLevel::TRACE_LABEL, "%s:%s:%d| " format, __FILE__, __func__, __LINE__, ##__VA_ARGS__) +#else +#define printFatal(format, ...) \ + if (MTS::Logger::isPrintable(MTS::Logger::PrintLevel::FATAL_LEVEL)) \ + MTS::Logger::printfGeneric(MTS::Logger::PrintLevel::FATAL_LEVEL, MTS::Logger::PrintLevel::FATAL_LABEL, format, ##__VA_ARGS__) +#define printError(format, ...) \ + if (MTS::Logger::isPrintable(MTS::Logger::PrintLevel::ERROR_LEVEL)) \ + MTS::Logger::printfGeneric(MTS::Logger::PrintLevel::ERROR_LEVEL, MTS::Logger::PrintLevel::ERROR_LABEL, format, ##__VA_ARGS__) +#define printWarning(format, ...) \ + if (MTS::Logger::isPrintable(MTS::Logger::PrintLevel::WARNING_LEVEL)) \ + MTS::Logger::printfGeneric(MTS::Logger::PrintLevel::WARNING_LEVEL, MTS::Logger::PrintLevel::WARNING_LABEL, format, ##__VA_ARGS__) +#define printInfo(format, ...) \ + if (MTS::Logger::isPrintable(MTS::Logger::PrintLevel::INFO_LEVEL)) \ + MTS::Logger::printfGeneric(MTS::Logger::PrintLevel::INFO_LEVEL, MTS::Logger::PrintLevel::INFO_LABEL, format, ##__VA_ARGS__) +#define printConfig(format, ...) \ + if (MTS::Logger::isPrintable(MTS::Logger::PrintLevel::CONFIG_LEVEL)) \ + MTS::Logger::printfGeneric(MTS::Logger::PrintLevel::CONFIG_LEVEL, MTS::Logger::PrintLevel::CONFIG_LABEL, format, ##__VA_ARGS__) +#define printDebug(format, ...) \ + if (MTS::Logger::isPrintable(MTS::Logger::PrintLevel::DEBUG_LEVEL)) \ + MTS::Logger::printfGeneric(MTS::Logger::PrintLevel::DEBUG_LEVEL, MTS::Logger::PrintLevel::DEBUG_LABEL, format, ##__VA_ARGS__) +#define printTrace(format, ...) \ + if (MTS::Logger::isPrintable(MTS::Logger::PrintLevel::TRACE_LEVEL)) \ + MTS::Logger::printfGeneric(MTS::Logger::PrintLevel::TRACE_LEVEL, MTS::Logger::PrintLevel::TRACE_LABEL, format, ##__VA_ARGS__) +#endif + +namespace MTS { + + class Logger: private NonCopyable { + + public: + + class PrintLevel: private NonConstructable { + public: + static const int32_t OFF_LEVEL; + static const int32_t MINIMUM_LEVEL; + static const int32_t FATAL_LEVEL; + static const int32_t ERROR_LEVEL; + static const int32_t WARNING_LEVEL; + static const int32_t INFO_LEVEL; + static const int32_t CONFIG_LEVEL; + static const int32_t DEBUG_LEVEL; + static const int32_t TRACE_LEVEL; + static const int32_t MAXIMUM_LEVEL; + + static const char* OFF_LABEL; + static const char* FATAL_LABEL; + static const char* ERROR_LABEL; + static const char* WARNING_LABEL; + static const char* INFO_LABEL; + static const char* CONFIG_LABEL; + static const char* DEBUG_LABEL; + static const char* TRACE_LABEL; + static const char* MAXIMUM_LABEL; + }; + + enum PrintMode { + NO_PRINTING, + STDOUT_ONLY, + FILE_ONLY, + SYSLOG_ONLY, + STDOUT_AND_FILE, + STDOUT_AND_SYSLOG + }; + + static int getPrintLevel(); + static const std::string& getPrintLevelString(); + static void setPrintLevel(int32_t level, bool silent = false); + static bool isPrintable(int32_t level); + + static void printfFatal(const char* format, ...); + static void printfError(const char* format, ...); + static void printfWarning(const char* format, ...); + static void printfInfo(const char* format, ...); + static void printfConfig(const char* format, ...); + static void printfDebug(const char* format, ...); + static void printfTrace(const char* format, ...); + static void printfGeneric(int level, const char* label, const char* format, ...); + + static void printf(int level, const char* format, ...); + static void printf(const char* format, ...); + + static bool setup(const PrintMode& mode); + static bool setup(const PrintMode& mode, const std::string& ident, const int& option, const int& facility); + static bool setup(const PrintMode& mode, const std::string& filename); + + private: + + static volatile int m_iPrintLevel; + static std::string m_sPrintLevel; + static Logger::PrintMode m_eMode; + static Lock m_oPrintLock; + static FILE* m_pFile; + static int m_iLogFacility; + static std::string m_sIdent; + static std::string m_sFileName; + + static int32_t syslogPrintLevelConversion(const int32_t& level); + static void printMessage(const int32_t& level, const char* label, const char* format, va_list argptr); + + Logger(); + }; +} + +#endif /* MTS_LOG_H_ */ diff --git a/include/mts/MTS_NonConstructable.h b/include/mts/MTS_NonConstructable.h new file mode 100644 index 0000000..ab69c1d --- /dev/null +++ b/include/mts/MTS_NonConstructable.h @@ -0,0 +1,47 @@ +/* + * 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_NonConstructable.h + \brief Disables Constructors + \date May 7, 2012 + \author Sean Godinez + + Prevents this class from being constructed by anyone + */ + +#ifndef MTS_NONCONSTRUCTABLE_H_ +#define MTS_NONCONSTRUCTABLE_H_ + +namespace MTS { + + class NonConstructable { + protected: + + private: + // Do Not Allow Use of Any Constructors or the Assignment Operator + NonConstructable(); + ~NonConstructable(); + NonConstructable(const NonConstructable&); + const NonConstructable& operator=(const NonConstructable&); + }; + +} + +#endif /* MTS_NONCONSTRUCTABLE_H_ */ diff --git a/include/mts/MTS_NonCopyable.h b/include/mts/MTS_NonCopyable.h new file mode 100644 index 0000000..c5a772a --- /dev/null +++ b/include/mts/MTS_NonCopyable.h @@ -0,0 +1,47 @@ +/* + * 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_NonCopyable.h + \brief Disables Copying + \date May 7, 2012 + \author Sean Godinez + + Prevents this class from being copied by anyone + */ +#ifndef _MTS_NONCOPYABLE_H_ +#define _MTS_NONCOPYABLE_H_ + +namespace MTS { + + class NonCopyable { + protected: + NonCopyable() { + } + ~NonCopyable() { + } + private: + // Do Not Allow Use of Copy Constructor or Assignment Operator + NonCopyable(const NonCopyable&); + const NonCopyable& operator=(const NonCopyable&); + }; + +} + +#endif /* _MTS_NONCOPYABLE_H_ */ diff --git a/include/mts/MTS_Object.h b/include/mts/MTS_Object.h new file mode 100644 index 0000000..4fd9d2d --- /dev/null +++ b/include/mts/MTS_Object.h @@ -0,0 +1,62 @@ +/* + * 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_Object.h + \brief An object + \date 28MAR12 + \author Sean Godinez + + A common base class to handle objects. + */ +#ifndef _MTS_OBJECT_H_ +#define _MTS_OBJECT_H_ + +#include <mts/MTS_Buffer.h> +#include <ostream> +#include <string> +#include <set> + +namespace MTS { + + //! An object + /*! + A common base class to handle objects + + \sa Buffer + */ + class Object { + + public: + virtual ~Object(); //!< Destructs object + + virtual void encode(Buffer& buffer) const = 0; //!< Encodes object into byte buffer + virtual void decode(Buffer& buffer) = 0; //!< Decodes object from byte buffer + virtual void copy(const Object& object) = 0; //!< Copies another object + virtual Object* clone() const = 0; //!< Clones object + + virtual std::string toString() const; //!< Generates a string of the object + virtual std::string toIDD() const; //!< Generates a string of the object type id + + protected: + Object(); //!< Constructor of object + }; +} + +#endif diff --git a/include/mts/MTS_Publisher.h b/include/mts/MTS_Publisher.h new file mode 100644 index 0000000..8b0fcee --- /dev/null +++ b/include/mts/MTS_Publisher.h @@ -0,0 +1,130 @@ +/* + * 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_Publisher.h + \brief An abstract publisher + \date 28MAR12 + \author Sean Godinez + + A publisher base class. Requires derived class to handle publishing. + */ +#ifndef _MTS_PUBLISHER_H_ +#define _MTS_PUBLISHER_H_ + +#include <mts/MTS_Subscriber.h> +#include <mts/MTS_AutoPtr.h> +#include <mts/MTS_Lock.h> +#include <mts/MTS_Logger.h> +#include <mts/MTS_NonCopyable.h> +#include <set> + +namespace MTS { + + //! An abstract publisher + /*! + A template abstract publisher class + + \sa Subscriber + */ + template<class T> class Publisher: NonCopyable { + + public: + virtual ~Publisher(); //!< Destructs subscriber + + void addSubscriber(Subscriber<T>& subscriber); //!< Adds a subscriber + void removeSubscriber(Subscriber<T>& subscriber); //!< Removes a subscriber + + protected: + Publisher(); //!< Constructs publisher + virtual void publish(const T& object); //!< Protects publishing operation + + private: + typedef std::set<Subscriber<T>*> SubscriberSet; + + AutoPtr<Lock> m_apLock; //!< Guards for thread safety + SubscriberSet m_sSubscribers; //!< Set of subscribers + + }; + + template<class T> Publisher<T>::Publisher() { + m_apLock.reset(new Lock()); + } + + template<class T> Publisher<T>::~Publisher() { + m_apLock.reset(); + } + + template<class T> void Publisher<T>::addSubscriber( + Subscriber<T>& subscriber) { + + m_apLock->lock(); + m_sSubscribers.insert(&subscriber); + + /* + + //WARNING IF ALREADY PRESENT IN SET + pair<set<T>::iterator,bool> ret; + ret = m_sSubscribers.insert(&subscriber); + if (ret.second==false) { + Logger::printWarning("subscriber already in set %s (%p)", + subscriber->getName().c_str(), subscriber); + } + + */ + + m_apLock->unlock(); + } + + template<class T> void Publisher<T>::removeSubscriber( + Subscriber<T>& subscriber) { + m_apLock->lock(); + m_sSubscribers.erase(&subscriber); + + /* + + //WARNING IF SUBSCRIBER WAS NOT IN SET + uint32_t count = m_sSubscribers.erase(&subscriber); + if (count == 0) { + Logger::printWarning("subscriber was not in set %s (%p)", + subscriber->getName().c_str(), subscriber); + } + + */ + + m_apLock->unlock(); + } + + template<class T> void Publisher<T>::publish(const T& object) { + m_apLock->lock(); + SubscriberSet set(m_sSubscribers); + for (typename SubscriberSet::iterator i = set.begin(); i != set.end(); i++) { + Subscriber<T>* subscriber = *i; + try { + subscriber->update(object); + } catch (...) { + printWarning("Publisher| exception caught while updating subscriber %s (%p)", + subscriber->getName().c_str(), subscriber); + } + } + m_apLock->unlock(); + } +} + +#endif diff --git a/include/mts/MTS_Queue.h b/include/mts/MTS_Queue.h new file mode 100644 index 0000000..aa59e51 --- /dev/null +++ b/include/mts/MTS_Queue.h @@ -0,0 +1,215 @@ +/* + * 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_Queue.h + \brief A queue + \date 15JUN11 + \author Sean Godinez + + A thread-safe queue with condition signaling + */ +#ifndef _MTS_QUEUE_H_ +#define _MTS_QUEUE_H_ + +#include <mts/MTS_NonCopyable.h> +#include <mts/MTS_AutoPtr.h> +#include <mts/MTS_Lock.h> +#include <mts/MTS_Logger.h> +#include <mts/MTS_Stdint.h> +#include <queue> +#include <cassert> + +namespace MTS { + + //! A queue + /*! + A thread-safe, blocking, template queue that signals when it is not empty, and when it is not full. + + \sa AutoPtr, Lock + */ + template<class T> class Queue : public NonCopyable { + + public: + explicit Queue(uint32_t capacity = UINT32_MAX); //!< Constructs the queue + ~Queue(); //!< Destructs the queue + + bool empty() const; //!< Returns true if queue is empty + bool full() const; //!< Returns true if queue is full + uint32_t size() const; //!< Returns the number of elements in the queue + uint32_t capacity() const; //!< Returns the maximum capacity of the queue + + void push(const T& object); //!< Pushes an object on the queue (BLOCKS IF FULL) + void pop(T& object); //!< Pops an object off the queue (BLOCKS IF EMPTY) + + bool offer(const T& object, uint32_t waitMillis); + bool poll(T& object, uint32_t waitMillis); + bool peek(T& object, uint32_t waitMillis); + + private: + const uint32_t m_iMaxSize; + std::queue<T> m_qContainer; + AutoPtr<Lock> m_apLock; + AutoPtr<Condition> m_apNotEmpty; + AutoPtr<Condition> m_apNotFull; + }; + + template<class T> Queue<T>::Queue(uint32_t capacity) + : m_iMaxSize(capacity) { + m_apLock.reset(new Lock()); + m_apNotEmpty.reset(m_apLock->createCondition()); + m_apNotFull.reset(m_apLock->createCondition()); + } + + template<class T> Queue<T>::~Queue() { + m_apNotFull.reset(); + m_apNotEmpty.reset(); + m_apLock.reset(); + } + + template<class T> bool Queue<T>::empty() const { + bool result = false; + m_apLock->lock(); + result = m_qContainer.empty(); + m_apLock->unlock(); + return result; + } + + template<class T> bool Queue<T>::full() const { + bool result = false; + m_apLock->lock(); + result = (m_qContainer.size() >= m_iMaxSize); + m_apLock->unlock(); + return result; + } + + template<class T> uint32_t Queue<T>::size() const { + uint32_t result = 0; + m_apLock->lock(); + result = static_cast<uint32_t>(m_qContainer.size()); + m_apLock->unlock(); + return result; + } + + template<class T> uint32_t Queue<T>::capacity() const { + return m_iMaxSize; + } + + template<class T> void Queue<T>::push(const T& object) { + // Block forever until there is room on the queue + m_apLock->lock(); + try { + while (m_qContainer.size() >= m_iMaxSize) { + m_apNotFull->wait(); + } + m_qContainer.push(object); + m_apNotEmpty->signal(); + } catch (...) { + printWarning("Queue| failed to push object"); + assert(false); + } + m_apLock->unlock(); + } + + template<class T> void Queue<T>::pop(T& object) { + //Block on not-empty condition if empty + m_apLock->lock(); + while (m_qContainer.empty()) { + m_apNotEmpty->wait(); + } + object = m_qContainer.front(); + m_qContainer.pop(); + m_apNotFull->signal(); + m_apLock->unlock(); + } + + //! Attempts to push an object onto the queue within the given milliseconds + /*! + \param object an object to push onto the queue + \param waitMillis the amount of milliseonds to wait if the queue is full + \return Returns true if the object was pushed onto the queue, false otherwise + \sa push() + */ + template<class T> bool Queue<T>::offer(const T& object, + uint32_t waitMillis) { + bool result = false; + m_apLock->lock(); + try { + if (waitMillis > 0 && m_qContainer.size() >= m_iMaxSize) { + m_apNotFull->wait(waitMillis); + } + if (m_qContainer.size() < m_iMaxSize) { + m_qContainer.push(object); + m_apNotEmpty->signal(); + result = true; + } + } catch (...) { + printWarning("Queue| failed to offer object"); + assert(false); + } + m_apLock->unlock(); + return result; + } + + //! Attempts to pop an object from the queue within the given milliseconds + /*! + \param object if successful, a reference to the object that was popped + \param waitMillis the amount of milliseonds to wait if the queue is empty + \return Returns true if an object was popped from the queue, false otherwise + \sa pop(), peek() + */ + template<class T> bool Queue<T>::poll(T& object, uint32_t waitMillis) { + bool result = false; + m_apLock->lock(); + if (waitMillis > 0 && m_qContainer.empty()) { + m_apNotEmpty->wait(waitMillis); + } + if (!m_qContainer.empty()) { + object = m_qContainer.front(); + m_qContainer.pop(); + m_apNotFull->signal(); + result = true; + } + m_apLock->unlock(); + return result; + } + + //! Attempts to view the top of the queue within the given milliseconds + /*! + \param object if successful, a reference to the object that is at the head of the queue + \param waitMillis the amount of milliseonds to wait if the queue is empty + \return Returns true if there is an object in the queue, false otherwise + \sa pop(), poll() + */ + template<class T> bool Queue<T>::peek(T& object, uint32_t waitMillis) { + bool result = false; + m_apLock->lock(); + if (waitMillis > 0 && m_qContainer.empty()) { + m_apNotEmpty->wait(waitMillis); + } + if (!m_qContainer.empty()) { + object = m_qContainer.front(); + result = true; + } + m_apLock->unlock(); + return result; + } +} + +#endif diff --git a/include/mts/MTS_SharedPtr.h b/include/mts/MTS_SharedPtr.h new file mode 100644 index 0000000..0fd670e --- /dev/null +++ b/include/mts/MTS_SharedPtr.h @@ -0,0 +1,137 @@ +/* + * 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_SharedPtr.h + \brief A shared pointer template + \date 15JUN11 + \author Sean Godinez + + A shared pointer template + */ + +#ifndef _MTS_SHAREDPTR_H +#define _MTS_SHAREDPTR_H + +namespace MTS { + + //! A shared pointer + /*! + A shared pointer keeps track of references and + cleans up when the reference count reaches zero + */ + template<class T> class SharedPtr { + + public: + + enum MallocType { + SINGLE, ARRAY + }; + + explicit SharedPtr(T* ptr = 0, MallocType type = SINGLE); //!< Explicitly Constructs the SharedPtr to reference ptr + SharedPtr(const SharedPtr<T>& other); //!< Constructs from another shared pointer, increasing reference count + ~SharedPtr(); //!< Destructs the AutoPtr deleting T + + SharedPtr<T>& operator=(const SharedPtr<T>& other); //!< Releases *this and shares other's T + + T& operator*() const; //!< Returns T + T* operator->() const; //!< Returns address of T + T& operator[](const uint32_t& index) const; //!< Returns T at index + T* get() const; //!< Returns address of T + + bool isNull() const; //!< Returns true if pointer is NULL + bool isShared() const; //!< Returns true if reference count is greater than 1 + int count() const; //!< Returns number of references + + private: + + T* m_pObj; //!< Reference to class T + int* m_pReferences; //!< Reference to reference count + MallocType m_eType; //!< Describes memory allocated as a single T or block of Ts + + void release(); + }; + + template<class T> SharedPtr<T>::SharedPtr(T* ptr, MallocType type) + : m_pObj(ptr), m_pReferences(new int(1)), m_eType(type) { + } + + template<class T> SharedPtr<T>::SharedPtr(const SharedPtr<T>& other) + : m_pObj(other.m_pObj), m_pReferences(other.m_pReferences) { + *(m_pReferences) += 1; + } + + template<class T> SharedPtr<T>::~SharedPtr() { + release(); + } + + template<class T> SharedPtr<T>& SharedPtr<T>::operator=( + const SharedPtr<T>& other) { + if (&other != this) { + release(); + this->m_pObj = other.m_pObj; + this->m_pReferences = other.m_pReferences; + this->m_eType = other.m_eType; + (*m_pReferences) += 1; + } + return *this; + } + + template<class T> T& SharedPtr<T>::operator*() const { + return *m_pObj; + } + + template<class T> T* SharedPtr<T>::operator->() const { + return m_pObj; + } + + template<class T> T& SharedPtr<T>::operator[](const uint32_t& index) const { + return m_pObj[index]; + } + + template<class T> T* SharedPtr<T>::get() const { + return m_pObj; + } + + template<class T> bool SharedPtr<T>::isNull() const { + return (m_pObj == 0); + } + + template<class T> bool SharedPtr<T>::isShared() const { + return ((*m_pReferences) > 1); + } + + template<class T> int SharedPtr<T>::count() const { + return (*m_pReferences); + } + + template<class T> void SharedPtr<T>::release() { + (*m_pReferences) -= 1; + if ((*m_pReferences) == 0) { + if (m_eType == SINGLE) { + delete m_pObj; + } else { + delete[] m_pObj; + } + delete m_pReferences; + } + } +} + +#endif diff --git a/include/mts/MTS_SignalThread.h b/include/mts/MTS_SignalThread.h new file mode 100644 index 0000000..5f6aab4 --- /dev/null +++ b/include/mts/MTS_SignalThread.h @@ -0,0 +1,74 @@ +/* + * 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_SignalThread.h + \brief An abstract worker thread + \date 01MAR13 + \author Mike Fiore + + A platform independent thread that executes a task when it is signaled. + */ + +#ifndef _MTS_SIGNAL_THREAD_H_ +#define _MTS_SIGNAL_THREAD_H_ + +#include <mts/MTS_Thread.h> +#include <mts/MTS_AutoPtr.h> +#include <mts/MTS_Lock.h> +#include <mts/MTS_Condition.h> + +namespace MTS { + + //! An abstract worker thread + /*! + A platform independent worker thread class + */ + + class SignalThread: public Thread { + + public: + + ~SignalThread(); //!< Destructs the thread + + bool isExecuting() const; //!< Returns true if the thread is currently running executing the execute() function + + void signal(); //!< Instructs the thread to run the execute() function + + protected: + + explicit SignalThread(const std::string& name); //!< Creates the thread + explicit SignalThread(const std::string& name, uint32_t waitMillis); //!< Creates the thread + + private: + + void run(); //!< Abstracts the running of the thread + virtual void execute() = 0; //!< main() of the thread which is executed each time signal() is called + void init(); //!< Startup code common to all constructors + + AutoPtr<Lock> m_apStateLock; //!< Condition Lock + AutoPtr<Lock> m_apConditionLock; //!< Condition Lock + AutoPtr<Condition> m_apCondition; //!< Condition for signaling and waiting + bool m_bShouldExecute; //!< Should execute flag + bool m_bCurrentlyExecuting; //!< Currently executing flag + uint32_t m_ui32WaitMillis; //!< Max time to wait on condition before checking for shutdown + }; +} + +#endif diff --git a/include/mts/MTS_Singleton.h b/include/mts/MTS_Singleton.h new file mode 100644 index 0000000..463d1c0 --- /dev/null +++ b/include/mts/MTS_Singleton.h @@ -0,0 +1,77 @@ +/* + * 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_Singleton.h + \brief A Documented file. + \date 2011-06-20 + \author Sean Godinez + + Details. + */ + +#ifndef _MTS_SINGLETON_H_ +#define _MTS_SINGLETON_H_ + +#include <mts/MTS_Lock.h> + +namespace MTS { + + template<class T> class Singleton { + + public: + + static T* instance(); + + protected: + + private: + static T* m_pInstance; + static AutoPtr<Lock> m_apLock; + + // Do Not Allow Use of Any Constructor or Assignment Operator + Singleton() { + } + ~Singleton(); + Singleton(const Singleton&); + Singleton& operator=(const Singleton&); + }; + + template<class T> Singleton<T>::~Singleton() { + m_apLock.reset(); + delete T; + } + + template<class T> T* Singleton<T>::instance() { + + m_apLock->lock(); + if (m_pInstance == NULL) { + m_pInstance = new T(); + } + m_apLock->unlock(); + + return m_pInstance; + } + + //FIXME: Template Initialization + template<class T> static AutoPtr<Lock> Singleton<T>::m_apLock.reset(new Lock()); + template<class T> static T* Singleton<T>::m_pInstance = NULL; +} + +#endif /* MTS_SINGLETON_H_ */ diff --git a/include/mts/MTS_Stdint.h b/include/mts/MTS_Stdint.h new file mode 100644 index 0000000..9a6885b --- /dev/null +++ b/include/mts/MTS_Stdint.h @@ -0,0 +1,104 @@ +/* + * 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_Stdint.h + \brief A set of defined types + \date 28MAR12 + \author Sean Godinez + + A set of defined types + */ +#ifndef MTS_STDINT_H +#define MTS_STDINT_H + +#ifdef WIN32 +typedef __int8 int8_t; +typedef __int16 int16_t; +typedef __int32 int32_t; +typedef __int64 int64_t; + +typedef unsigned __int8 uint8_t; +typedef unsigned __int16 uint16_t; +typedef unsigned __int32 uint32_t; +typedef unsigned __int64 uint64_t; + +#ifndef INT8_MIN +#define INT8_MIN (-127i8 - 1) +#endif + +#ifndef INT16_MIN +#define INT16_MIN (-32767i16 - 1) +#endif + +#ifndef INT32_MIN +#define INT32_MIN (-2147483647i32 - 1) +#endif + +#ifndef INT64_MIN +#define INT64_MIN (-9223372036854775807i64 - 1) +#endif + +#ifndef INT8_MAX +#define INT8_MAX (127i8) +#endif + +#ifndef INT16_MAX +#define INT16_MAX (32767i16) +#endif + +#ifndef INT32_MAX +#define INT32_MAX (2147483647i32) +#endif + +#ifndef INT64_MAX +#define INT64_MAX (9223372036854775807i64) +#endif + +#ifndef UINT8_MAX +#define UINT8_MAX (0xffui8) +#endif + +#ifndef UINT16_MAX +#define UINT16_MAX (0xffffui16) +#endif + +#ifndef UINT32_MAX +#define UINT32_MAX (0xffffffffui32) +#endif + +#ifndef UINT64_MAX +#define UINT64_MAX (0xffffffffffffffffui64) +#endif + +#elif __cplusplus +#ifdef STDINT +#ifndef __STDC_LIMIT_MACROS +#define __STDC_LIMIT_MACROS +#endif +#include <stdint.h> +#else +#include <cstdint> +#endif +#else +#include <stdint.h> +#endif + +#endif + diff --git a/include/mts/MTS_Subscriber.h b/include/mts/MTS_Subscriber.h new file mode 100644 index 0000000..e359d4c --- /dev/null +++ b/include/mts/MTS_Subscriber.h @@ -0,0 +1,71 @@ +/* + * 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_Subscriber.h + \brief An abstract subscriber + \date 28MAR12 + \author Sean Godinez + + A subscriber. + */ +#ifndef _MTS_SUBSCRIBER_H_ +#define _MTS_SUBSCRIBER_H_ + +#include <mts/MTS_NonCopyable.h> +#include <string> + +namespace MTS { + + //! An abstract subscriber + /*! + A template abstract subscriber class + + \sa Publisher + */ + template<class T> class Subscriber: NonCopyable { + + public: + virtual ~Subscriber(); //!< Destructs subscriber + + const std::string& getName() const; //!< Returns name + + virtual void update(const T& object) = 0; //!< Updates subscriber + + protected: + explicit Subscriber(const std::string& name = ""); //!< Protected Constructor + + private: + const std::string m_sName; //!< Name of subscriber + + }; + + template<class T> Subscriber<T>::Subscriber(const std::string& name) + : m_sName(name) { + } + + template<class T> Subscriber<T>::~Subscriber() { + } + + template<class T> const std::string& Subscriber<T>::getName() const { + return m_sName; + } +} + +#endif 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 diff --git a/include/mts/MTS_System.h b/include/mts/MTS_System.h new file mode 100644 index 0000000..a16265c --- /dev/null +++ b/include/mts/MTS_System.h @@ -0,0 +1,55 @@ +/* + * 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_System.h + \brief System Utilities + \date 15JUN11 + \author Sean Godinez + + System utilities for time, endian, etc. + */ +#ifndef MTS_SYSTEM_H +#define MTS_SYSTEM_H + +#include <mts/MTS_NonConstructable.h> +#include <mts/MTS_Stdint.h> +#include <string> + +namespace MTS { + + class System: private NonConstructable { + + public: + + //UTC (Coordinated Universal Time) : This is the standard international time or the Greenwich Mean Time. + //EPOCH : number of seconds elapsed since 00:00:00 on January 1, 1970, Coordinated Universal Time. + + static uint64_t timeMicros(); + static uint64_t precisionTimeMicros(); + + static bool isBigEndian(); + static void swapBytes(uint8_t* const pBuffer, const uint32_t iSize); + + static int32_t cmd(const std::string& cmd, std::string& result); + static int32_t readFile(const std::string& path, std::string& result); + }; +} + +#endif diff --git a/include/mts/MTS_Text.h b/include/mts/MTS_Text.h new file mode 100644 index 0000000..2380add --- /dev/null +++ b/include/mts/MTS_Text.h @@ -0,0 +1,143 @@ +/* + * 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_Text.h + \brief A set of string formatting utilities + \date 15JUN11 + \author Sean Godinez + + A set of string formatting utilities + */ + +#ifndef MTS_TEXT_H_ +#define MTS_TEXT_H_ + +#include <mts/MTS_NonConstructable.h> +#include <mts/MTS_Stdint.h> +#include <mts/MTS_Buffer.h> +#include <sstream> +#include <string> +#include <vector> +#include <time.h> + +namespace MTS { + + class Text: private NonConstructable { + + public: + + enum DATEFORMAT { + MMsDDsYY, //MM/DD/YYYY + MMsDDsYY_HHcMMcSS, //MM/DD/YYYY HH:MM:SS:mmm + RFC_1123 //Wdy, DD Mon YYYY HH:MM:SS GMT (Thu, 01 Jan 1970 00:00:01 GMT) + }; + + enum TIMEFORMAT { + HHcMMcSScmmm, //HH:MM:SS:mmm + HHcMMcSS //HH:MM:SS + }; + + static const uint8_t RDSTATE_SUCCESS_MASK = 0x02; //Only OEF bit + + static std::string time(const uint64_t& iTimeMicros, const TIMEFORMAT& eTime = HHcMMcSScmmm); + static std::string date(const tm& stTime, const DATEFORMAT& eDate = MMsDDsYY); + static bool datetimeIsBefore(const std::string& time1, const std::string& time2); + + static std::vector<std::string> split(const std::string& str, char delimiter, int limit = 0); + static std::vector<std::string> split(const std::string& str, const std::string& delimiter, int limit = 0); + static std::string join(std::vector<std::string> list, char delimiter); + static std::string replace(const std::string& str, const std::string& from, const std::string& to); + static uint32_t count(const std::string& str, const std::string& element); + static uint32_t count(const std::string& str, const uint8_t& element); + static bool endsWith(const std::string& str, const std::string& key); + + static std::string getLine(const std::string& source, const size_t& start, size_t& cursor); + + static std::string trim(const std::string& str); + static std::string trim(const std::string& str, const uint8_t& element); + static std::string strip(const std::string& str, const std::vector<uint8_t>& elements); + static std::string strip(const std::string& str, const uint8_t& element); + + static std::string toLowerCase(const std::string& str); + static std::string toUpperCase(const std::string& str); + static std::string toCapitalized(const std::string& str); + static std::string toCamelCase(const std::string& str); + static std::string toCommandLineEscaped(const std::string& str); + + static std::wstring widen(const std::string& str); + static std::string narrow(const std::wstring& str); + + static std::string format(bool value); + static std::string format(double value, std::streamsize percision = 6); + + static std::string format(int8_t value); + static std::string format(int16_t value); + static std::string format(int32_t value); + static std::string format(int64_t value); + + static std::string format(uint8_t value); + static std::string format(uint16_t value); + static std::string format(uint32_t value); + static std::string format(uint64_t value); + + static std::string formatHex(int8_t value, bool pad = false); + static std::string formatHex(int16_t value, bool pad = false); + static std::string formatHex(int32_t value, bool pad = false); + static std::string formatHex(int64_t value, bool pad = false); + + static std::string formatHex(uint8_t value, bool pad = false); + static std::string formatHex(uint16_t value, bool pad = false); + static std::string formatHex(uint32_t value, bool pad = false); + static std::string formatHex(uint64_t value, bool pad = false); + + static std::string formatHex(const Buffer& value); + + static bool parse(bool& value, const std::string& str); + static bool parse(double& value, const std::string& str); + + static bool parse(int8_t& value, const std::string& str); + static bool parse(int16_t& value, const std::string& str); + static bool parse(int32_t& value, const std::string& str); + static bool parse(int64_t& value, const std::string& str); + + static bool parse(uint8_t& value, const std::string& str); + static bool parse(uint16_t& value, const std::string& str); + static bool parse(uint32_t& value, const std::string& str); + static bool parse(uint64_t& value, const std::string& str); + + static bool parseHex(uint8_t& value, const std::string& str); + static bool parseHex(uint16_t& value, const std::string& str); + static bool parseHex(uint32_t& value, const std::string& str); + static bool parseHex(uint64_t& value, const std::string& str); + + template<typename S, typename T> + static bool lexicalCast(S source, T& target) { + std::stringstream interpreter; + if(!(interpreter << source) || + !(interpreter >> target) || + !(interpreter >> std::ws).eof()) { + return false; + } + return true; + } + }; +} + +#endif diff --git a/include/mts/MTS_Thread.h b/include/mts/MTS_Thread.h new file mode 100644 index 0000000..7a3472c --- /dev/null +++ b/include/mts/MTS_Thread.h @@ -0,0 +1,105 @@ +/* + * 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_Thread.h + \brief A thread + \date 15JUN11 + \author Sean Godinez + + A platform independent thread. + */ +#ifndef _MTS_THREAD_H_ +#define _MTS_THREAD_H_ + +#include <mts/MTS_AutoPtr.h> +#include <mts/MTS_Lock.h> +#include <mts/MTS_Condition.h> +#include <mts/MTS_Stdint.h> +#include <mts/MTS_NonCopyable.h> +#include <string> + +#ifdef WIN32 +#include <windows.h> +#else +#include <pthread.h> +#endif + +namespace MTS { + + //! An abstract thread + /*! + A platform independent abstract thread class + + \sa Condition, Lock + */ + + class Thread: private NonCopyable { + + public: + + virtual ~Thread(); //!< Destructs thread + + static void sleep(uint32_t iMillis); //!< Pauses calling thread for the given amount of milliseconds + + const std::string& getName() const; //!< Returns the name of thread + + virtual void start(); //!< Starts thread + virtual void stop(); //!< Ends thread + + void wait(); //!< Block until *this thread has finished executing + void wait(uint32_t millis); //!< Waits on signal for *this thread to finish executing or time is up + + bool isAlive() const; //!< Returns true if it is still running + bool isCanceled() const; //!< Returns true if it has been told to shutdown + bool isDone() const; //!< Returns true if it finished running + bool isManaged() const; //!< Returns true if it should not Destruct itself when done running + + protected: + explicit Thread(const std::string& name, bool managed = true); //!< Explicit Constructor + + private: + +#ifdef WIN32 + static DWORD WINAPI doInBackground(__in LPVOID lpParameter); //!< WIN32 function callback + HANDLE m_pThread;//!< WIN32 Thread Handle +#else + static void* doInBackground(void* parameters); //!< PTHREAD function callback + pthread_t* m_pThread; //!< PTHREAD Thread Handle +#endif + + const std::string m_sName; //!< Thread's name + const bool m_bManaged; //!< Self-cleanup flag + bool m_bComplete; //!< Completed flag + bool m_bCanceled; //!< Canceled/Shutdown flag + + AutoPtr<Lock> m_apThreadLock; //!< Thread Lock + AutoPtr<Lock> m_apStateLock; //!< State Lock + AutoPtr<Condition> m_apCompleteCondition; //!< Thread Completed Condition/Signal + + void cancel(); //!< Cancels thread by setting Canceled/Shutdown flag + void done(); //!< Triggers Thread Completed Condition/Signal + void reset(); //!< Resets Canceled and Completed flags + + + virtual void run() = 0; //!< main() of *this Thread + }; +} + +#endif diff --git a/include/mts/MTS_Timer.h b/include/mts/MTS_Timer.h new file mode 100644 index 0000000..3f0b0e5 --- /dev/null +++ b/include/mts/MTS_Timer.h @@ -0,0 +1,66 @@ +/* + * 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_Timer.h + \brief Tracks passage of time + \date Jan 3, 2014 + \author sgodinez + + Useful for tracking time and timeout loops + */ + +#ifndef MTS_TIMER_H_ +#define MTS_TIMER_H_ + +#include <mts/MTS_Stdint.h> + +namespace MTS { + + class Timer { + + public: + Timer(); + virtual ~Timer(); + + void start(); + void stop(); + void pause(); + void resume(); + void reset(); + + bool isRunning(); + + uint64_t getMicros(); + uint64_t getMillis(); + uint64_t getSeconds(); + uint64_t getMinutes(); + + protected: + + private: + uint64_t m_iStart; + uint64_t m_iPause; + bool m_bStarted; + bool m_bPaused; + }; +} + +#endif /* MTS_TIMER_H_ */ diff --git a/include/mts/MTS_TimerThread.h b/include/mts/MTS_TimerThread.h new file mode 100644 index 0000000..28e3e73 --- /dev/null +++ b/include/mts/MTS_TimerThread.h @@ -0,0 +1,77 @@ +/* + * 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_TimerThread.h + \brief An abstract worker thread + \date 10MAY13 + \author Mike Fiore + + A platform independent thread that executes a task every x miliseconds. + Thread can be paused and unpaused. + */ + +#ifndef _MTS_TIMER_THREAD_H_ +#define _MTS_TIMER_THREAD_H_ + +#include <mts/MTS_Thread.h> +#include <mts/MTS_AutoPtr.h> +#include <mts/MTS_Lock.h> + +namespace MTS { + + //! An abstract worker thread + /*! + A platform independent worker thread class + */ + + class TimerThread: public Thread { + + public: + + ~TimerThread(); //!< Destructs the thread + + virtual bool isExecuting() const; //!< Returns true if the thread has been started + + virtual void stopExecution(); //!< Stops running execute() function + + virtual void restartExecution(); //!< Resumes running execute() function + + virtual void updateIntervalMillis(uint32_t newMillis); //!< Updates interval to given value + + protected: + + explicit TimerThread(const std::string& name); //!< Creates the thread + + explicit TimerThread(const std::string& name, uint32_t intervalMillis); //!< Creates the thread + + private: + + void run(); //!< Abstracts the running of the thread + virtual void execute() = 0; //!< main() of the thread which is executed once per interval + void init(); //!< Startup code common to all constructors + + AutoPtr<Lock> m_apStateLock; //!< Condition Lock + bool m_bCurrentlyExecuting; //!< Currently executing flag + bool m_bExecutionStopped; //!< Execution stopped flag + uint32_t m_ui32IntervalMillis; //!< How often the thread executes + }; +} + +#endif |