refs #478 refs #488, utils / free functions

* seed for random numbers per thread
* added function to get all user metatypes as string
* added new read file function per path/name
* removed DBus operators for pixmap
* JSON operators for byte array
* util function for JSON string
This commit is contained in:
Klaus Basan
2015-10-14 01:48:52 +02:00
committed by Mathew Sutcliffe
parent 19df8a5d71
commit a725ce2181
14 changed files with 120 additions and 64 deletions

View File

@@ -267,13 +267,13 @@ size_t BlackMisc::heapSizeOf(const QMetaObject &metaObject)
size_t BlackMisc::heapSizeOf(const QMetaType &)
{
qDebug() << "heapSizeOf not supported by your compiler toolchain";
// qDebug() << "heapSizeOf not supported by your compiler toolchain";
return 0;
}
size_t BlackMisc::heapSizeOf(const QMetaObject &)
{
qDebug() << "heapSizeOf not supported by your compiler toolchain";
// qDebug() << "heapSizeOf not supported by your compiler toolchain";
return 0;
}
@@ -281,14 +281,31 @@ size_t BlackMisc::heapSizeOf(const QMetaObject &)
void BlackMisc::displayAllUserMetatypesTypes(QTextStream &out)
{
out << getAllUserMetatypesTypes();
}
QString BlackMisc::getAllUserMetatypesTypes(const QString &separator)
{
int fails = 0;
QString meta;
for (int mt = QMetaType::User; mt < QMetaType::User + 1000; mt++)
{
if (!QMetaType::isRegistered(mt)) { continue; }
if (!QMetaType::isRegistered(mt))
{
fails++;
// normally a consecutive order of metatypes, we allow a space before we break
if (fails > 3) { return meta; }
continue;
}
QMetaType metaType(mt);
out << "type: " << mt << " name:" << QMetaType::typeName(mt) << " | "
<< QMetaType::sizeOf(mt) << " / " << BlackMisc::heapSizeOf(metaType) << endl;
meta = meta.
append("type: ").append(QString::number(mt)).
append(" name: ").append(QMetaType::typeName(mt)).
append(" | ").append(QString::number(QMetaType::sizeOf(mt))).
append(" / ").append(QString::number(BlackMisc::heapSizeOf(metaType))).
append(separator);
}
return meta;
}
const QString &BlackMisc::localHostName()
@@ -369,12 +386,13 @@ QString BlackMisc::boolToTrueFalse(bool v, bool i18n)
bool BlackMisc::stringToBool(const QString &string)
{
if (string.isEmpty()) { return false; }
QChar c = string.at(0).toLower();
QString s(string.trimmed().toLower());
if (s.isEmpty()) { return false; }
QChar c = s.at(0);
// explicit values
if (c == '1' || c == 't' || c == 'y' || c == 'x') { return true; }
if (c == '0' || c == 'f' || c == 'n' || c == ' ') { return false; }
if (c == '0' || c == 'f' || c == 'n' || c == '_') { return false; }
return false;
}
@@ -439,7 +457,6 @@ bool BlackMisc::pngByteArrayToPixmapRef(const QByteArray &array, QPixmap &pixmap
return s;
}
QString BlackMisc::pixmapToPngHexString(const QPixmap &pixmap)
{
QByteArray ba;

View File

@@ -35,11 +35,6 @@ inline void initBlackMiscResources()
// cannot be declared within namespace, see docu
// hence BlackMisc::initResources() calls this inline function
Q_INIT_RESOURCE(blackmisc);
// set seed for random number if ever used
// that is needed only once on application startup
QTime time = QTime::currentTime();
qsrand((uint)time.msec());
}
//! Free functions in BlackMisc
@@ -258,6 +253,9 @@ namespace BlackMisc
//! \remarks Used in order to debug code, do not remove
BLACKMISC_EXPORT void displayAllUserMetatypesTypes(QTextStream &out);
//! Get all user metatypes
BLACKMISC_EXPORT QString getAllUserMetatypesTypes(const QString &separator = "\n");
/*!
* \brief Calculate a single hash value based on a list of individual hash values
* \param values

View File

@@ -131,6 +131,13 @@ namespace BlackMisc
return json;
}
//! Convenience function JSON as string
QString toJsonString(QJsonDocument::JsonFormat format = QJsonDocument::Indented) const
{
QJsonDocument jsonDoc(toJson());
return jsonDoc.toJson(format);
}
//! \copydoc CValueObject::convertFromJson
void convertFromJson(const QJsonObject &json)
{
@@ -144,6 +151,12 @@ namespace BlackMisc
}
}
//! Assign from JSON object string
void convertFromJson(const QString &jsonString)
{
convertFromJson(BlackMisc::Json::jsonObjectFromString(jsonString));
}
//! \copydoc BlackMisc::CValueObject::convertToQString
QString convertToQString(bool i18n = false) const
{

View File

@@ -1,31 +0,0 @@
/* Copyright (C) 2015
* swift project Community / Contributors
*
* This file is part of swift project. It is subject to the license terms in the LICENSE file found in the top-level
* directory of this distribution and at http://www.swift-project.org/license.html. No part of swift project,
* including this file, may be copied, modified, propagated, or distributed except according to the terms
* contained in the LICENSE file.
*/
#include "blackmisc/dbus.h"
#include "blackmisc/blackmiscfreefunctions.h"
const QDBusArgument &operator>>(const QDBusArgument &argument, QPixmap &pixmap)
{
QByteArray ba;
argument.beginStructure();
argument >> ba;
BlackMisc::pngByteArrayToPixmapRef(ba, pixmap);
argument.endStructure();
return argument;
}
QDBusArgument &operator<<(QDBusArgument &argument, const QPixmap &pixmap)
{
QByteArray ba;
BlackMisc::pixmapToPngByteArray(pixmap, ba);
argument.beginStructure();
argument << ba;
argument.endStructure();
return argument;
}

View File

@@ -21,7 +21,6 @@ namespace BlackMisc
{
namespace Mixin
{
/*!
* CRTP class template which will generate marshalling operators for a derived class with its own marshalling implementation.
*
@@ -124,14 +123,4 @@ const QDBusArgument &operator >>(const QDBusArgument &arg, E &value)
return arg;
}
/*!
* Non member non-friend streaming for QPixmap
*/
const QDBusArgument &operator>>(const QDBusArgument &argument, QPixmap &pixmap);
/*!
* Non member non-friend streaming for QPixmap
*/
QDBusArgument &operator<<(QDBusArgument &argument, const QPixmap &pixmap);
#endif // guard

View File

@@ -36,6 +36,11 @@ namespace BlackMisc
return content;
}
QString CFileUtils::readFileToString(const QString &filePath, const QString &fileName)
{
return readFileToString(appendFilePaths(filePath, fileName));
}
bool CFileUtils::writeStringToFileInBackground(const QString &content, const QString &fileNameAndPath)
{
if (fileNameAndPath.isEmpty()) { return false; }

View File

@@ -30,6 +30,9 @@ namespace BlackMisc
//! Read file into string
static QString readFileToString(const QString &fileNameAndPath);
//! Read file into string
static QString readFileToString(const QString &filePath, const QString &fileName);
//! Write string to text file in background
static bool writeStringToFileInBackground(const QString &content, const QString &fileNameAndPath);

View File

@@ -65,6 +65,20 @@ const QJsonValue &operator >>(const QJsonValue &json, QDateTime &value)
return json;
}
const QJsonValue &operator >>(const QJsonValue &json, QPixmap &value)
{
const QString hex(json.toString());
BlackMisc::pngHexStringToPixmapRef(hex, value);
return json;
}
const QJsonValue &operator >>(const QJsonValue &json, QByteArray &value)
{
const QString hex(json.toString());
value = BlackMisc::byteArrayFromHexString(hex);
return json;
}
const QJsonValueRef &operator >>(const QJsonValueRef &json, int &value)
{
value = json.toInt();
@@ -126,10 +140,10 @@ const QJsonValueRef &operator >>(const QJsonValueRef &json, QPixmap &value)
return json;
}
const QJsonValue &operator >>(const QJsonValue &json, QPixmap &value)
const QJsonValueRef &operator >>(const QJsonValueRef &json, QByteArray &value)
{
const QString hex(json.toString());
BlackMisc::pngHexStringToPixmapRef(hex, value);
value = BlackMisc::byteArrayFromHexString(hex);
return json;
}
@@ -194,6 +208,13 @@ QJsonArray &operator<<(QJsonArray &json, const QPixmap &value)
return json;
}
QJsonArray &operator<<(QJsonArray &json, const QByteArray &value)
{
QString pm(BlackMisc::byteArrayFromHexString(value));
json.append(QJsonValue(pm));
return json;
}
QJsonObject &operator<<(QJsonObject &json, const std::pair<QString, const int &> &value)
{
json.insert(value.first, QJsonValue(value.second));
@@ -255,6 +276,13 @@ QJsonObject &operator<<(QJsonObject &json, const std::pair<QString, const QPixma
return json;
}
QJsonObject &operator<<(QJsonObject &json, const std::pair<QString, const QByteArray &> &value)
{
QString pm(BlackMisc::bytesToHexString(value.second));
json.insert(value.first, pm);
return json;
}
QJsonObject BlackMisc::Json::jsonObjectFromString(const QString &json)
{
if (json.isEmpty()) { return QJsonObject();}

View File

@@ -16,12 +16,14 @@
#include "blackmisc/tuple.h"
#include "blackmisc/inheritance_traits.h"
#include <QJsonObject>
#include <QJsonDocument>
#include <QJsonValue>
#include <QJsonValueRef>
#include <QJsonArray>
#include <QDateTime>
#include <QStringList>
#include <QPixmap>
#include <QByteArray>
#include <utility>
/*!
@@ -41,6 +43,7 @@ BLACKMISC_EXPORT const QJsonValue &operator >>(const QJsonValue &json, double &v
BLACKMISC_EXPORT const QJsonValue &operator >>(const QJsonValue &json, bool &value);
BLACKMISC_EXPORT const QJsonValue &operator >>(const QJsonValue &json, QDateTime &value);
BLACKMISC_EXPORT const QJsonValue &operator >>(const QJsonValue &json, QPixmap &value);
BLACKMISC_EXPORT const QJsonValue &operator >>(const QJsonValue &json, QByteArray &value);
BLACKMISC_EXPORT const QJsonValueRef &operator >>(const QJsonValueRef &json, int &value);
BLACKMISC_EXPORT const QJsonValueRef &operator >>(const QJsonValueRef &json, qlonglong &value);
BLACKMISC_EXPORT const QJsonValueRef &operator >>(const QJsonValueRef &json, qulonglong &value);
@@ -51,6 +54,7 @@ BLACKMISC_EXPORT const QJsonValueRef &operator >>(const QJsonValueRef &json, dou
BLACKMISC_EXPORT const QJsonValueRef &operator >>(const QJsonValueRef &json, bool &value);
BLACKMISC_EXPORT const QJsonValueRef &operator >>(const QJsonValueRef &json, QDateTime &value);
BLACKMISC_EXPORT const QJsonValueRef &operator >>(const QJsonValueRef &json, QPixmap &value);
BLACKMISC_EXPORT const QJsonValueRef &operator >>(const QJsonValueRef &json, QByteArray &value);
//! @}
@@ -98,6 +102,7 @@ BLACKMISC_EXPORT QJsonArray &operator<<(QJsonArray &json, const double value);
BLACKMISC_EXPORT QJsonArray &operator<<(QJsonArray &json, const bool value);
BLACKMISC_EXPORT QJsonArray &operator<<(QJsonArray &json, const QDateTime &value);
BLACKMISC_EXPORT QJsonArray &operator<<(QJsonArray &json, const QPixmap &value);
BLACKMISC_EXPORT QJsonArray &operator<<(QJsonArray &json, const QByteArray &value);
//! @}
//! \name Streaming operators for QJsonObject (from value)
@@ -113,6 +118,7 @@ BLACKMISC_EXPORT QJsonObject &operator<<(QJsonObject &json, const std::pair<QStr
BLACKMISC_EXPORT QJsonObject &operator<<(QJsonObject &json, const std::pair<QString, const bool &> &value);
BLACKMISC_EXPORT QJsonObject &operator<<(QJsonObject &json, const std::pair<QString, const QDateTime &> &value);
BLACKMISC_EXPORT QJsonObject &operator<<(QJsonObject &json, const std::pair<QString, const QPixmap &> &value);
BLACKMISC_EXPORT QJsonObject &operator<<(QJsonObject &json, const std::pair<QString, const QByteArray &> &value);
//! @}
namespace BlackMisc
@@ -198,6 +204,13 @@ namespace BlackMisc
return Json::appendJsonObject(json, baseToJson(static_cast<const BaseOfT<Derived> *>(derived())));
}
//! Convenience function JSON as string
QString toJsonString(QJsonDocument::JsonFormat format = QJsonDocument::Indented) const
{
QJsonDocument jsonDoc(toJson());
return jsonDoc.toJson(format);
}
//! Assign from JSON object
void convertFromJson(const QJsonObject &json)
{
@@ -205,6 +218,12 @@ namespace BlackMisc
BlackMisc::deserializeJson(json, Private::EncapsulationBreaker::toMetaTuple(*derived()));
}
//! Assign from JSON object string
void convertFromJson(const QString &jsonString)
{
convertFromJson(BlackMisc::Json::jsonObjectFromString(jsonString));
}
private:
const Derived *derived() const { return static_cast<const Derived *>(this); }
Derived *derived() { return static_cast<Derived *>(this); }

View File

@@ -8,6 +8,8 @@
*/
#include "blackmisc/math/mathutils.h"
#include <QThreadStorage>
#include <QTime>
#include <algorithm>
#include <cmath>
@@ -77,5 +79,19 @@ namespace BlackMisc
return (result >= 0.0) ? result : result + 360.0;
}
int CMathUtils::randomInteger(int low, int high)
{
static QThreadStorage<uint> seeds;
if (!seeds.hasLocalData())
{
// seed is per thread!
uint seed = static_cast<uint>(QTime::currentTime().msec());
qsrand(seed);
seeds.setLocalData(seed);
}
int r(qrand());
return r % ((high + 1) - low) + low;
}
} // namespace
} // namespace

View File

@@ -100,10 +100,7 @@ namespace BlackMisc
static double normalizeDegrees(double degrees);
//! Random number between low and high
static int randomInteger(int low, int high)
{
return qrand() % ((high + 1) - low) + low;
}
static int randomInteger(int low, int high);
};
} // namespace

View File

@@ -13,7 +13,6 @@
namespace BlackMisc
{
CPixmap::CPixmap() = default;
CPixmap::CPixmap(const QPixmap &pixmap) : m_pixmap(pixmap), m_hasCachedPixmap(true)

View File

@@ -66,7 +66,7 @@ namespace BlackMisc
} // namespace
BLACK_DECLARE_TUPLE_CONVERSION(BlackMisc::CPixmap, (
attr(o.m_array, flags<DisabledForJson>())
attr(o.m_array)
))
Q_DECLARE_METATYPE(BlackMisc::CPixmap)

View File

@@ -112,6 +112,9 @@ namespace BlackMisc
//! \copydoc BlackMisc::Mixin::JsonByTuple::toJson
using Mixin::JsonByTuple<Derived>::toJson;
//! \copydoc BlackMisc::Mixin::JsonByTuple::toJsonString
using Mixin::JsonByTuple<Derived>::toJsonString;
//! \copydoc BlackMisc::Mixin::JsonByTuple::convertFromJson
using Mixin::JsonByTuple<Derived>::convertFromJson;