From 61b85df09b80374f2d4e5d2f6004d020b3cb05a8 Mon Sep 17 00:00:00 2001 From: Oswald Buddenhagen Date: Thu, 21 Apr 2011 21:34:59 +0200 Subject: make QProcessEnvironment on Unix cache converted values values are converted between byte arrays and qstrings on demand. this makes it feasible to use the class as a generic environment container with fast reading and writing access. Reviewed-by: thiago Reviewed-by: dt (cherry picked from commit 7aa4ecdedba60ac4cbc07a774ae9d834677002e9) --- src/corelib/io/qprocess_p.h | 37 ++++++++++++++++++++++++++++++++++--- src/corelib/io/qprocess_unix.cpp | 2 +- 2 files changed, 35 insertions(+), 4 deletions(-) (limited to 'src') diff --git a/src/corelib/io/qprocess_p.h b/src/corelib/io/qprocess_p.h index 9a9981efda..251f8bc772 100644 --- a/src/corelib/io/qprocess_p.h +++ b/src/corelib/io/qprocess_p.h @@ -113,7 +113,35 @@ public: uint hash; }; - typedef QByteArray Value; + class Value + { + public: + Value() {} + Value(const Value &other) { *this = other; } + explicit Value(const QString &value) : stringValue(value) {} + explicit Value(const QByteArray &value) : byteValue(value) {} + bool operator==(const Value &other) const + { + return byteValue.isEmpty() && other.byteValue.isEmpty() + ? stringValue == other.stringValue + : bytes() == other.bytes(); + } + QByteArray bytes() const + { + if (byteValue.isEmpty() && !stringValue.isEmpty()) + byteValue = stringValue.toLocal8Bit(); + return byteValue; + } + QString string() const + { + if (stringValue.isEmpty() && !byteValue.isEmpty()) + stringValue = QString::fromLocal8Bit(byteValue); + return stringValue; + } + + mutable QByteArray byteValue; + mutable QString stringValue; + }; inline Key prepareName(const QString &name) const { @@ -128,8 +156,8 @@ public: nameMap[sname] = name; return sname; } - inline Value prepareValue(const QString &value) const { return value.toLocal8Bit(); } - inline QString valueToString(const Value &value) const { return QString::fromLocal8Bit(value); } + inline Value prepareValue(const QString &value) const { return Value(value); } + inline QString valueToString(const Value &value) const { return value.string(); } #endif typedef QHash Hash; @@ -146,6 +174,9 @@ public: void insert(const QProcessEnvironmentPrivate &other); }; Q_DECLARE_TYPEINFO(QProcessEnvironmentPrivate::Key, Q_MOVABLE_TYPE); +#ifdef Q_OS_UNIX +Q_DECLARE_TYPEINFO(QProcessEnvironmentPrivate::Value, Q_MOVABLE_TYPE); +#endif #ifdef Q_OS_WIN inline uint qHash(const QProcessEnvironmentPrivate::Key &key) { return qHash(key.toCaseFolded()); } diff --git a/src/corelib/io/qprocess_unix.cpp b/src/corelib/io/qprocess_unix.cpp index 4e50bc546b..7a9e75d534 100644 --- a/src/corelib/io/qprocess_unix.cpp +++ b/src/corelib/io/qprocess_unix.cpp @@ -510,7 +510,7 @@ static char **_q_dupEnvironment(const QProcessEnvironmentPrivate::Hash &environm const QProcessEnvironmentPrivate::Hash::ConstIterator end = environment.constEnd(); for ( ; it != end; ++it) { QByteArray key = it.key().key; - QByteArray value = it.value(); + QByteArray value = it.value().bytes(); key.reserve(key.length() + 1 + value.length()); key.append('='); key.append(value); -- cgit v1.2.3