Android-cuttlefish cvd tool
auto_cmd.h
Go to the documentation of this file.
1//
2// Copyright (C) 2023 The Android Open Source Project
3//
4// Licensed under the Apache License, Version 2.0 (the "License");
5// you may not use this file except in compliance with the License.
6// You may obtain a copy of the License at
7//
8// http://www.apache.org/licenses/LICENSE-2.0
9//
10// Unless required by applicable law or agreed to in writing, software
11// distributed under the License is distributed on an "AS IS" BASIS,
12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13// See the License for the specific language governing permissions and
14// limitations under the License.
15
16#pragma once
17
18#include <optional>
19#include <string>
20#include <tuple>
21#include <type_traits>
22#include <utility>
23#include <vector>
24
25#include "fruit/fruit.h"
26
32
33namespace cuttlefish {
34
35template <class...>
36constexpr std::false_type CommandAlwaysFalse{};
37
38template <auto Fn, typename R, typename... Args>
41 public:
43 : args_(std::forward_as_tuple(args...)) {}
44
45 Result<void> ResultSetup() override {
46 commands_.clear();
47 if constexpr (std::is_same_v<R, Result<std::vector<MonitorCommand>>>) {
48 commands_ = CF_EXPECT(std::apply(Fn, args_));
49 } else if constexpr (std::is_same_v<R, std::vector<MonitorCommand>>) {
50 commands_ = std::apply(Fn, args_);
51 } else if constexpr (std::is_same_v<R, Result<MonitorCommand>>) {
52 commands_.emplace_back(CF_EXPECT(std::apply(Fn, args_)));
53 } else if constexpr (std::is_same_v<R, MonitorCommand>) {
54 commands_.emplace_back(std::apply(Fn, args_));
55 } else if constexpr (std::is_same_v<
57 auto cmd = CF_EXPECT(std::apply(Fn, args_));
58 if (cmd) {
59 commands_.emplace_back(std::move(*cmd));
60 }
61 } else if constexpr (std::is_same_v<R, std::optional<MonitorCommand>>) {
62 auto cmd = std::apply(Fn, args_);
63 if (cmd) {
64 commands_.emplace_back(std::move(*cmd));
65 }
66 } else {
67 static_assert(CommandAlwaysFalse<R>, "Unexpected AutoCmd return type");
68 }
69 return {};
70 }
71
72 std::string Name() const override {
73 static constexpr auto kName = ValueName<Fn>();
74 return std::string(kName);
75 }
76
77 std::unordered_set<SetupFeature*> Dependencies() const override {
78 return SetupFeatureDeps(args_);
79 }
80
82 return std::move(commands_);
83 }
84
85 private:
86 std::tuple<Args...> args_;
87 std::vector<MonitorCommand> commands_;
88};
89
90template <auto Fn1, typename Fn2>
92
93template <auto Fn, typename R, typename... Args>
94struct GenericCommandImpl<Fn, R (*)(Args...)> {
95 using Type = GenericCommandSource<Fn, R, Args...>;
96
97 static fruit::Component<
98 fruit::Required<typename std::remove_reference_t<Args>...>, Type>
100 auto cmd = fruit::createComponent()
101 .template addMultibinding<CommandSource, Type>()
102 .template addMultibinding<SetupFeature, Type>();
103 constexpr bool uses_kernel_log_pipe =
104 (std::is_base_of_v<KernelLogPipeProvider,
105 std::remove_reference_t<Args>> ||
106 ...);
107 if constexpr (uses_kernel_log_pipe) {
108 return cmd.template addMultibinding<KernelLogPipeConsumer, Type>();
109 } else {
110 return cmd;
111 }
112 }
113};
114
115template <auto Fn>
116using AutoCmd = GenericCommandImpl<Fn, decltype(Fn)>;
117
118} // namespace cuttlefish
Definition: command_source.h:37
Definition: auto_cmd.h:40
std::tuple< Args... > args_
Definition: auto_cmd.h:86
std::string Name() const override
Definition: auto_cmd.h:72
std::vector< MonitorCommand > commands_
Definition: auto_cmd.h:87
Result< std::vector< MonitorCommand > > Commands() override
Definition: auto_cmd.h:81
std::unordered_set< SetupFeature * > Dependencies() const override
Definition: auto_cmd.h:77
Definition: kernel_log_pipe_provider.h:32
Definition: kernel_log_pipe_provider.h:25
virtual Result< void > ResultSetup()=0
#define CF_EXPECT(...)
Definition: expect.h:149
@ INJECT
Definition: f2fs_fs.h:443
Definition: alloc_driver.h:20
static constexpr std::string_view kName
Definition: efi_loader.cc:30
std::unordered_set< SetupFeature * > SetupFeatureDeps(const std::tuple< Args... > &args)
Definition: feature.h:175
tl::expected< T, StackTraceError > Result
Definition: result_type.h:30
constexpr std::false_type CommandAlwaysFalse
Definition: auto_cmd.h:36
std::vector< std::string_view > Args
Definition: incremental.h:28
#define R(x, n)
Definition: sha512.c:86
static fruit::Component< fruit::Required< typename std::remove_reference_t< Args >... >, Type > Component()
Definition: auto_cmd.h:99
Definition: auto_cmd.h:91