Use std::string_view for CodeFactory parameters (where possible)

This commit is contained in:
Martchus 2021-05-16 19:09:46 +02:00
parent 8f1909dfdf
commit e3d32ddfa1
4 changed files with 19 additions and 16 deletions

View File

@ -30,8 +30,8 @@ CodeFactory::ToolInvocation::ToolInvocation(CodeFactory &factory)
* \brief Constructs a new instance. * \brief Constructs a new instance.
* \remarks The specified arguments are not copied and must remain valid for the live-time of the code factory. * \remarks The specified arguments are not copied and must remain valid for the live-time of the code factory.
*/ */
CodeFactory::CodeFactory( CodeFactory::CodeFactory(std::string_view applicationPath, const std::vector<const char *> &sourceFiles,
const char *applicationPath, const std::vector<const char *> &sourceFiles, const std::vector<string> &clangOptions, std::ostream &os) const std::vector<std::string_view> &clangOptions, std::ostream &os)
: m_applicationPath(applicationPath) : m_applicationPath(applicationPath)
, m_sourceFiles(sourceFiles) , m_sourceFiles(sourceFiles)
, m_clangOptions(clangOptions) , m_clangOptions(clangOptions)
@ -50,7 +50,7 @@ CodeFactory::~CodeFactory()
*/ */
std::vector<string> CodeFactory::makeClangArgs() const std::vector<string> CodeFactory::makeClangArgs() const
{ {
static const initializer_list<const char *> flags static const initializer_list<std::string_view> flags
= { m_applicationPath, "-x", "c++", "-Wno-pragma-once-outside-header", "-std=c++14", "-fsyntax-only" }; = { m_applicationPath, "-x", "c++", "-Wno-pragma-once-outside-header", "-std=c++14", "-fsyntax-only" };
vector<string> clangArgs; vector<string> clangArgs;
clangArgs.reserve(flags.size() + m_clangOptions.size() + m_sourceFiles.size()); clangArgs.reserve(flags.size() + m_clangOptions.size() + m_sourceFiles.size());

View File

@ -29,8 +29,8 @@ class CodeFactory {
friend class Visitor; friend class Visitor;
public: public:
CodeFactory( CodeFactory(std::string_view applicationPath, const std::vector<const char *> &sourceFiles, const std::vector<std::string_view> &clangOptions,
const char *applicationPath, const std::vector<const char *> &sourceFiles, const std::vector<std::string> &clangOptions, std::ostream &os); std::ostream &os);
~CodeFactory(); ~CodeFactory();
const std::vector<std::unique_ptr<CodeGenerator>> &generators() const; const std::vector<std::unique_ptr<CodeGenerator>> &generators() const;
@ -50,9 +50,9 @@ private:
bool generate() const; bool generate() const;
std::vector<std::string> makeClangArgs() const; std::vector<std::string> makeClangArgs() const;
const char *const m_applicationPath; std::string_view m_applicationPath;
const std::vector<const char *> &m_sourceFiles; const std::vector<const char *> &m_sourceFiles;
const std::vector<std::string> &m_clangOptions; const std::vector<std::string_view> &m_clangOptions;
std::ostream &m_os; std::ostream &m_os;
std::vector<std::unique_ptr<CodeGenerator>> m_generators; std::vector<std::unique_ptr<CodeGenerator>> m_generators;
std::unique_ptr<ToolInvocation> m_toolInvocation; std::unique_ptr<ToolInvocation> m_toolInvocation;

View File

@ -69,14 +69,16 @@ int main(int argc, char *argv[])
} }
// compose options passed to the clang tool invocation // compose options passed to the clang tool invocation
vector<string> clangOptions; auto clangOptions = std::vector<std::string_view>();
if (clangOptionsArg.isPresent()) { if (clangOptionsArg.isPresent()) {
// add additional options specified via CLI argument // add additional options specified via CLI argument
for (const auto *const value : clangOptionsArg.values(0)) { for (const auto *const value : clangOptionsArg.values(0)) {
// split options by ";" - not nice but this eases using CMake generator expressions // split options by ";" - not nice but this eases using CMake generator expressions
const auto splittedValues(splitString<vector<string>>(value, ";", EmptyPartsTreat::Omit)); const auto splittedValues = splitStringSimple<std::vector<std::string_view>>(value, ";");
for (const auto &splittedValue : splittedValues) { for (const auto &splittedValue : splittedValues) {
clangOptions.emplace_back(move(splittedValue)); if (!splittedValue.empty()) {
clangOptions.emplace_back(splittedValue);
}
} }
} }
} }
@ -88,7 +90,7 @@ int main(int argc, char *argv[])
} }
// instantiate the code factory and add generators to it // instantiate the code factory and add generators to it
CodeFactory factory(parser.executable(), inputFileArg.values(0), clangOptions, *os); auto factory = CodeFactory(parser.executable(), inputFileArg.values(0), clangOptions, *os);
factory.setErrorResilient(errorResilientArg.isPresent()); factory.setErrorResilient(errorResilientArg.isPresent());
// add specified generators if the --generator argument is present; otherwise add default generators // add specified generators if the --generator argument is present; otherwise add default generators
if (generatorsArg.isPresent()) { if (generatorsArg.isPresent()) {

View File

@ -64,13 +64,14 @@ JsonGeneratorTests::JsonGeneratorTests()
*/ */
void JsonGeneratorTests::testGeneratorItself() void JsonGeneratorTests::testGeneratorItself()
{ {
const string inputFilePath(testFilePath("some_structs.h")); const auto inputFilePath = testFilePath("some_structs.h");
const vector<const char *> inputFiles{ inputFilePath.data() }; const auto inputFiles = vector<const char *>{ inputFilePath.data() };
const vector<string> clangOptions{ "-resource-dir", REFLECTION_GENERATOR_CLANG_RESOURCE_DIR, "-std=c++17", "-I", CPP_UTILITIES_INCLUDE_DIRS, const auto clangOptions
= vector<std::string_view>{ "-resource-dir", REFLECTION_GENERATOR_CLANG_RESOURCE_DIR, "-std=c++17", "-I", CPP_UTILITIES_INCLUDE_DIRS,
#ifdef RAPIDJSON_INCLUDE_DIRS #ifdef RAPIDJSON_INCLUDE_DIRS
"-I", RAPIDJSON_INCLUDE_DIRS "-I", RAPIDJSON_INCLUDE_DIRS
#endif #endif
}; };
stringstream buffer; stringstream buffer;
JsonSerializationCodeGenerator::Options jsonOptions; JsonSerializationCodeGenerator::Options jsonOptions;