CMake/Source/cmRemoveCommand.cxx
Sebastian Holtermann cdff7f4e2a cmSystemTools: Add ExpandedListArgument and ExpandedLists methods
Changes
-------

In `cmSystemTools` this
- renames the method `ExpandList` to `ExpandLists` and makes it iterator based

and adds the methods
- `std::vector<std::string> ExpandedLists(InputIt first, InputIt last)`
- `std::vector<std::string> ExpandedListArgument(const std::string& arg,
                                                       bool emptyArgs)`

Both return the  `std::vector<std::string>` instead of taking a return vector
reference like `cmSystemTools::ExpandLists` and
`cmSystemTools::ExpandListArgument`.

Motivation
----------

Since C++17 return value optimization is mandatory, so returning a
`std:vector<std::string>` from a function should be (at least) as fast as
passing a return vector reference to the function.

The new methods can replace `cmSystemTools::ExpandLists` and
`cmSystemTools::ExpandListArgument` in many cases, which leads to
shorter and simpler syntax.

E.g. the commonly used pattern
```
  if (const char* value = X->GetProperty("A_KEY_STRING")) {
    std::vector<std::string> valuesList;
    cmSystemTools::ExpandListArgument(value, valuesList);
    for (std::string const& i : valuesList) {
      doSomething(i);
    }
  }
```
becomes
```
  if (const char* value = X->GetProperty("A_KEY_STRING")) {
    for (std::string const& i :
      cmSystemTools::ExpandedListArgument(value)) {
      doSomething(i);
    }
  }
```
2019-05-13 15:37:18 +02:00

59 lines
1.5 KiB
C++

/* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
file Copyright.txt or https://cmake.org/licensing for details. */
#include "cmRemoveCommand.h"
#include "cmMakefile.h"
#include "cmSystemTools.h"
class cmExecutionStatus;
// cmRemoveCommand
bool cmRemoveCommand::InitialPass(std::vector<std::string> const& args,
cmExecutionStatus&)
{
if (args.empty()) {
return true;
}
std::string const& variable = args[0]; // VAR is always first
// get the old value
const char* cacheValue = this->Makefile->GetDefinition(variable);
// if there is no old value then return
if (!cacheValue) {
return true;
}
// expand the variable
std::vector<std::string> const varArgsExpanded =
cmSystemTools::ExpandedListArgument(cacheValue);
// expand the args
// check for REMOVE(VAR v1 v2 ... vn)
std::vector<std::string> const argsExpanded =
cmSystemTools::ExpandedLists(args.begin() + 1, args.end());
// now create the new value
std::string value;
for (std::string const& varArgExpanded : varArgsExpanded) {
int found = 0;
for (std::string const& argExpanded : argsExpanded) {
if (varArgExpanded == argExpanded) {
found = 1;
break;
}
}
if (!found) {
if (!value.empty()) {
value += ";";
}
value += varArgExpanded;
}
}
// add the definition
this->Makefile->AddDefinition(variable, value.c_str());
return true;
}