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:
parent
5a4df43c71
commit
61b85df09b
@ -113,7 +113,35 @@ public:
|
|||||||
uint hash;
|
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
|
inline Key prepareName(const QString &name) const
|
||||||
{
|
{
|
||||||
@ -128,8 +156,8 @@ public:
|
|||||||
nameMap[sname] = name;
|
nameMap[sname] = name;
|
||||||
return sname;
|
return sname;
|
||||||
}
|
}
|
||||||
inline Value prepareValue(const QString &value) const { return value.toLocal8Bit(); }
|
inline Value prepareValue(const QString &value) const { return Value(value); }
|
||||||
inline QString valueToString(const Value &value) const { return QString::fromLocal8Bit(value); }
|
inline QString valueToString(const Value &value) const { return value.string(); }
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
typedef QHash<Key, Value> Hash;
|
typedef QHash<Key, Value> Hash;
|
||||||
@ -146,6 +174,9 @@ public:
|
|||||||
void insert(const QProcessEnvironmentPrivate &other);
|
void insert(const QProcessEnvironmentPrivate &other);
|
||||||
};
|
};
|
||||||
Q_DECLARE_TYPEINFO(QProcessEnvironmentPrivate::Key, Q_MOVABLE_TYPE);
|
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
|
#ifdef Q_OS_WIN
|
||||||
inline uint qHash(const QProcessEnvironmentPrivate::Key &key) { return qHash(key.toCaseFolded()); }
|
inline uint qHash(const QProcessEnvironmentPrivate::Key &key) { return qHash(key.toCaseFolded()); }
|
||||||
|
@ -510,7 +510,7 @@ static char **_q_dupEnvironment(const QProcessEnvironmentPrivate::Hash &environm
|
|||||||
const QProcessEnvironmentPrivate::Hash::ConstIterator end = environment.constEnd();
|
const QProcessEnvironmentPrivate::Hash::ConstIterator end = environment.constEnd();
|
||||||
for ( ; it != end; ++it) {
|
for ( ; it != end; ++it) {
|
||||||
QByteArray key = it.key().key;
|
QByteArray key = it.key().key;
|
||||||
QByteArray value = it.value();
|
QByteArray value = it.value().bytes();
|
||||||
key.reserve(key.length() + 1 + value.length());
|
key.reserve(key.length() + 1 + value.length());
|
||||||
key.append('=');
|
key.append('=');
|
||||||
key.append(value);
|
key.append(value);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user