Commit 3dcc8e2e authored by Steven Murray's avatar Steven Murray
Browse files

Added common::QtCoreAppSingleton

parent 115659c3
......@@ -75,6 +75,7 @@ set (COMMON_LIB_SRC_FILES
ArchiveRequest.cpp
CreationLog.cpp
Configuration.cpp
QtCoreAppSingleton.cpp
SecurityIdentity.cpp
TapePool.cpp
Timer.cpp
......@@ -88,6 +89,7 @@ install (TARGETS ctacommon DESTINATION usr/${CMAKE_INSTALL_LIBDIR})
target_link_libraries (ctacommon
ctautils
pthread
QtCore
${SQLITE3_LIBRARY_RELEASE}
uuid
z
......@@ -100,6 +102,7 @@ set (COMMON_UNIT_TESTS_LIB_SRC_FILES
log/ParamTest.cpp
log/SyslogLoggerTest.cpp
log/StringLoggerTest.cpp
QtCoreAppSingletonTest.cpp
remoteFS/RemotePathTest.cpp
threading/DaemonTest.cpp
utils/UtilsTest.cpp
......
/*
* The CERN Tape Archive (CTA) project
* Copyright (C) 2015 CERN
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "common/exception/Exception.hpp"
#include "common/QtCoreAppSingleton.hpp"
#include <iostream>
#include <string.h>
#include <strings.h>
std::mutex cta::common::QtCoreAppSingleton::s_mutex;
cta::common::QtCoreAppSingleton *cta::common::QtCoreAppSingleton::s_instance =
NULL;
//------------------------------------------------------------------------------
// constructor
//------------------------------------------------------------------------------
cta::common::QtCoreAppSingleton::QtCoreAppSingleton() {
m_appProgramName[0] = 'q';
m_appProgramName[1] = 't';
m_appProgramName[2] = '\0';
m_appArgv[0] = m_appProgramName;
m_appArgc = 1;
m_app = new QCoreApplication(m_appArgc, m_appArgv);
}
//------------------------------------------------------------------------------
// destructor
//------------------------------------------------------------------------------
cta::common::QtCoreAppSingleton::~QtCoreAppSingleton() {
std::cout << "Deleting Qt core application object" << std::endl;
delete m_app;
}
//------------------------------------------------------------------------------
// operator==
//------------------------------------------------------------------------------
bool cta::common::QtCoreAppSingleton::operator==(const QtCoreAppSingleton &rhs)
const {
return m_app == m_app;
}
//------------------------------------------------------------------------------
// getApp
//------------------------------------------------------------------------------
QCoreApplication &cta::common::QtCoreAppSingleton::getApp() const {
if(NULL == m_app) {
throw exception::Exception("Failed to get QCoreApplication object"
": object not allocated");
}
return *m_app;
}
//------------------------------------------------------------------------------
// instance
//------------------------------------------------------------------------------
cta::common::QtCoreAppSingleton &cta::common::QtCoreAppSingleton::instance() {
std::lock_guard<std::mutex> lock(s_mutex);
if(NULL == s_instance) {
s_instance = new QtCoreAppSingleton();
}
return *s_instance;
}
//------------------------------------------------------------------------------
// destroy
//------------------------------------------------------------------------------
void cta::common::QtCoreAppSingleton::destroy() {
std::lock_guard<std::mutex> lock(s_mutex);
delete s_instance;
s_instance = NULL;
}
//------------------------------------------------------------------------------
// operator<<
//------------------------------------------------------------------------------
std::ostream &operator<<(std::ostream &os,
const cta::common::QtCoreAppSingleton &obj) {
os << "app=" << std::hex << &obj.getApp() << std::dec;
return os;
}
/*
* The CERN Tape Archive (CTA) project
* Copyright (C) 2015 CERN
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#pragma once
#include <mutex>
#include <QtCore/QCoreApplication>
#include <ostream>
namespace cta {
namespace common {
/**
* Singleton ensure that a QCoreApplication object is only created once.
*/
class QtCoreAppSingleton {
public:
/**
* Creates if necessary and returns a pointer to the singleton object.
*/
static QtCoreAppSingleton &instance();
/**
* Deletes the singleton object if there is one.
*
* This method should ONLY be called once at the end of the program or even
* not all. This method is intended for unit test programs ran with valgrind.
*/
static void destroy();
/**
* Comparison operator.
*
* @rhs The object on the right hand side of the operator.
*/
bool operator==(const QtCoreAppSingleton &rhs) const;
/**
* Returns the QCoreApplication object.
*
* @return The QCoreApplication object.
*/
QCoreApplication &getApp() const;
private:
static std::mutex s_mutex;
/**
* The singleton.
*/
static QtCoreAppSingleton *s_instance;
/**
* Singleton constructor must be private.
*/
QtCoreAppSingleton();
/**
* Singleton destructor must be private.
*/
~QtCoreAppSingleton();
/**
* The number of dummy command-line arguments passed to the QCoreApplication
* object.
*/
int m_appArgc;
/**
* The dummy name of the program ("qt") that will be passed to the
* QCoreApplication object.
*/
char m_appProgramName[3];
/**
* A dummy argv array for the QCoreApplication object. A dummy program name
* followed by the conventional pointer to NULL.
*/
char *m_appArgv[2];
/**
* The QCoreApplication object.
*/
QCoreApplication *m_app;
}; // QtCoreAppSingleton
} // namespace common
} // namespace castor
/**
* Output stream operator for QtCoreAppSingleton.
*
* @param os The output stream.
* @param obj The QtCoreAppSingleton object.
*/
std::ostream &operator<<(std::ostream &os,
const cta::common::QtCoreAppSingleton &obj);
/*
* The CERN Tape Archive (CTA) project
* Copyright (C) 2015 CERN
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "common/QtCoreAppSingleton.hpp"
#include <gtest/gtest.h>
namespace unitTests {
class cta_common_QtCoreAppSingletonTest: public ::testing::Test {
protected:
virtual void SetUp() {
}
virtual void TearDown() {
}
};
TEST_F(cta_common_QtCoreAppSingletonTest, instance) {
using namespace cta::common;
ASSERT_NO_THROW(
QtCoreAppSingleton& app1 = QtCoreAppSingleton::instance();
QtCoreAppSingleton& app2 = QtCoreAppSingleton::instance();
ASSERT_EQ(app1, app2););
}
TEST_F(cta_common_QtCoreAppSingletonTest, getApp) {
using namespace cta::common;
QtCoreAppSingleton& app = QtCoreAppSingleton::instance();
ASSERT_NO_THROW(app.getApp());
} // namespace unitTests
} // namespace unitTests
......@@ -16,6 +16,8 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "common/QtCoreAppSingleton.hpp"
#include <gtest/gtest.h>
#include <gmock/gmock.h>
......@@ -25,6 +27,9 @@ int main(int argc, char** argv) {
::testing::InitGoogleMock(&argc, argv);
int ret = RUN_ALL_TESTS();
// Clean up any memory allocated for the Qt Core application object
cta::common::QtCoreAppSingleton::destroy();
// Close standard in, out and error so that valgrind can be used with the
// following command-line to track open file-descriptors:
//
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment