Initial commit

This commit is contained in:
Roland Winklmeier
2013-02-15 18:50:17 +01:00
parent 57214c1c1e
commit 584251e0a8
125 changed files with 10640 additions and 0 deletions

View File

@@ -0,0 +1 @@
FILE(GLOB HEADERS *.h)

View File

@@ -0,0 +1,117 @@
//! Copyright (C) 2013 Roland Winklmeier
//! This Source Code Form is subject to the terms of the Mozilla Public
//! License, v. 2.0. If a copy of the MPL was not distributed with this
//! file, You can obtain one at http://mozilla.org/MPL/2.0/
#ifndef COM_CLIENT_H
#define COM_CLIENT_H
#include <QString>
#include <QAbstractSocket>
#include "blackmisc/com_handler.h"
class QTcpSocket;
namespace BlackMisc
{
class CComClient : public IComHandler
{
Q_OBJECT
public:
//! Constructor
/*!
\param parent Pointer to the parent QObject
*/
CComClient(QObject *parent = 0);
//! Destructor
~CComClient();
//! This method initializes the client
virtual bool init();
//! Connects to the host address.
/*!
\param host Host address of the remote host.
\param port Port number of the remote host.
\return Returns true if connecting was successfull, otherwise false.
*/
void connectTo (const QString& hostName, quint16 port);
//! Returns the connection status
/*!
\return Returns true if connected, otherwise false
*/
bool isConnected() const;
//! Disconnects from the current server
void disconnectFrom();
//! Sends a message to the remote host.
/*!
\param data Reference to the raw byte data to be sent.
\return Returns true if sending was successfull, otherwise false.
*/
bool sendMessage (const QString &messageID, const QByteArray &message);
//! Returns a human readable description of the last error that occurred.
QString getErrorMessage(QAbstractSocket::SocketError error);
signals:
//! emitted when new data were received
void doReceivedMessage(QString &messageID, QByteArray &message);
//! emitted when cliebt connected
void doConnected();
//! emitted when client disconnected
void doDisconnected();
//! emitted when an error has occured
void doError(QAbstractSocket::SocketError error, const QString& error_message);
protected slots:
//! Call this slot, when connected succesfully
void onConnected();
//! Call this slot, when disconnected succesfully
void onDisconnected();
//! Call this slot, when an error appeared
void onError(QAbstractSocket::SocketError error);
//! Call this slot, when data has been received
void onReceivingData();
protected:
//! TCP Socket
/*!
Pointer to the tcp socket.
*/
QTcpSocket* m_tcp_socket;
//! Remote hostname
QString m_hostName;
//! Remote host port
quint16 m_port;
//! This variable holds the last appeared error
QString m_last_error;
private:
CComClient( const CComClient& other);
const CComClient& operator = ( const CComClient& other);
};
} // namespace BlackMisc
#endif // COM_CLIENT_H

View File

@@ -0,0 +1,53 @@
//! Copyright (C) 2013 Roland Winklmeier
//! This Source Code Form is subject to the terms of the Mozilla Public
//! License, v. 2.0. If a copy of the MPL was not distributed with this
//! file, You can obtain one at http://mozilla.org/MPL/2.0/
#ifndef COM_CLIENT_BUFFER_H
#define COM_CLIENT_BUFFER_H
#include <QObject>
#include "blackmisc/com_handler.h"
class QTcpSocket;
namespace BlackMisc
{
class CComClientBuffer : public IComHandler
{
Q_OBJECT
public:
CComClientBuffer(uint clientID, QTcpSocket *socket, QObject *parent = 0);
virtual ~CComClientBuffer();
//! Sends a message to the remote host.
/*!
\param data Reference to the raw byte data to be sent.
\return Returns true if sending was successfull, otherwise false.
*/
bool sendMessage (const QString &id, const QByteArray &message);
signals:
void doReceivedMessage(uint clientID, QString& messageID, QByteArray &message);
void doDisconnected(uint clientID);
protected slots:
void onReceivingData();
void onClientDisconnected();
protected:
QTcpSocket* m_tcp_socket;
uint m_client_id;
};
} // namespace BlackMisc
#endif // COM_CLIENT_BUFFER_H

View File

@@ -0,0 +1,72 @@
//! Copyright (C) 2013 Roland Winklmeier
//! This Source Code Form is subject to the terms of the Mozilla Public
//! License, v. 2.0. If a copy of the MPL was not distributed with this
//! file, You can obtain one at http://mozilla.org/MPL/2.0/
#ifndef COM_HANDLER_H
#define COM_HANDLER_H
#include <QObject>
class QTcpSocket;
const qint32 Sync_Marker = 0x1ACFFC1D;
namespace BlackMisc
{
//! IComHandler Interface.
/*!
This interface implements the basic class for every InterCommunikation
objects. It creates the frames in which the data is packed and
deframes it, when it receives something from the TCP socket.
\sa CComClient CComServer
*/
class IComHandler : public QObject
{
Q_OBJECT
public:
//! Constructor
/*!
\param parent Pointer to the parent QObject
*/
explicit IComHandler(QObject *parent = 0);
//! Virtual destructor
virtual ~IComHandler() {}
protected:
//! Creates a sendable frame containing some data.
/*!
\param messageID QString with the unique messageID
\param message The actual data
\sa IMessage
*/
void createFrame (const QString &messageID, const QByteArray &data);
//! Parses a new frame and constructs messageID and data out of it
/*!
\param messageID Reference to the QString messageID
\param message Reference where the data will be stored in.
\sa IMessage
*/
bool parseFrame(QString &messageID, QByteArray &data);
//! Receive Buffer
/*!
Data received from the TCP socket, will stored in here.
*/
QByteArray m_receive_buffer;
//! Sender Buffer
/*!
Sending data via the TCP socket, should be stored in here.
*/
QByteArray m_sender_buffer;
};
} // namespace BlackMisc
#endif // COM_HANDLER_H

View File

@@ -0,0 +1,104 @@
//! Copyright (C) 2013 Roland Winklmeier
//! This Source Code Form is subject to the terms of the Mozilla Public
//! License, v. 2.0. If a copy of the MPL was not distributed with this
//! file, You can obtain one at http://mozilla.org/MPL/2.0/
#ifndef TCP_SERVER_H
#define TCP_SERVER_H
#include <QHash>
#include <QHostAddress>
#include <QTcpSocket>
class QTcpServer;
class CComClientBuffer;
namespace BlackMisc
{
class CComServer : public QObject
{
Q_OBJECT
public:
//! Constructor
/*!
\param parent Pointer to the parent QObject
*/
CComServer(QObject *parent = 0);
//! Destructor
~CComServer();
//! This method initializes the server
/*!
\return Returns true, if successfull, otherwise false
*/
bool init ();
//! Call this method to start hosting
void Host ( const QHostAddress & address, const quint16 port);
//! Hosting status
/*!
\return Returns true if hosting, otherwise false
*/
bool isHosting () const;
//! Closes the opened hosting session
void close();
//! Sends data to a client
/*!
\param clientID ID of the target client
\param messageID ID of the message, to be sent to the client
\param data The actual data/message
*/
void sendToClient(const quint32 clientID, const QString &messageID, const QByteArray& data);
//! Sends data to all clients
/*!
\param messageID ID of the message, to be sent to the client
\param data The actual data/message
*/
void sendToAll(const QString &messageID, const QByteArray& data);
//! Call this method to get last error
/*!
\return Returns a string representation of the last error.
*/
QString getErrorMessage( const QAbstractSocket::SocketError error);
signals:
void doHostClosed();
void doHosting();
void doClientConnected();
void doClientDisconnected();
void doMessageReceived(QString &, QByteArray &);
void doMessageReceived();
protected slots:
void onIncomingConnection();
void onClientDisconnected(uint clientID);
void onClientMessageReceived(uint clientHash, QString &messageID, QByteArray &message);
protected:
QTcpServer* m_tcp_server;
QHostAddress m_address;
quint16 m_port;
typedef QHash<uint, CComClientBuffer*> TClientBufferHash;
TClientBufferHash m_client_buffers;
};
} // namespace BlackMisc
#endif // TCP_SERVER_H

View File

@@ -0,0 +1,162 @@
//! Copyright (C) 2013 Roland Winklmeier
//! This Source Code Form is subject to the terms of the Mozilla Public
//! License, v. 2.0. If a copy of the MPL was not distributed with this
//! file, You can obtain one at http://mozilla.org/MPL/2.0/
#ifndef CONFIG_H
#define CONFIG_H
#include <QtGlobal>
#include <QMap>
namespace BlackMisc
{
class CValue
{
public:
//! Configuration type enum.
/*! This enum lists the three different values, used in the config file. */
typedef enum { CONFIG_STRING = 0, /*!< Type String. */
CONFIG_INT, /*!< Type Integer. */
CONFIG_DOUBLE, /*!< Type Double. */
CONFIG_BOOL, /*!< Type Bool. */
CONFIG_UNKOWN /*!< Type Unknown. */
} TConfigType;
CValue ();
CValue (const QString& value);
void init();
QString& asString() { return _value; }
qint32 asInt( bool* ok = NULL );
bool asBool(bool* ok = NULL );
double asDouble( bool* result );
inline bool isValid() {return _type != CONFIG_UNKOWN;}
inline bool isInt() { return _type == CONFIG_INT; }
inline bool isDouble() { return _type == CONFIG_DOUBLE; }
inline bool isBool() { return _type == CONFIG_BOOL; }
protected:
QString _value;
TConfigType _type;
qint32 _int_value;
double _double_value;
bool _bool_value;
};
//! Configuration class.
/*!
This class implements the configuration part of the library.
*/
class CConfig
{
public:
CConfig(const QString& filename, const QString& separator = "=", bool isRelative = false);
//! Sets the value of the specified key.
/*!
\param key Key, which value should be set.
\param value The actual value as T.
*/
void setValue(const QString& key, const CValue& value);
//! Returns the value from key.
/*!
\param key Specified key.
\return The value to key 'key' as T&
*/
CValue value(const QString& key) const;
//! Function to check if the key is in the config.
/*!
\param key Specified key.
\return Returns true, if key is in the config.
*/
void add (const QString& key, const CValue& value);
//! Function to check if the key is in the config.
/*!
\param key Specified key.
\return Returns true, if key is in the config.
*/
void update (const QString& key, const CValue& value);
//! Function to check if the key is in the config.
/*!
\param key Specified key.
\return Returns true, if key is in the config.
*/
bool contains (const QString& key) const;
//! Removes the key and its value from the config.
/*!
\param key Key, which has to be removed.
*/
void remove (const QString& key);
//! Displays the configuration to the debug output.
/*!
More info.
*/
void display ();
/*!
* Config File section
*/
//! Returns the config filename of this config object.
/*!
\return filename of this config.
*/
const QString& configFile() const {return m_configfile; }
//! Loads the config file.
/*!
\return Returns true if loading went well.
*/
bool load ();
//! Loads the config file.
/*!
\param filename Specify a different filename.
\return Returns true if loading went well.
*/
bool load (const QString& filename);
//! Saves the config to file.
/*!
\return Returns true if loading went well.
*/
bool save() {} // TODO
//! Saves the config to file.
/*!
\param filename Specify a different filename.
\return Returns true if loading went well.
*/
bool save(const QString& filename) {} // TODO
protected:
QString m_configfile;
QString m_separator;
typedef QMap<QString, CValue> TValueMap;
TValueMap m_value_map;
};
} //! namespace BlackMisc
#endif // CONFIG_H

View File

@@ -0,0 +1,59 @@
//! Copyright (C) 2013 Roland Winklmeier
//! This Source Code Form is subject to the terms of the Mozilla Public
//! License, v. 2.0. If a copy of the MPL was not distributed with this
//! file, You can obtain one at http://mozilla.org/MPL/2.0/
#ifndef CONFIGMANAGER_H
#define CONFIGMANAGER_H
#include <QMap>
#include <QString>
#include "blackmisc/context.h"
namespace BlackMisc
{
class CConfig;
class CConfigManager
{
// safe singleton declaration
SINGLETON_CLASS_DECLARATION(CConfigManager)
CConfigManager();
public:
//! Configuration Manager error codes.
/*! This enum lists all errors, which can appear. If you need
* a readable output then consider using /sa getErrorString()
*/
typedef enum { UNKNOWN_PATH = 0, /*!< Type String. */
} TErrorCodes;
//! Sets the global path, where all the config files are located
/*!
\param path absolute pathname
*/
void setConfigPath(QString &path);
int readConfig(bool forceReload = false);
int writeConfig();
void clear();
CConfig *getConfig(const QString &section);
private:
typedef QMap<QString, CConfig*> TConfigMap;
TConfigMap m_config_map;
QString m_config_path;
};
} // namespace BlackLib
#endif // CONFIGMANAGER_H

View File

@@ -0,0 +1,249 @@
//! Copyright (C) 2013 Roland Winklmeier
//! This Source Code Form is subject to the terms of the Mozilla Public
//! License, v. 2.0. If a copy of the MPL was not distributed with this
//! file, You can obtain one at http://mozilla.org/MPL/2.0/
#ifndef CONTEXT_H
#define CONTEXT_H
#include <QMap>
namespace BlackMisc
{
class CLog;
class CDebug;
//! IContext Interface.
/*!
This interface abstracts any application and library context. Use it to
add singleton classes and for application and library wide logging.
*/
class IContext
{
public:
//! Reference to IConext.
/*!
This static function returns a reference to the singleton object.
Use always this method to retrieve the context, since you cannot
instantiate your own one. Before it can be used you have to create
either a CApplicationContext or a CLibraryContext in the very
beginning of the executable/libary.
*/
static IContext &getInstance();
//! Returns true if the context is initialized and ready.
/*!
\return Returns true if context is initialized, otherwise false.
*/
static bool isInitialised();
//! Destructor.
/*!
Destroys the context.
*/
virtual ~IContext();
//! Pure virtual method returns the pointer to singletone object by its name.
/*!
\param singletonName a reference to the singletones name.
\return Returns a pointer to the singleton object.
*/
virtual void *singletonPointer(const QString &singletonName) = NULL;
//! Pure virtual method sets the singletone pointer, given by its name.
/*!
\param singletonName a reference to the singletones name.
\param object a pointer to the singletone object.
*/
virtual void setSingletonPointer(const QString &singletonName, void *object) = NULL;
//! Deallocates the object and removes the singletone pointer from the context map.
/*!
\param singletonName a reference to the singletones name.
\param object a pointer to the singletone object.
*/
virtual void releaseSingletonPointer(const QString &singletonName, void *object) = NULL;
//! Pure virtual method returns the pointer to debug object
/*!
\return Pointer to CDebug object
*/
virtual CDebug *getDebug() = NULL;
//! Pure virtual function to set the global error log object
/*!
\param Pointer to CDebug object
*/
virtual void setDebug(CDebug *debug) = NULL;
protected:
//! Method to register the context.
/*! This method should be called by the derived class. It sets the
context variable to the calling object.*/
/*!
\param Pointer to CLog object
*/
void registerContext();
//! Pointer to the global context.
/*!
This variable holds a pointer to the application context.
*/
static IContext *m_context;
//! Pointer to the global Debug object.
/*!
This member variable contains all logging types, not only debug.
*/
CDebug *m_debug;
};
//! CApplicationContext.
/*!
This class implements the IContext interface for applications.
*/
class CApplicationContext : public IContext
{
public:
CApplicationContext();
//! This method returns the pointer to singletone object by its name.
/*!
\param singletonName a reference to the singletones name.
\return Returns a pointer to the singleton object.
*/
virtual void *singletonPointer(const QString &singletonName);
//! Sets the singletone pointer, given by its name.
/*!
\param singletonName a reference to the singletones name.
\param object a pointer to the singletone object.
*/
virtual void setSingletonPointer(const QString &singletonName, void *object);
//! Deallocates the object and removes the singletone pointer from the context map.
/*!
\param singletonName a reference to the singletones name.
\param object a pointer to the singletone object.
*/
virtual void releaseSingletonPointer(const QString &singletonName, void *ptr);
//! Pure virtual method returns the pointer to debug object
/*!
\return Pointer to CDebug object
*/
virtual CDebug *getDebug();
//! Pure virtual function to set the global error log object
/*!
\param Pointer to CDebug object
*/
virtual void setDebug(CDebug *debug);
private:
//! Typedef for the singleton map
typedef QMap<QString, void*> TSingletonMap;
//! Map of all registered objects inside the application.
/*!
This map holds associates all registered singleton objects to their names.
*/
TSingletonMap m_singletonObjects;
};
//! CApplicationContext.
/*!
This class implements the IContext interface for libraries.
*/
class CLibraryContext : public IContext
{
public:
CLibraryContext (IContext &application);
//! This method returns the pointer to singletone object by its name.
/*!
\param singletonName a reference to the singletones name.
\return Returns a pointer to the singleton object.
*/
virtual void *singletonPointer(const QString &singletonName);
//! Sets the singletone pointer, given by its name.
/*!
\param singletonName a reference to the singletones name.
\param object a pointer to the singletone object.
*/
virtual void setSingletonPointer(const QString &singletonName, void *ptr);
//! Deallocates the object and removes the singletone pointer from the context map.
/*!
\param singletonName a reference to the singletones name.
\param object a pointer to the singletone object.
*/
virtual void releaseSingletonPointer(const QString &singletonName, void *ptr);
//! Pure virtual method returns the pointer to debug object
/*!
\return Pointer to CDebug object
*/
virtual CDebug *getDebug();
//! Pure virtual function to set the global error log object
/*!
\param Pointer to CDebug object
*/
virtual void setDebug(CDebug *debug);
private:
//! Pointer the application context.
/*!
Libraries do not have their own context, because they are using
the one from the linked application.
*/
IContext *m_applicationContext;
};
/*!
This macros helps to create the body of a singletone class,
which registers itself with the application context.
*/
#define SINGLETON_CLASS_DECLARATION(className) \
private:\
/* Singletone class constructor */ \
className (const className &) {}\
/* the local static pointer to the singleton instance */ \
static className *m_instance; \
public:\
static className &getInstance() \
{ \
if (m_instance == NULL) \
{ \
/* We need a valid context.*/ \
bAssertstr(BlackMisc::IContext::isInitialised(), QString("You are trying to access a singleton without having initialized a context. The simplest correction is to add 'BlackMisc::CApplicationContext myApplicationContext;' at the very beginning of your application.")); \
\
/* Get the singleton object from the context, if there is one */ \
void *object = BlackMisc::IContext::getInstance().singletonPointer(#className); \
if (object == NULL) \
{ \
/* We have no allocated object yet, so do it now. */ \
m_instance = new className; \
BlackMisc::IContext::getInstance().setSingletonPointer(#className, m_instance); \
} \
else \
{ \
/* Always use the c++ methods instead of casting the C way. */ \
m_instance = reinterpret_cast<className*>(object); \
} \
} \
return *m_instance; \
} \
private:
/* Put your class constructor directly after this makro and make sure it is private! */
#define SINGLETON_CLASS_IMPLEMENTATION(className) className *className::m_instance = NULL;
} // namespace BlackMisc
#endif CONTEXT_H

View File

@@ -0,0 +1,147 @@
//! Copyright (C) 2013 Roland Winklmeier
//! This Source Code Form is subject to the terms of the Mozilla Public
//! License, v. 2.0. If a copy of the MPL was not distributed with this
//! file, You can obtain one at http://mozilla.org/MPL/2.0/
#ifndef DEBUG_H
#define DEBUG_H
#include "blackmisc/context.h"
#include "blackmisc/log.h"
#include "blackmisc/display.h"
#include "blackmisc/logmessage.h"
// This header contains debug macros
namespace BlackMisc
{
//! class CDebug
/*! This class implements the logging of the library. It holds a list of displays
takes care of any additional information needed, e.g. timestamps, filename etc.
To use the logging use the default displayer or implement your own one and
register it with this class.
*/
class CDebug
{
public:
CDebug();
// internal use only
void create (QString &logPath = QString(""), bool logInFile = true, bool eraseLastLog = false);
// init Debug
void init(bool logInFile);
/// Do not call this, unless you know what you're trying to do (it kills debug)!
void destroy() {}
void changeLogDirectory(const QString &dir) {}
QString getLogDirectory();
// Assert function
//! Method returns the pointer to the global assert log object
/*!
\return Pointer to CLog object
*/
CLog *getAssertLog();
//! Pure virtual function to set the global assert log object
/*!
\param Pointer to CLog object
*/
void setAssertLog(CLog *assertLog);
//! Pure virtual method returns the pointer to the global error log object
/*!
\return Pointer to CLog object
*/
CLog *getErrorLog();
//! Pure virtual function to set the global error log object
/*!
\param Pointer to CLog object
*/
void setErrorLog(CLog *errorLog);
//! Pure virtual method returns the pointer to the global warning log object
/*!
\return Pointer to CLog object
*/
CLog *getWarningLog();
//! Pure virtual function to set the global warning log object
/*!
\param Pointer to CLog object
*/
void setWarningLog(CLog *warningLog);
//! Pure virtual method returns the pointer to the global info log object
/*!
\return Pointer to CLog object
*/
CLog *getInfoLog();
//! Pure virtual function to set the global info log object
/*!
\param Pointer to CLog object
*/
void setInfoLog(CLog *infoLog);
//! Pure virtual method returns the pointer to the global debug log object
/*!
\return Pointer to CLog object
*/
CLog *getDebugLog();
//! Pure virtual function to set the global debug log object
/*!
\param Pointer to CLog object
*/
void setDebugLog(CLog *debugLog);
void assertFailed(int line, const char *file, const char* function, const char *exp);
void assertFailedString(int line, const char *fileName, const char *methodName, const char* exp, const char* string);
CLogMessage blackInfo(int line, const char *fileName, const char *methodName);
CLogMessage blackWarning(int line, const char *fileName, const char *methodName);
CLogMessage blackDebug(int line, const char *fileName, const char *methodName);
CLogMessage blackError(int line, const char *fileName, const char *methodName);
CLogMessage blackAssert(int line, const char *fileName, const char *methodName);
CLogMessage blackAssertqstr(int line, const char *fileName, const char *methodName);
private:
bool m_isInitialized;
CStdDisplay *stdDisplayer;
CFileDisplay *fileDisplayer;
QString m_logPath;
CLog *m_errorLog;
CLog *m_warningLog;
CLog *m_infoLog;
CLog *m_debugLog;
CLog *m_assertLog;
};
/*!
Macro is not defined in VC6
*/
#if _MSC_VER <= 1200
# define __FUNCTION__ NULL
#endif
#define bInfo ( BlackMisc::IContext::getInstance().getDebug()->blackInfo(__LINE__, __FILE__, __FUNCTION__ ) )
#define bWarning ( BlackMisc::IContext::getInstance().getDebug()->blackWarning(__LINE__, __FILE__, __FUNCTION__ ) )
#define bDebug ( BlackMisc::IContext::getInstance().getDebug()->blackDebug(__LINE__, __FILE__, __FUNCTION__ ) )
#define bError ( BlackMisc::IContext::getInstance().getDebug()->blackError(__LINE__, __FILE__, __FUNCTION__ ) )
#define bAssert(exp) if (!(exp)) BlackMisc::IContext::getInstance().getDebug()->assertFailed(__LINE__, __FILE__, __FUNCTION__, #exp)
#define bAssertstr(exp, str) if (!(exp)) BlackMisc::IContext::getInstance().getDebug()->assertFailedString(__LINE__, __FILE__, __FUNCTION__, #exp, #str)
} // namespace BlackMisc
#endif DEBUG_H // DEBUG_H

View File

@@ -0,0 +1,95 @@
//! Copyright (C) 2013 Roland Winklmeier
//! This Source Code Form is subject to the terms of the Mozilla Public
//! License, v. 2.0. If a copy of the MPL was not distributed with this
//! file, You can obtain one at http://mozilla.org/MPL/2.0/
#ifndef DISPLAY_H
#define DISPLAY_H
#include <QFile>
#include <QMutex>
#include "blackmisc/log.h"
namespace BlackMisc
{
class ILogDisplay
{
public:
/// Constructor
ILogDisplay(const QString &displayName );
/// Destructor
virtual ~ILogDisplay();
/// Display the string where it does.
void print( const CLog::SLogInformation &logInformation, const QString &message );
/// This is the identifier for a displayer, it is used to find or remove a displayer
QString DisplayName;
protected:
/// Method to implement in the derived class
virtual void doPrint(const CLog::SLogInformation &logInformation, const QString &message) = NULL;
// Return the header string with date (for the first line of the log)
static QString headLine ();
public:
/// Convert log type to string
static const char *logTypeToString (CLog::TLogType logType);
/// Convert the current date to human string
static QString currentDateToString ();
/// Convert date to "2000/01/14 10:05:17" string
static QString dateToString (const QDateTime &time);
private:
QMutex *m_mutex;
};
class CStdDisplay : virtual public ILogDisplay
{
public:
CStdDisplay ( const QString &displayerName = QString("")) : ILogDisplay (displayerName) {}
protected:
/// Display the string to stdout and OutputDebugString on Windows
virtual void doPrint (const CLog::SLogInformation &logInformation, const QString &message);
};
class CFileDisplay : virtual public ILogDisplay
{
public:
/// Constructor
CFileDisplay (const QString &filename, bool eraseLastLog = false, const QString &displayName = "");
CFileDisplay ();
~CFileDisplay ();
/// Set Parameter of the displayer if not set at the ctor time
void setParam (const QString &filename, bool eraseLastLog = false);
protected:
/// Put the string into the file.
virtual void doPrint (const CLog::SLogInformation &logInformation, const QString &message);
private:
QString m_fileName;
QFile *m_file;
bool m_needHeader;
};
} // BlackMisc
#endif // DISPLAY_H

View File

@@ -0,0 +1,115 @@
//! Copyright (C) 2013 Roland Winklmeier
//! This Source Code Form is subject to the terms of the Mozilla Public
//! License, v. 2.0. If a copy of the MPL was not distributed with this
//! file, You can obtain one at http://mozilla.org/MPL/2.0/
#ifndef GUI_MESSAGES_H
#define GUI_MESSAGES_H
#include <blackmisc/message.h>
namespace BlackMisc
{
class MSG_CONNECT_TO_VATSIM : public IMessage
{
public:
MSG_CONNECT_TO_VATSIM() : IMessage(QString("MSG_ID_CONNECT_TO_VATSIM"))
{
}
QString getHost () const { return m_host; }
quint16 getPort () const { return m_port; }
QString getCallsign () const { return m_callsign; }
QString getUserID () const { return m_userid; }
QString getPassword () const { return m_password; }
QString getRealName () const { return m_realName; }
void setHost (const QString &host) { m_host = host; }
void setPort (const quint16 &port) { m_port = port; }
void setCallsign (const QString &callsign) { m_callsign = callsign; }
void setUserID (const QString &id) { m_userid = id; }
void setPassword (const QString &password) { m_password = password; }
void setRealName (const QString &realname) { m_realName = realname; }
virtual QDataStream& operator<< ( QDataStream& in)
{
in >> m_message_id;
in >> m_host;
in >> m_port;
in >> m_callsign;
in >> m_userid;
in >> m_password;
in >> m_realName;
return in;
}
virtual QDataStream& operator>> (QDataStream& out) const
{
out << m_message_id;
out << m_host;
out << m_port;
out << m_callsign;
out << m_userid;
out << m_password;
out << m_realName;
return out;
}
virtual QTextStream& operator<< ( QTextStream& in) { return in; }
virtual QTextStream& operator>> (QTextStream& out) const { return out; }
protected:
private:
QString m_host;
quint16 m_port;
QString m_callsign;
QString m_userid;
QString m_password;
QString m_realName;
};
class MSG_CHAT_MESSAGE : public IMessage
{
public:
MSG_CHAT_MESSAGE() : IMessage(QString("MSG_ID_CHAT_MESSAGE"))
{
}
void setSource (const QString &source) { m_source = source; }
void setDestination (const QString &destination) { m_destination = destination; }
void setText (const QString &text) { m_source = text; }
QString getSource() const {return m_source;}
QString getDestination() const {return m_destination;}
QString getText() const {return m_text;}
virtual QDataStream& operator<< ( QDataStream& in)
{
in >> m_message_id;
in >> m_source;
in >> m_destination;
return in;
}
virtual QDataStream& operator>> (QDataStream& out) const
{
out << m_message_id;
out << m_source;
out << m_destination;
return out;
}
virtual QTextStream& operator<< ( QTextStream& in) { return in; }
virtual QTextStream& operator>> (QTextStream& out) const { return out; }
protected:
private:
QString m_source;
QString m_destination;
QString m_text;
};
}
#endif // GUI_MESSAGES_H

View File

@@ -0,0 +1,204 @@
//! Copyright (C) 2013 Roland Winklmeier
//! This Source Code Form is subject to the terms of the Mozilla Public
//! License, v. 2.0. If a copy of the MPL was not distributed with this
//! file, You can obtain one at http://mozilla.org/MPL/2.0/
#ifndef LOG_H
#define LOG_H
#include <QMutex>
#include <QList>
#include <QString>
#include <QDateTime>
namespace BlackMisc {
class ILogDisplay;
//! Logging class
/*! This class implements the logging of the library. It holds a list of displays
takes care of any additional information needed, e.g. timestamps, filename etc.
To use the logging use the default displayer or implement your own one and
register it with this class.
*/
class CLog
{
public:
//! Logging type.
/*! This enum holds all different available log types. */
typedef enum { OFF = 0,
ERROR,
WARNING,
INFO,
DEBUG,
ASSERT,
UNKNOWN } TLogType;
//! SLogParameter
/*!
This structs capsulates all information needed to output a log line.
*/
struct SLogInformation
{
SLogInformation() : m_logType(CLog::OFF), m_threadId(0), m_line(-1), m_sourceFile(NULL), m_methodName(NULL) {}
QDateTime m_dateTime;
TLogType m_logType;
QString m_applicationName;
quint32 m_threadId;
const char *m_sourceFile;
qint32 m_line;
const char *m_methodName;
};
CLog (TLogType logType = OFF);
//! CLog destructor.
virtual ~CLog(void);
/*!
* Display
*/
//! This method adds a Displayer to the Log object
/*! CLog does not own the pointer. It is the callers responsibility
to allocate the displayer and remove it after using. */
/*!
\param display A pointer to a display.
\sa ILogDisplay
*/
void attachDisplay (BlackMisc::ILogDisplay *display);
//! This method returns the pointer to a registered display.
/*!
\return display A pointer to a display.
*/
ILogDisplay *getDisplay (const QString &displayName);
//! Removes a display
/*!
\param logDisplay Pointer to the display.
*/
void dettachDisplay (ILogDisplay *logDisplay);
//! Removes a display by its name.
/*!
\param displayName Name of the display.
*/
void dettachDisplay (const QString& displayName);
//! Checks if the displayer is added
/*!
\param logDisplay Pointer to a display.
\return Returns true if display is attached, otherwise false.
*/
bool isAttached (ILogDisplay *logDisplay) const;
//! Checks if the object has any attached displays
/*!
\return Returns true if no display is attached, otherwise false.
*/
bool hasNoDisplays () const { return m_logDisplays.empty(); }
//! Sets the name of the application process
/*!
\param displayName Name of the application.
*/
static void setApplicationName (const QString &displayName);
//! Sets the default application name. This is the name
//! of the executable.
static void setDefaultApplicationName ();
//! Sets any additional information as prefix to the log message
/*!
\param fileName This is the name of the corresponding source file.
\param methodName This is the name of calling method.
*/
void setLogInformation (int line, const char *fileName, const char *methodName = NULL);
//! Prints the message and adds an new line character at the end
/*!
\param message Message string, which has to be logged
*/
void printWithNewLine( QString &message );
//! Prints the message as is and appends no additional characters
/*!
\param message Message string, which has to be logged
*/
void print( QString &message );
void printString ( QString &message );
protected:
/// Display a string in decorated form to all attached displayers.
void displayString (const char *str);
/// Symetric to setPosition(). Automatically called by display...(). Do not call if noDisplayer().
void resetLogInformation();
//! Logging Type.
/*!
Specifies which logging type should be used.
Possible are: Error, Warning, Info, Debug and Assert.
*/
TLogType m_logType;
//! Application name.
/*!
Specifies the name of the application to put it into the log line
*/
static QString *m_applicationName;
//! Source file name.
/*!
Specifies the name of the source file, log message was called from.
*/
const char *m_sourceFile;
//! Code line.
/*!
Specifies the line in the source file, log message was called from.
*/
qint32 m_line;
//! Method name.
/*!
Specifies the method in the source file, log message was called from.
*/
const char *m_methodName;
/*!
\typedef TLogDisplayerMap
QList container holding pointers to ILogDisplay objects
\sa ArrayList::GetEnumerator, \sa List::GetEnumerator
*/
typedef QList<ILogDisplay *> TLogDisplayList;
//! List of all attached displays.
TLogDisplayList m_logDisplays;
//! Mutex object
/*!
This makes our class thread safe.
*/
QMutex m_mutex;
//! Position
quint32 m_posSet;
//! Log message string
/*!
This variable is used, if a log line consists of two or more
calls. The first ones are then stored temporary in this variable
until one message ends with a line feed.
*/
QString m_logLine;
SLogInformation m_logInformation;
};
} // namespace BlackMisc
#endif LOG_H

View File

@@ -0,0 +1,83 @@
//! Copyright (C) 2013 Roland Winklmeier
//! This Source Code Form is subject to the terms of the Mozilla Public
//! License, v. 2.0. If a copy of the MPL was not distributed with this
//! file, You can obtain one at http://mozilla.org/MPL/2.0/
#ifndef LOGMESSAGE_H
#define LOGMESSAGE_H
// Qt includes
#include <QTextStream>
#include "blackmisc/context.h"
#include "blackmisc/log.h"
namespace BlackMisc
{
class CLogMessage
{
struct LogStream {
//! Constructor
LogStream(CLog::TLogType type);
QTextStream output;
//! Message Buffer
QString buffer;
//! Logging type
CLog::TLogType type;
bool needSpace;
bool enableOutput;
//! Reference count
quint32 reference;
} *logStream;
public:
CLogMessage( CLog::TLogType type );
CLogMessage(const CLogMessage &other);
~CLogMessage();
inline CLogMessage &maybeSpace() { if (logStream->needSpace) logStream->output << ' '; return *this; }
inline CLogMessage &operator<<(QChar t) { logStream->output << '\'' << t << '\''; return maybeSpace(); }
inline CLogMessage &operator<<(QBool t) { logStream->output << (bool(t != 0) ? "true" : "false"); return maybeSpace(); }
inline CLogMessage &operator<<(bool t) { logStream->output << (t ? "true" : "false"); return maybeSpace(); }
inline CLogMessage &operator<<(char t) { logStream->output << t; return maybeSpace(); }
inline CLogMessage &operator<<(signed short t) { logStream->output << t; return maybeSpace(); }
inline CLogMessage &operator<<(unsigned short t) { logStream->output << t; return maybeSpace(); }
inline CLogMessage &operator<<(signed int t) { logStream->output << t; return maybeSpace(); }
inline CLogMessage &operator<<(unsigned int t) { logStream->output << t; return maybeSpace(); }
inline CLogMessage &operator<<(signed long t) { logStream->output << t; return maybeSpace(); }
inline CLogMessage &operator<<(unsigned long t) { logStream->output << t; return maybeSpace(); }
inline CLogMessage &operator<<(qint64 t)
{ logStream->output << QString::number(t); return maybeSpace(); }
inline CLogMessage &operator<<(quint64 t)
{ logStream->output << QString::number(t); return maybeSpace(); }
inline CLogMessage &operator<<(float t) { logStream->output << t; return maybeSpace(); }
inline CLogMessage &operator<<(double t) { logStream->output << t; return maybeSpace(); }
inline CLogMessage &operator<<(const char* t) { logStream->output << QString::fromAscii(t); return maybeSpace(); }
inline CLogMessage &operator<<(const QString & t) { logStream->output << '\"' << t << '\"'; return maybeSpace(); }
inline CLogMessage &operator<<(const QByteArray & t) { logStream->output << '\"' << t << '\"'; return maybeSpace(); }
static CLogMessage getWarningMsgObj() { return CLogMessage(CLog::WARNING); }
static CLogMessage getInfoMsgObj() { return CLogMessage(CLog::INFO); }
static CLogMessage getDebugMsgObj() { return CLogMessage(CLog::DEBUG); }
static CLogMessage getErrorMsgObj() { return CLogMessage(CLog::ERROR); }
static CLogMessage getAssertMsgObj() { return CLogMessage(CLog::ASSERT); }
inline CLogMessage &operator<<(QTextStreamManipulator m)
{ logStream->output << m; return *this; }
};
} // BlackMisc
#endif LOGMESSAGE_H// LOGMESSAGE_H

View File

@@ -0,0 +1,73 @@
//! Copyright (C) 2013 Roland Winklmeier
//! This Source Code Form is subject to the terms of the Mozilla Public
//! License, v. 2.0. If a copy of the MPL was not distributed with this
//! file, You can obtain one at http://mozilla.org/MPL/2.0/
#ifndef MESSAGE_H
#define MESSAGE_H
#include <QtGlobal>
#include <QDataStream>
#include <QTextStream>
#include "blackmisc/serialize.h"
namespace BlackMisc
{
class IMessage : public ISerialize
{
public:
IMessage(QString& id);
QString getID() const;
virtual QDataStream& operator<< ( QDataStream& in) = 0;
virtual QDataStream& operator>> (QDataStream& out) const = 0;
virtual QTextStream& operator<< ( QTextStream& in) = 0;
virtual QTextStream& operator>> (QTextStream& out) const = 0;
protected:
QString m_message_id;
};
class TestMessage : public IMessage
{
public:
TestMessage() : IMessage(QString("MSG_ID_TestMessage"))
{
testString = "This is a test message!";
}
QString getTestString () const { return testString; }
//QDataStream &operator>>(qint8 &i);
virtual QDataStream& operator<< ( QDataStream& in)
{
in >> m_message_id;
in >> testString;
return in;
}
virtual QDataStream& operator>> (QDataStream& out) const
{
out << m_message_id;
out << testString;
return out;
}
virtual QTextStream& operator<< ( QTextStream& in) { return in; }
virtual QTextStream& operator>> (QTextStream& out) const { return out; }
protected:
private:
QString testString;
};
} // namespace BlackMisc
#endif // MESSAGE_H

View File

@@ -0,0 +1,59 @@
//! Copyright (C) 2013 Roland Winklmeier
//! This Source Code Form is subject to the terms of the Mozilla Public
//! License, v. 2.0. If a copy of the MPL was not distributed with this
//! file, You can obtain one at http://mozilla.org/MPL/2.0/
#ifndef MESSAGE_DISPATCHER_H
#define MESSAGE_DISPATCHER_H
#include <QQueue>
#include <QMultiMap>
#include "blackmisc/message.h"
#include "blackmisc/context.h"
#include "blackmisc/type_info.h"
namespace BlackMisc
{
class CMessageHandler;
class CMessageDispatcher
{
// safe singleton declaration
SINGLETON_CLASS_DECLARATION(CMessageDispatcher)
CMessageDispatcher() {}
public:
virtual ~CMessageDispatcher() {}
void dispatch ();
void append ( IMessage* message);
bool empty ()
{
return m_messageQueue.size() == 0;
}
template <class T>
void registerClass(T*, CTypeInfo);
private:
typedef QQueue<IMessage*> TMessageQueue;
TMessageQueue m_messageQueue;
typedef QMultiMap<CTypeInfo, CMessageHandler*> TMessageHandlerMap;
TMessageHandlerMap m_messageHander;
};
template <class T>
void CMessageDispatcher::registerClass( T* object, CTypeInfo typeinfo)
{
m_messageHander.insert(typeinfo, object);
}
} // namespace BlackMisc
#endif // MESSAGE_DISPATCHER_H

View File

@@ -0,0 +1,59 @@
//! Copyright (C) 2013 Roland Winklmeier
//! This Source Code Form is subject to the terms of the Mozilla Public
//! License, v. 2.0. If a copy of the MPL was not distributed with this
//! file, You can obtain one at http://mozilla.org/MPL/2.0/
#ifndef MESSAGE_FACTORY_H
#define MESSAGE_FACTORY_H
#include <QHash>
#include "blackmisc/message.h"
#include "blackmisc/gui_messages.h"
#include "blackmisc/debug.h"
#include "blackmisc/context.h"
#define REGISTER_MESSAGE(classname, msgName) new MessageCreatorImpl<classname>(#msgName);
namespace BlackMisc
{
class IMessageCreator
{
public:
IMessageCreator(const QString& messageID);
virtual IMessage* create() = 0;
};
template <class T>
class MessageCreatorImpl : public IMessageCreator
{
public:
MessageCreatorImpl(const QString& messageID) : IMessageCreator(messageID) {}
virtual IMessage* create() { return new T; }
};
class CMessageFactory
{
// safe singleton declaration
SINGLETON_CLASS_DECLARATION(CMessageFactory)
CMessageFactory() { }
public:
virtual ~CMessageFactory();
IMessage* create (const QString &messageID);
void registerMessage(const QString &messageID, IMessageCreator* creator);
static void registerMessages();
private:
typedef QHash<QString, IMessageCreator*> TMessageCreatorHash;
TMessageCreatorHash m_creators;
};
} // namespace BlackMisc
#endif // MESSAGE_FACTORY_H

View File

@@ -0,0 +1,75 @@
//! Copyright (C) 2013 Roland Winklmeier
//! This Source Code Form is subject to the terms of the Mozilla Public
//! License, v. 2.0. If a copy of the MPL was not distributed with this
//! file, You can obtain one at http://mozilla.org/MPL/2.0/
#ifndef MESSAGE_HANDLER_H
#define MESSAGE_HANDLER_H
#include <QMap>
#include "blackmisc/message.h"
#include "blackmisc/debug.h"
#include "blackmisc/type_info.h"
#include "blackmisc/message_dispatcher.h"
namespace BlackMisc
{
class IFunctionHandler
{
public:
virtual ~IFunctionHandler() {}
void exec (const IMessage* message)
{
call (message);
}
private:
virtual void call (const IMessage*) = 0;
};
template <class T, class MessageT>
class MemberFunctionHandler : public IFunctionHandler
{
public:
typedef void (T::*MemberFunction)(MessageT*);
MemberFunctionHandler(T* instance, MemberFunction memfunc) : m_instance(instance), m_function(memfunc) {}
void call(const IMessage* message)
{
(m_instance->*m_function)(static_cast<MessageT*>(message));
}
private:
T* m_instance;
MemberFunction m_function;
};
class CMessageHandler
{
public:
~CMessageHandler();
void handleMessage(const IMessage* message);
template <class T, class MessageT>
void registerMessageFunction(T*, void (T::*memfunc)(MessageT*));
private:
typedef QMap<CTypeInfo, IFunctionHandler*> TFunctionHandlerMap;
TFunctionHandlerMap m_messagehandler;
};
template <class T, class MessageT>
void CMessageHandler::registerMessageFunction(T* obj, void (T::*memfunc)(MessageT*))
{
CTypeInfo typeinfo = CTypeInfo(typeid(MessageT));
m_messagehandler[typeinfo]= new MemberFunctionHandler<T, MessageT>(obj, memfunc);
CMessageDispatcher::getInstance().registerClass(obj, typeinfo);
}
} // namespace BlackMisc
#endif // MESSAGE_HANDLER_H

View File

@@ -0,0 +1,26 @@
//! Copyright (C) 2013 Roland Winklmeier
//! This Source Code Form is subject to the terms of the Mozilla Public
//! License, v. 2.0. If a copy of the MPL was not distributed with this
//! file, You can obtain one at http://mozilla.org/MPL/2.0/
#ifndef MESSAGE_SYSTEM_H
#define MESSAGE_SYSTEM_H
#include "blackmisc/message_handler.h"
#include "blackmisc/message_dispatcher.h"
#include "blackmisc/message_factory.h"
namespace BlackMisc
{
class CMessageSystem
{
public:
CMessageSystem();
static void init();
};
} // namespace BlackMisc
#endif // MESSAGE_SYSTEM_H

View File

@@ -0,0 +1,21 @@
//! Copyright (C) 2013 Roland Winklmeier
//! This Source Code Form is subject to the terms of the Mozilla Public
//! License, v. 2.0. If a copy of the MPL was not distributed with this
//! file, You can obtain one at http://mozilla.org/MPL/2.0/
#ifndef SERIALIZE_H
#define SERIALIZE_H
class QDataStream;
class ISerialize
{
public:
ISerialize();
virtual ~ISerialize() {};
virtual QDataStream& operator<< (QDataStream& in) = 0;
virtual QDataStream& operator>> (QDataStream& out) const = 0;
};
#endif // SERIALIZE_H

View File

@@ -0,0 +1,27 @@
//! Copyright (C) 2013 Roland Winklmeier
//! This Source Code Form is subject to the terms of the Mozilla Public
//! License, v. 2.0. If a copy of the MPL was not distributed with this
//! file, You can obtain one at http://mozilla.org/MPL/2.0/
#ifndef TYPE_INFO_H
#define TYPE_INFO_H
#include <typeinfo.h>
namespace BlackMisc
{
class CTypeInfo
{
public:
explicit CTypeInfo(const type_info& info);
bool operator < (const CTypeInfo& rhs) const;
private:
const type_info& m_typeinfo;
};
} // namespace BlackMisc
#endif // TYPE_INFO_H