diff --git a/src/libs/utils/environment.cpp b/src/libs/utils/environment.cpp index fa61313144f..8c13585155a 100644 --- a/src/libs/utils/environment.cpp +++ b/src/libs/utils/environment.cpp @@ -433,44 +433,61 @@ optional EnvironmentProvider::provider(const QByteArray &id void EnvironmentChange::addSetValue(const QString &key, const QString &value) { - m_changeItems.append([key, value](Environment &env) { env.set(key, value); }); + m_changeItems.append({Item::SetValue, QVariant::fromValue(QPair(key, value))}); } void EnvironmentChange::addUnsetValue(const QString &key) { - m_changeItems.append([key](Environment &env) { env.unset(key); }); + m_changeItems.append({Item::UnsetValue, key}); } void EnvironmentChange::addPrependToPath(const FilePaths &values) { for (int i = values.size(); --i >= 0; ) { const FilePath value = values.at(i); - m_changeItems.append([value](Environment &env) { env.prependOrSetPath(value); }); + m_changeItems.append({Item::PrependToPath, value.toVariant()}); } } void EnvironmentChange::addAppendToPath(const FilePaths &values) { for (const FilePath &value : values) - m_changeItems.append([value](Environment &env) { env.appendOrSetPath(value); }); -} - -void EnvironmentChange::addModify(const NameValueItems &items) -{ - m_changeItems.append([items](Environment &env) { env.modify(items); }); + m_changeItems.append({Item::AppendToPath, value.toVariant()}); } EnvironmentChange EnvironmentChange::fromFixedEnvironment(const Environment &fixedEnv) { EnvironmentChange change; - change.m_changeItems.append([fixedEnv](Environment &env) { env = fixedEnv; }); + change.m_changeItems.append({Item::SetFixedEnvironment, QVariant::fromValue(fixedEnv)}); return change; } void EnvironmentChange::applyToEnvironment(Environment &env) const { - for (const Item &item : m_changeItems) - item(env); + for (const Item &item : m_changeItems) { + switch (item.type) { + case Item::SetSystemEnvironment: + env = Environment::systemEnvironment(); + break; + case Item::SetFixedEnvironment: + env = item.data.value(); + break; + case Item::SetValue: { + auto data = item.data.value>(); + env.set(data.first, data.second); + break; + } + case Item::UnsetValue: + env.unset(item.data.toString()); + break; + case Item::PrependToPath: + env.prependOrSetPath(FilePath::fromVariant(item.data)); + break; + case Item::AppendToPath: + env.appendOrSetPath(FilePath::fromVariant(item.data)); + break; + } + } } } // namespace Utils diff --git a/src/libs/utils/environment.h b/src/libs/utils/environment.h index 7aa42ab2ad9..6429ba90536 100644 --- a/src/libs/utils/environment.h +++ b/src/libs/utils/environment.h @@ -132,10 +132,24 @@ private: class QTCREATOR_UTILS_EXPORT EnvironmentChange final { public: - using Item = std::function; - EnvironmentChange() = default; + class Item final + { + public: + enum Type { + SetSystemEnvironment, + SetFixedEnvironment, + SetValue, + UnsetValue, + PrependToPath, + AppendToPath, + }; + + Type type; + QVariant data; + }; + static EnvironmentChange fromFixedEnvironment(const Environment &fixedEnv); void applyToEnvironment(Environment &) const; @@ -144,8 +158,6 @@ public: void addUnsetValue(const QString &key); void addPrependToPath(const FilePaths &values); void addAppendToPath(const FilePaths &values); - void addModify(const NameValueItems &items); - void addChange(const Item &item) { m_changeItems.append(item); } private: QList m_changeItems;