From 17b117e73df71925d73ee026b4f54aa1867ce0a5 Mon Sep 17 00:00:00 2001 From: Jesse Gilles Date: Mon, 20 Apr 2015 16:49:52 -0500 Subject: initial commit --- include/mts/MTS_AutoLock.h | 56 ++++++++++ include/mts/MTS_AutoPtr.h | 115 ++++++++++++++++++++ include/mts/MTS_BasicPublisher.h | 63 +++++++++++ include/mts/MTS_BasicSubscriber.h | 71 ++++++++++++ include/mts/MTS_Buffer.h | 87 +++++++++++++++ include/mts/MTS_Component.h | 84 +++++++++++++++ include/mts/MTS_Condition.h | 75 +++++++++++++ include/mts/MTS_Lock.h | 76 +++++++++++++ include/mts/MTS_Logger.h | 161 +++++++++++++++++++++++++++ include/mts/MTS_NonConstructable.h | 47 ++++++++ include/mts/MTS_NonCopyable.h | 47 ++++++++ include/mts/MTS_Object.h | 62 +++++++++++ include/mts/MTS_Publisher.h | 130 ++++++++++++++++++++++ include/mts/MTS_Queue.h | 215 +++++++++++++++++++++++++++++++++++++ include/mts/MTS_SharedPtr.h | 137 +++++++++++++++++++++++ include/mts/MTS_SignalThread.h | 74 +++++++++++++ include/mts/MTS_Singleton.h | 77 +++++++++++++ include/mts/MTS_Stdint.h | 104 ++++++++++++++++++ include/mts/MTS_Subscriber.h | 71 ++++++++++++ include/mts/MTS_SubscriberQueue.h | 94 ++++++++++++++++ include/mts/MTS_System.h | 55 ++++++++++ include/mts/MTS_Text.h | 143 ++++++++++++++++++++++++ include/mts/MTS_Thread.h | 105 ++++++++++++++++++ include/mts/MTS_Timer.h | 66 ++++++++++++ include/mts/MTS_TimerThread.h | 77 +++++++++++++ 25 files changed, 2292 insertions(+) create mode 100644 include/mts/MTS_AutoLock.h create mode 100644 include/mts/MTS_AutoPtr.h create mode 100644 include/mts/MTS_BasicPublisher.h create mode 100644 include/mts/MTS_BasicSubscriber.h create mode 100644 include/mts/MTS_Buffer.h create mode 100644 include/mts/MTS_Component.h create mode 100644 include/mts/MTS_Condition.h create mode 100644 include/mts/MTS_Lock.h create mode 100644 include/mts/MTS_Logger.h create mode 100644 include/mts/MTS_NonConstructable.h create mode 100644 include/mts/MTS_NonCopyable.h create mode 100644 include/mts/MTS_Object.h create mode 100644 include/mts/MTS_Publisher.h create mode 100644 include/mts/MTS_Queue.h create mode 100644 include/mts/MTS_SharedPtr.h create mode 100644 include/mts/MTS_SignalThread.h create mode 100644 include/mts/MTS_Singleton.h create mode 100644 include/mts/MTS_Stdint.h create mode 100644 include/mts/MTS_Subscriber.h create mode 100644 include/mts/MTS_SubscriberQueue.h create mode 100644 include/mts/MTS_System.h create mode 100644 include/mts/MTS_Text.h create mode 100644 include/mts/MTS_Thread.h create mode 100644 include/mts/MTS_Timer.h create mode 100644 include/mts/MTS_TimerThread.h (limited to 'include') 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 . + * + */ + +/*! \file MTS_AutoLock.h + \brief Auto lock + \date 08OCT14 + \author Pavlo Samko + + Auto lock. + */ + + +#ifndef _MTS_AUTOLOCK_H_ +#define _MTS_AUTOLOCK_H_ + +#include +#include + +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 . + * + */ + +/*! \file MTS_AutoPtr.h + \brief An autopointer + \date 15JUN11 + \author Sean Godinez + + An autopointer. + */ + +#ifndef MTS_AUTOPTR_H +#define MTS_AUTOPTR_H + +#include +#include + +namespace MTS { + + //! An autopointer + /*! + An autopointer cleans up after itself. + */ + template 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 AutoPtr::AutoPtr(T* ptr, MallocType type) + : m_pObj(ptr), m_eType(type) { + } + + template AutoPtr::~AutoPtr() { + if (m_eType == SINGLE) { + delete m_pObj; + } else { + delete[] m_pObj; + } + } + + template T& AutoPtr::operator*() const { + return *m_pObj; + } + + template T* AutoPtr::operator->() const { + return m_pObj; + } + + template T& AutoPtr::operator[](const uint32_t& index) const { + return m_pObj[index]; + } + + template T* AutoPtr::get() const { + return m_pObj; + } + + template T* AutoPtr::release() { + T* temp = m_pObj; + m_pObj = 0; + return temp; + } + + template void AutoPtr::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 bool AutoPtr::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 . + * + */ + +/*! \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 + +namespace MTS { + + //! A basic publisher + /*! + A basic publisher provides simple publishing functionality. + + \sa Publisher, Subscriber, BasicSubscriber + */ + template class BasicPublisher: public Publisher { + + public: + BasicPublisher(); //!< Constructor + virtual ~BasicPublisher(); //!< Destructor + + virtual void publish(const T& object); //!< Publishes T to Subscribers + }; + + template BasicPublisher::BasicPublisher() { + } + + template BasicPublisher::~BasicPublisher() { + } + + template void BasicPublisher::publish(const T& object) { + Publisher::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 . + * + */ + +/*! \file MTS_BasicSubscriber.h + \brief A simple subscriber + \date 28MAR12 + \author Sean Godinez + + A subscriber. + */ +#ifndef _MTS_BASICSUBSCRIBER_H_ +#define _MTS_BASICSUBSCRIBER_H_ + +#include +#include + +namespace MTS { + + //! A basic subscriber + /*! + A basic subscriber provides simple subscribing functionality. + + \sa Publisher, Subscriber, BasicPublisher + */ + template class BasicSubscriber: public Subscriber { + + public: + explicit BasicSubscriber(const Callback& callback, const std::string& name = ""); //!< Constructor + virtual ~BasicSubscriber();//!< Destructor + + virtual void update(const T& object); //!< Invokes callback with published data T + + private: + Callback* callback; + + //Prevent Copy and Assignment operators from being called + BasicSubscriber(const BasicSubscriber&); + BasicSubscriber& operator=(const BasicSubscriber&); + }; + + template BasicSubscriber::BasicSubscriber(const Callback& callback, const std::string& name) + : Subscriber(name), callback(callback.clone()) { + } + + template BasicSubscriber::~BasicSubscriber() { + delete callback; + } + + template void BasicSubscriber::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 . + * + */ + +/*! \file MTS_Buffer.h + \brief A buffer + \date 15JUN11 + \author Sean Godinez + + A buffer. + */ + +#ifndef MTS_BUFFER_H +#define MTS_BUFFER_H + +#include +#include +#include + +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 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 . + * + */ + +/*! \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 +#include +#include +#include +#include +#include + +namespace MTS { + + template class Component: public Thread { + + public: + virtual ~Component(); + + Subscriber& getSubscriber(); + Publisher

& getPublisher(); + + virtual void stop(); + + protected: + explicit Component(const std::string& sName, uint32_t iCapacity = UINT32_MAX, bool bManaged = true); + + SubscriberQueue m_oSubscriber; + BasicPublisher

m_oPublisher; + + private: + + }; + + template Component::Component( + const std::string& name, uint32_t capacity, bool managed) + : Thread(name, managed), m_oSubscriber(name, capacity) { + } + + template Component::~Component() { + Component::stop(); + } + + template Subscriber& Component::getSubscriber() { + return m_oSubscriber; + } + + template Publisher

& Component::getPublisher() { + return m_oPublisher; + } + + template void Component::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 . + * + */ + +/*! \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 +#include +#include + +#ifdef WIN32 +#include +#else +#include +#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 m_apCondition; //!< Handle to WIN32 Condition +#else + AutoPtr 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 . + * + */ + +/*! \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 +#include +#include + +#ifdef WIN32 +#include +#else +#include +#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 m_apMutex; //!< WIN32 Mutex Handle +#else + AutoPtr m_apMutex; //!< PTHREAD Mutex Handle + AutoPtr 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 . + * + */ + +/*! \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 +#include +#include +#include +#include + +#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 . + * + */ + +/*! \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 . + * + */ + +/*! \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 . + * + */ + +/*! \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 +#include +#include +#include + +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 . + * + */ + +/*! \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 +#include +#include +#include +#include +#include + +namespace MTS { + + //! An abstract publisher + /*! + A template abstract publisher class + + \sa Subscriber + */ + template class Publisher: NonCopyable { + + public: + virtual ~Publisher(); //!< Destructs subscriber + + void addSubscriber(Subscriber& subscriber); //!< Adds a subscriber + void removeSubscriber(Subscriber& subscriber); //!< Removes a subscriber + + protected: + Publisher(); //!< Constructs publisher + virtual void publish(const T& object); //!< Protects publishing operation + + private: + typedef std::set*> SubscriberSet; + + AutoPtr m_apLock; //!< Guards for thread safety + SubscriberSet m_sSubscribers; //!< Set of subscribers + + }; + + template Publisher::Publisher() { + m_apLock.reset(new Lock()); + } + + template Publisher::~Publisher() { + m_apLock.reset(); + } + + template void Publisher::addSubscriber( + Subscriber& subscriber) { + + m_apLock->lock(); + m_sSubscribers.insert(&subscriber); + + /* + + //WARNING IF ALREADY PRESENT IN SET + pair::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 void Publisher::removeSubscriber( + Subscriber& 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 void Publisher::publish(const T& object) { + m_apLock->lock(); + SubscriberSet set(m_sSubscribers); + for (typename SubscriberSet::iterator i = set.begin(); i != set.end(); i++) { + Subscriber* 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 . + * + */ + +/*! \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 +#include +#include +#include +#include +#include +#include + +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 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 m_qContainer; + AutoPtr m_apLock; + AutoPtr m_apNotEmpty; + AutoPtr m_apNotFull; + }; + + template Queue::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 Queue::~Queue() { + m_apNotFull.reset(); + m_apNotEmpty.reset(); + m_apLock.reset(); + } + + template bool Queue::empty() const { + bool result = false; + m_apLock->lock(); + result = m_qContainer.empty(); + m_apLock->unlock(); + return result; + } + + template bool Queue::full() const { + bool result = false; + m_apLock->lock(); + result = (m_qContainer.size() >= m_iMaxSize); + m_apLock->unlock(); + return result; + } + + template uint32_t Queue::size() const { + uint32_t result = 0; + m_apLock->lock(); + result = static_cast(m_qContainer.size()); + m_apLock->unlock(); + return result; + } + + template uint32_t Queue::capacity() const { + return m_iMaxSize; + } + + template void Queue::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 void Queue::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 bool Queue::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 bool Queue::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 bool Queue::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 . + * + */ + +/*! \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 SharedPtr { + + public: + + enum MallocType { + SINGLE, ARRAY + }; + + explicit SharedPtr(T* ptr = 0, MallocType type = SINGLE); //!< Explicitly Constructs the SharedPtr to reference ptr + SharedPtr(const SharedPtr& other); //!< Constructs from another shared pointer, increasing reference count + ~SharedPtr(); //!< Destructs the AutoPtr deleting T + + SharedPtr& operator=(const SharedPtr& 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 SharedPtr::SharedPtr(T* ptr, MallocType type) + : m_pObj(ptr), m_pReferences(new int(1)), m_eType(type) { + } + + template SharedPtr::SharedPtr(const SharedPtr& other) + : m_pObj(other.m_pObj), m_pReferences(other.m_pReferences) { + *(m_pReferences) += 1; + } + + template SharedPtr::~SharedPtr() { + release(); + } + + template SharedPtr& SharedPtr::operator=( + const SharedPtr& 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 T& SharedPtr::operator*() const { + return *m_pObj; + } + + template T* SharedPtr::operator->() const { + return m_pObj; + } + + template T& SharedPtr::operator[](const uint32_t& index) const { + return m_pObj[index]; + } + + template T* SharedPtr::get() const { + return m_pObj; + } + + template bool SharedPtr::isNull() const { + return (m_pObj == 0); + } + + template bool SharedPtr::isShared() const { + return ((*m_pReferences) > 1); + } + + template int SharedPtr::count() const { + return (*m_pReferences); + } + + template void SharedPtr::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 . + * + */ + +/*! \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 +#include +#include +#include + +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 m_apStateLock; //!< Condition Lock + AutoPtr m_apConditionLock; //!< Condition Lock + AutoPtr 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 . + * + */ + +/*! \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 + +namespace MTS { + + template class Singleton { + + public: + + static T* instance(); + + protected: + + private: + static T* m_pInstance; + static AutoPtr m_apLock; + + // Do Not Allow Use of Any Constructor or Assignment Operator + Singleton() { + } + ~Singleton(); + Singleton(const Singleton&); + Singleton& operator=(const Singleton&); + }; + + template Singleton::~Singleton() { + m_apLock.reset(); + delete T; + } + + template T* Singleton::instance() { + + m_apLock->lock(); + if (m_pInstance == NULL) { + m_pInstance = new T(); + } + m_apLock->unlock(); + + return m_pInstance; + } + + //FIXME: Template Initialization + template static AutoPtr Singleton::m_apLock.reset(new Lock()); + template static T* Singleton::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 . + * + */ + +/*! \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 +#else +#include +#endif +#else +#include +#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 . + * + */ + +/*! \file MTS_Subscriber.h + \brief An abstract subscriber + \date 28MAR12 + \author Sean Godinez + + A subscriber. + */ +#ifndef _MTS_SUBSCRIBER_H_ +#define _MTS_SUBSCRIBER_H_ + +#include +#include + +namespace MTS { + + //! An abstract subscriber + /*! + A template abstract subscriber class + + \sa Publisher + */ + template 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 Subscriber::Subscriber(const std::string& name) + : m_sName(name) { + } + + template Subscriber::~Subscriber() { + } + + template const std::string& Subscriber::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 . + * + */ + +/*! \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 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 . + * + */ + +/*! \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 +#include +#include + +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 . + * + */ + +/*! \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 +#include +#include +#include +#include +#include +#include + +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 split(const std::string& str, char delimiter, int limit = 0); + static std::vector split(const std::string& str, const std::string& delimiter, int limit = 0); + static std::string join(std::vector 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& 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 + 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 . + * + */ + +/*! \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 +#include +#include +#include +#include +#include + +#ifdef WIN32 +#include +#else +#include +#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 m_apThreadLock; //!< Thread Lock + AutoPtr m_apStateLock; //!< State Lock + AutoPtr 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 . + * + */ + +/*! + \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 + +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 . + * + */ + +/*! \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 +#include +#include + +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 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 -- cgit v1.2.3