initial commit
This commit is contained in:
commit
9d20827c46
2469 changed files with 470994 additions and 0 deletions
44
src/utilities/iodeviceopener.cpp
Normal file
44
src/utilities/iodeviceopener.cpp
Normal file
|
|
@ -0,0 +1,44 @@
|
|||
/*
|
||||
* Copyright (C) 2007 Remko Troncon
|
||||
*
|
||||
* 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 2
|
||||
* 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 library; if not, write to the Free Software
|
||||
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||
*
|
||||
*/
|
||||
|
||||
#include "iodeviceopener.h"
|
||||
|
||||
|
||||
IODeviceOpener::IODeviceOpener(QIODevice* device, QIODevice::OpenModeFlag mode) : device_(device), close_(false)
|
||||
{
|
||||
if (!device_->isOpen()) {
|
||||
if (!device_->open(mode)) {
|
||||
isOpen_ = false;
|
||||
return;
|
||||
}
|
||||
close_ = true;
|
||||
}
|
||||
else if (!(device_->openMode() & mode)) {
|
||||
isOpen_ = false;
|
||||
return;
|
||||
}
|
||||
isOpen_ = true;
|
||||
}
|
||||
|
||||
IODeviceOpener::~IODeviceOpener()
|
||||
{
|
||||
if (close_) {
|
||||
device_->close();
|
||||
}
|
||||
}
|
||||
72
src/utilities/iodeviceopener.h
Normal file
72
src/utilities/iodeviceopener.h
Normal file
|
|
@ -0,0 +1,72 @@
|
|||
/*
|
||||
* Copyright (C) 2007 Remko Troncon
|
||||
*
|
||||
* 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 2
|
||||
* 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 library; if not, write to the Free Software
|
||||
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||
*
|
||||
*/
|
||||
|
||||
#ifndef IODEVICEOPENER_H
|
||||
#define IODEVICEOPENER_H
|
||||
|
||||
#include <QIODevice>
|
||||
#include <QPointer>
|
||||
|
||||
/**
|
||||
* An IODeviceOpener is used to ensure that an IODevice is opened.
|
||||
* If the QIODevice was not open when the opener was created, it will be closed
|
||||
* again when the opener is destroyed.
|
||||
*
|
||||
* Example:
|
||||
* void foo(QIODevice* device) {
|
||||
* IODeviceOpener opener(device, QIODevice::ReadOnly);
|
||||
* if (!opener.isOpen()) {
|
||||
* qDebug() << "Error opening QIODevice";
|
||||
* return;
|
||||
* }
|
||||
* ...
|
||||
* device->readAll()
|
||||
* ...
|
||||
* }
|
||||
*/
|
||||
class IODeviceOpener
|
||||
{
|
||||
public:
|
||||
/**
|
||||
* Opens an QIODevice in a specific mode if the device was not opened
|
||||
* yet.
|
||||
* If the device was already open but in a different, non-compatible
|
||||
* mode than the one requested, isOpen() will return false.
|
||||
*/
|
||||
IODeviceOpener(QIODevice* device, QIODevice::OpenModeFlag mode);
|
||||
|
||||
/**
|
||||
* Closes the QIODevice passed to the constructor if the IODevice was not
|
||||
* opened at that time.
|
||||
*/
|
||||
~IODeviceOpener();
|
||||
|
||||
/**
|
||||
* Checks whether the io device was opened succesfully in the mode
|
||||
* requested.
|
||||
*/
|
||||
bool isOpen() const { return isOpen_; }
|
||||
|
||||
private:
|
||||
QPointer<QIODevice> device_;
|
||||
bool close_;
|
||||
bool isOpen_;
|
||||
};
|
||||
|
||||
#endif
|
||||
67
src/utilities/maybe.h
Normal file
67
src/utilities/maybe.h
Normal file
|
|
@ -0,0 +1,67 @@
|
|||
/*
|
||||
* maybe.h
|
||||
* Copyright (C) 2006 Remko Troncon
|
||||
*
|
||||
* 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 2
|
||||
* 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 library; if not, write to the Free Software
|
||||
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||
*
|
||||
*/
|
||||
|
||||
#ifndef MAYBE_H
|
||||
#define MAYBE_H
|
||||
|
||||
/**
|
||||
* \brief A template class either containing no value or a specific value.
|
||||
* This container is especially handy for types that do not have an isNull()
|
||||
* or isEmpty() (such as primitive types).
|
||||
*
|
||||
* Example: Returns the division of numer and denom if it is an integer
|
||||
* \code
|
||||
* Maybe<int> integer_divide(int numer, int denom) {
|
||||
* if (numer % denom == 0)
|
||||
* return Maybe<int>(numer / denom);
|
||||
* else
|
||||
* return Maybe<int>();
|
||||
* }
|
||||
* \endcode
|
||||
*/
|
||||
template<class T> class Maybe
|
||||
{
|
||||
public:
|
||||
/**
|
||||
* \brief Constructs a Maybe container with no value.
|
||||
*/
|
||||
Maybe() : hasValue_(false) {}
|
||||
|
||||
/**
|
||||
* \brief Constructs a Maybe container with a value.
|
||||
*/
|
||||
Maybe(const T& value) : value_(value), hasValue_(true) {}
|
||||
|
||||
/**
|
||||
* \brief Checks whether this container has a value.
|
||||
*/
|
||||
bool hasValue() const { return hasValue_; }
|
||||
|
||||
/**
|
||||
* \brief Returns the value of the container.
|
||||
*/
|
||||
const T& value() const { return value_; }
|
||||
|
||||
private:
|
||||
T value_;
|
||||
bool hasValue_;
|
||||
};
|
||||
|
||||
#endif
|
||||
154
src/utilities/unittest/iodeviceopenertest.cpp
Normal file
154
src/utilities/unittest/iodeviceopenertest.cpp
Normal file
|
|
@ -0,0 +1,154 @@
|
|||
/**
|
||||
* Copyright (C) 2007, Remko Troncon
|
||||
*/
|
||||
|
||||
#include <QList>
|
||||
#include <QStringList>
|
||||
#include <QBuffer>
|
||||
|
||||
#include <cppunit/extensions/HelperMacros.h>
|
||||
#include <cppunit/extensions/TestFactoryRegistry.h>
|
||||
|
||||
#include "iodeviceopener.h"
|
||||
|
||||
// -----------------------------------------------------------------------------
|
||||
|
||||
class IODeviceOpenerTest : public CppUnit::TestFixture
|
||||
{
|
||||
CPPUNIT_TEST_SUITE(IODeviceOpenerTest);
|
||||
|
||||
CPPUNIT_TEST(testConstructor);
|
||||
CPPUNIT_TEST(testConstructor_Open);
|
||||
CPPUNIT_TEST(testConstructor_OpenInSubsetMode);
|
||||
CPPUNIT_TEST(testConstructor_OpenInWrongMode);
|
||||
CPPUNIT_TEST(testConstructor_Unopenable);
|
||||
|
||||
CPPUNIT_TEST(testDestructor);
|
||||
CPPUNIT_TEST(testDestructor_Open);
|
||||
CPPUNIT_TEST(testDestructor_OpenInWrongMode);
|
||||
|
||||
CPPUNIT_TEST_SUITE_END();
|
||||
|
||||
public:
|
||||
IODeviceOpenerTest();
|
||||
|
||||
void testConstructor();
|
||||
void testConstructor_Open();
|
||||
void testConstructor_OpenInSubsetMode();
|
||||
void testConstructor_OpenInWrongMode();
|
||||
void testConstructor_Unopenable();
|
||||
|
||||
void testDestructor();
|
||||
void testDestructor_Open();
|
||||
void testDestructor_OpenInWrongMode();
|
||||
};
|
||||
|
||||
CPPUNIT_TEST_SUITE_REGISTRATION(IODeviceOpenerTest);
|
||||
|
||||
// -----------------------------------------------------------------------------
|
||||
|
||||
class UnopenableBuffer : public QBuffer
|
||||
{
|
||||
public:
|
||||
UnopenableBuffer() : QBuffer() { }
|
||||
|
||||
bool open(QIODevice::OpenMode) { return false; }
|
||||
};
|
||||
|
||||
// -----------------------------------------------------------------------------
|
||||
|
||||
IODeviceOpenerTest::IODeviceOpenerTest()
|
||||
{
|
||||
}
|
||||
|
||||
void IODeviceOpenerTest::testConstructor()
|
||||
{
|
||||
QBuffer buffer;
|
||||
|
||||
IODeviceOpener opener(&buffer, QIODevice::WriteOnly);
|
||||
|
||||
CPPUNIT_ASSERT(buffer.isOpen());
|
||||
CPPUNIT_ASSERT(QIODevice::WriteOnly == buffer.openMode());
|
||||
CPPUNIT_ASSERT(opener.isOpen());
|
||||
}
|
||||
|
||||
void IODeviceOpenerTest::testConstructor_Open()
|
||||
{
|
||||
QBuffer buffer;
|
||||
buffer.open(QIODevice::ReadOnly);
|
||||
|
||||
IODeviceOpener opener(&buffer, QIODevice::ReadOnly);
|
||||
|
||||
CPPUNIT_ASSERT(buffer.isOpen());
|
||||
CPPUNIT_ASSERT(QIODevice::ReadOnly == buffer.openMode());
|
||||
CPPUNIT_ASSERT(opener.isOpen());
|
||||
}
|
||||
|
||||
void IODeviceOpenerTest::testConstructor_OpenInSubsetMode()
|
||||
{
|
||||
QBuffer buffer;
|
||||
buffer.open(QIODevice::ReadWrite);
|
||||
|
||||
IODeviceOpener opener(&buffer, QIODevice::WriteOnly);
|
||||
|
||||
CPPUNIT_ASSERT(buffer.isOpen());
|
||||
CPPUNIT_ASSERT(QIODevice::ReadWrite == buffer.openMode());
|
||||
CPPUNIT_ASSERT(opener.isOpen());
|
||||
}
|
||||
|
||||
void IODeviceOpenerTest::testConstructor_OpenInWrongMode()
|
||||
{
|
||||
QBuffer buffer;
|
||||
buffer.open(QIODevice::ReadOnly);
|
||||
|
||||
IODeviceOpener opener(&buffer, QIODevice::WriteOnly);
|
||||
|
||||
CPPUNIT_ASSERT(buffer.isOpen());
|
||||
CPPUNIT_ASSERT(QIODevice::ReadOnly == buffer.openMode());
|
||||
CPPUNIT_ASSERT(!opener.isOpen());
|
||||
}
|
||||
|
||||
void IODeviceOpenerTest::testConstructor_Unopenable()
|
||||
{
|
||||
UnopenableBuffer buffer;
|
||||
|
||||
IODeviceOpener opener(&buffer, QIODevice::WriteOnly);
|
||||
|
||||
CPPUNIT_ASSERT(!opener.isOpen());
|
||||
}
|
||||
|
||||
void IODeviceOpenerTest::testDestructor()
|
||||
{
|
||||
QBuffer buffer;
|
||||
|
||||
{
|
||||
IODeviceOpener opener(&buffer, QIODevice::WriteOnly);
|
||||
}
|
||||
|
||||
CPPUNIT_ASSERT(!buffer.isOpen());
|
||||
}
|
||||
|
||||
void IODeviceOpenerTest::testDestructor_Open()
|
||||
{
|
||||
QBuffer buffer;
|
||||
buffer.open(QIODevice::ReadOnly);
|
||||
|
||||
{
|
||||
IODeviceOpener opener(&buffer, QIODevice::ReadOnly);
|
||||
}
|
||||
|
||||
CPPUNIT_ASSERT(buffer.isOpen());
|
||||
}
|
||||
|
||||
void IODeviceOpenerTest::testDestructor_OpenInWrongMode()
|
||||
{
|
||||
QBuffer buffer;
|
||||
buffer.open(QIODevice::ReadOnly);
|
||||
|
||||
{
|
||||
IODeviceOpener opener(&buffer, QIODevice::WriteOnly);
|
||||
}
|
||||
|
||||
CPPUNIT_ASSERT(buffer.isOpen());
|
||||
CPPUNIT_ASSERT(QIODevice::ReadOnly == buffer.openMode());
|
||||
}
|
||||
2
src/utilities/unittest/unittest.pri
Normal file
2
src/utilities/unittest/unittest.pri
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
SOURCES += \
|
||||
$$PWD/iodeviceopenertest.cpp
|
||||
9
src/utilities/utilities.pri
Normal file
9
src/utilities/utilities.pri
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
INCLUDEPATH *= $$PWD
|
||||
DEPENDPATH *= $$PWD
|
||||
|
||||
HEADERS += \
|
||||
$$PWD/maybe.h \
|
||||
$$PWD/iodeviceopener.h
|
||||
|
||||
SOURCES += \
|
||||
$$PWD/iodeviceopener.cpp
|
||||
Reference in a new issue