Android-cuttlefish cvd tool
feature.h
Go to the documentation of this file.
1/*
2 * Copyright (C) 2021 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 <ostream>
19#include <string>
20#include <type_traits>
21#include <unordered_map>
22#include <unordered_set>
23#include <vector>
24
26#include <fruit/fruit.h>
27
30
31namespace cuttlefish {
32
33template <typename Subclass>
34class Feature {
35 public:
36 virtual ~Feature() = default;
37
38 virtual std::string Name() const = 0;
39
41 const std::unordered_set<Subclass*>& features,
42 const std::function<Result<void>(Subclass*)>& callback);
43
44 private:
45 virtual std::unordered_set<Subclass*> Dependencies() const = 0;
46};
47
48class SetupFeature : public virtual Feature<SetupFeature> {
49 public:
50 virtual ~SetupFeature();
51
52 static Result<void> RunSetup(const std::vector<SetupFeature*>& features);
53
54 virtual bool Enabled() const { return true; }
55
56 private:
58};
59
60template <typename T>
62 public:
64 if constexpr (std::is_void_v<T>) {
65 calculated_ = false;
66 } else {
67 calculated_ = {};
68 }
69 }
70 template <typename S = T>
71 std::enable_if_t<!std::is_void_v<S>, S&> operator*() {
72 CHECK(calculated_.has_value()) << "precondition violation";
73 return *calculated_;
74 }
75 template <typename S = T>
76 std::enable_if_t<!std::is_void_v<S>, const S&> operator*() const {
77 CHECK(calculated_.has_value()) << "precondition violation";
78 return *calculated_;
79 }
80 template <typename S = T>
81 std::enable_if_t<!std::is_void_v<S>, S*> operator->() {
82 CHECK(calculated_.has_value()) << "precondition violation";
83 return &*calculated_;
84 }
85 template <typename S = T>
86 std::enable_if_t<!std::is_void_v<S>, const S*> operator->() const {
87 CHECK(calculated_.has_value()) << "precondition violation";
88 return &*calculated_;
89 }
90 template <typename S = T>
91 std::enable_if_t<!std::is_void_v<S>, S> Move() {
92 CHECK(calculated_.has_value()) << "precondition violation";
93 return std::move(*calculated_);
94 }
95
96 private:
97 Result<void> ResultSetup() override final {
98 if constexpr (std::is_void_v<T>) {
99 CF_EXPECT(!calculated_, "precondition violation");
101 calculated_ = true;
102 } else {
103 CF_EXPECT(!calculated_.has_value(), "precondition violation");
105 }
106 return {};
107 }
108
109 virtual Result<T> Calculate() = 0;
110
111 std::conditional_t<std::is_void_v<T>, bool, std::optional<T>> calculated_;
112};
113
114class FlagFeature : public Feature<FlagFeature> {
115 public:
116 static Result<void> ProcessFlags(const std::vector<FlagFeature*>& features,
117 std::vector<std::string>& flags);
118 static bool WriteGflagsHelpXml(const std::vector<FlagFeature*>& features,
119 std::ostream& out);
120
121 private:
122 // Must be executed in dependency order following Dependencies(). Expected to
123 // mutate the `flags` argument to remove handled flags, and possibly introduce
124 // new flag values (e.g. from a file).
125 virtual Result<void> Process(std::vector<std::string>& flags) = 0;
126
127 // TODO(schuffelen): Migrate the xml help to human-readable help output after
128 // the gflags migration is done.
129
130 // Write an xml fragment that is compatible with gflags' `--helpxml` format.
131 virtual bool WriteGflagsCompatHelpXml(std::ostream& out) const = 0;
132};
133
134template <typename Subclass>
136 const std::unordered_set<Subclass*>& features,
137 const std::function<Result<void>(Subclass*)>& callback) {
138 enum class Status { UNVISITED, VISITING, VISITED };
139 std::unordered_map<Subclass*, Status> features_status;
140 for (const auto& feature : features) {
141 features_status[feature] = Status::UNVISITED;
142 }
143 std::function<Result<void>(Subclass*)> visit;
144 visit = [&callback, &features_status,
145 &visit](Subclass* feature) -> Result<void> {
146 CF_EXPECT(features_status.count(feature) > 0,
147 "Dependency edge to "
148 << feature->Name() << " but it is not part of the feature "
149 << "graph. This feature is either disabled or not correctly "
150 << "registered.");
151 if (features_status[feature] == Status::VISITED) {
152 return {};
153 }
154 CF_EXPECT(features_status[feature] != Status::VISITING,
155 "Cycle detected while visiting " << feature->Name());
156 features_status[feature] = Status::VISITING;
157 for (const auto& dependency : feature->Dependencies()) {
158 CF_EXPECT(dependency != nullptr,
159 "SetupFeature " << feature->Name() << " has a null dependency.");
160 CF_EXPECT(visit(dependency),
161 "Error detected while visiting " << feature->Name());
162 }
163 features_status[feature] = Status::VISITED;
164 CF_EXPECT(callback(feature), "Callback error on " << feature->Name());
165 return {};
166 };
167 for (const auto& feature : features) {
168 CF_EXPECT(visit(feature)); // `visit` will log the error chain.
169 }
170 return {};
171}
172
173template <typename... Args>
174std::unordered_set<SetupFeature*> SetupFeatureDeps(
175 const std::tuple<Args...>& args) {
176 std::unordered_set<SetupFeature*> deps;
177 std::apply(
178 [&deps](auto&&... arg) {
179 (
180 [&] {
181 using ArgType = std::remove_reference_t<decltype(arg)>;
182 if constexpr (std::is_base_of_v<SetupFeature, ArgType>) {
183 deps.insert(static_cast<SetupFeature*>(&arg));
184 }
185 }(),
186 ...);
187 },
188 args);
189 return deps;
190}
191
192template <auto Fn, typename R, typename... Args>
194 public:
195 INJECT(GenericReturningSetupFeature(Args... args))
196 : args_(std::forward_as_tuple(args...)) {}
197
198 bool Enabled() const override { return true; }
199
200 std::string Name() const override {
201 static constexpr auto kName = ValueName<Fn>();
202 return std::string(kName);
203 }
204
205 std::unordered_set<SetupFeature*> Dependencies() const override {
206 return SetupFeatureDeps(args_);
207 }
208
209 private:
210 Result<R> Calculate() override {
211 if constexpr (std::is_void_v<R>) {
212 CF_EXPECT(std::apply(Fn, args_));
213 return {};
214 } else {
215 return CF_EXPECT(std::apply(Fn, args_));
216 }
217 }
218 std::tuple<Args...> args_;
219};
220
221template <auto Fn1, typename Fn2>
223
224template <auto Fn, typename R, typename... Args>
225struct GenericSetupImpl<Fn, Result<R> (*)(Args...)> {
227
228 static fruit::Component<
229 fruit::Required<typename std::remove_reference_t<Args>...>, Type>
231 return fruit::createComponent()
232 .template addMultibinding<SetupFeature, Type>();
233 }
234};
235
236template <auto Fn>
237using AutoSetup = GenericSetupImpl<Fn, decltype(Fn)>;
238
239} // namespace cuttlefish
Definition: expected.h:86
Definition: feature.h:34
virtual std::string Name() const =0
virtual ~Feature()=default
virtual std::unordered_set< Subclass * > Dependencies() const =0
static Result< void > TopologicalVisit(const std::unordered_set< Subclass * > &features, const std::function< Result< void >(Subclass *)> &callback)
Definition: feature.h:135
Definition: feature.h:114
static bool WriteGflagsHelpXml(const std::vector< FlagFeature * > &features, std::ostream &out)
Definition: feature.cpp:74
virtual Result< void > Process(std::vector< std::string > &flags)=0
static Result< void > ProcessFlags(const std::vector< FlagFeature * > &features, std::vector< std::string > &flags)
Definition: feature.cpp:58
virtual bool WriteGflagsCompatHelpXml(std::ostream &out) const =0
std::string Name() const override
Definition: feature.h:200
std::unordered_set< SetupFeature * > Dependencies() const override
Definition: feature.h:205
Result< R > Calculate() override
Definition: feature.h:210
std::tuple< Args... > args_
Definition: feature.h:218
Definition: feature.h:61
ReturningSetupFeature()
Definition: feature.h:63
std::enable_if_t<!std::is_void_v< S >, S * > operator->()
Definition: feature.h:81
virtual Result< T > Calculate()=0
std::enable_if_t<!std::is_void_v< S >, const S & > operator*() const
Definition: feature.h:76
Result< void > ResultSetup() override final
Definition: feature.h:97
std::conditional_t< std::is_void_v< T >, bool, std::optional< T > > calculated_
Definition: feature.h:111
std::enable_if_t<!std::is_void_v< S >, const S * > operator->() const
Definition: feature.h:86
std::enable_if_t<!std::is_void_v< S >, S & > operator*()
Definition: feature.h:71
std::enable_if_t<!std::is_void_v< S >, S > Move()
Definition: feature.h:91
Definition: feature.h:48
virtual ~SetupFeature()
Definition: feature.cpp:30
virtual bool Enabled() const
Definition: feature.h:54
static Result< void > RunSetup(const std::vector< SetupFeature * > &features)
Definition: feature.cpp:32
virtual Result< void > ResultSetup()=0
#define CF_EXPECT(...)
Definition: result.h:414
#define CHECK(x)
Definition: logging.h:251
Definition: alloc_utils.cpp:23
Status
Definition: buffers.h:24
std::unordered_set< SetupFeature * > SetupFeatureDeps(const std::tuple< Args... > &args)
Definition: feature.h:174
std::vector< std::string_view > Args
Definition: incremental.h:28
static fruit::Component< fruit::Required< typename std::remove_reference_t< Args >... >, Type > Component()
Definition: feature.h:230
Definition: feature.h:222