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)
This commit is contained in:
Oswald Buddenhagen 2011-04-21 21:34:59 +02:00 committed by Olivier Goffart
parent 5a4df43c71
commit 61b85df09b
2 changed files with 35 additions and 4 deletions

View File

@ -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<Key, Value> 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()); }

View File

@ -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);