summaryrefslogtreecommitdiffstats
path: root/Source/CTest/cmCTestHandlerCommand.h
blob: ef7bc5de924fd91dcf8d174c1c911360bc4705b9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
/* Distributed under the OSI-approved BSD 3-Clause License.  See accompanying
   file LICENSE.rst or https://cmake.org/licensing for details.  */
#pragma once

#include "cmConfigure.h" // IWYU pragma: keep

#include <functional>
#include <memory>
#include <string>
#include <type_traits>
#include <vector>

#include <cm/string_view>
#include <cmext/string_view>

#include "cmArgumentParser.h"
#include "cmCTestCommand.h"

class cmExecutionStatus;
class cmCTestGenericHandler;

class cmCTestHandlerCommand : public cmCTestCommand
{
public:
  using cmCTestCommand::cmCTestCommand;

protected:
  struct BasicArguments : ArgumentParser::ParseResult
  {
    std::string CaptureCMakeError;
    std::vector<cm::string_view> ParsedKeywords;
  };

  template <typename Args>
  static auto MakeBasicParser() -> cmArgumentParser<Args>
  {
    static_assert(std::is_base_of<BasicArguments, Args>::value, "");
    return cmArgumentParser<Args>{}
      .Bind("CAPTURE_CMAKE_ERROR"_s, &BasicArguments::CaptureCMakeError)
      .BindParsedKeywords(&BasicArguments::ParsedKeywords);
  }

  struct HandlerArguments : BasicArguments
  {
    bool Append = false;
    bool Quiet = false;
    std::string ReturnValue;
    std::string Build;
    std::string Source;
    std::string SubmitIndex;
  };

  template <typename Args>
  static auto MakeHandlerParser() -> cmArgumentParser<Args>
  {
    static_assert(std::is_base_of<HandlerArguments, Args>::value, "");
    return cmArgumentParser<Args>{ MakeBasicParser<Args>() }
      .Bind("APPEND"_s, &HandlerArguments::Append)
      .Bind("QUIET"_s, &HandlerArguments::Quiet)
      .Bind("RETURN_VALUE"_s, &HandlerArguments::ReturnValue)
      .Bind("SOURCE"_s, &HandlerArguments::Source)
      .Bind("BUILD"_s, &HandlerArguments::Build)
      .Bind("SUBMIT_INDEX"_s, &HandlerArguments::SubmitIndex);
  }

protected:
  template <typename Args, typename Handler>
  bool Invoke(cmArgumentParser<Args> const& parser,
              std::vector<std::string> const& arguments,
              cmExecutionStatus& status, Handler handler) const
  {
    std::vector<std::string> unparsed;
    Args args = parser.Parse(arguments, &unparsed);
    return this->InvokeImpl(args, unparsed, status,
                            [&]() -> bool { return handler(args); });
  };

  bool ExecuteHandlerCommand(HandlerArguments& args,
                             cmExecutionStatus& status) const;

private:
  bool InvokeImpl(BasicArguments& args,
                  std::vector<std::string> const& unparsed,
                  cmExecutionStatus& status,
                  std::function<bool()> handler) const;

  virtual std::string GetName() const = 0;

  virtual void CheckArguments(HandlerArguments& arguments,
                              cmExecutionStatus& status) const;

  virtual std::unique_ptr<cmCTestGenericHandler> InitializeHandler(
    HandlerArguments& arguments, cmExecutionStatus& status) const;

  virtual void ProcessAdditionalValues(cmCTestGenericHandler*,
                                       HandlerArguments const& arguments,
                                       cmExecutionStatus& status) const;
};