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 
17 #pragma once
18 
19 #include <string>
20 #include <tuple>
21 #include <vector>
22 
23 #include <fruit/fruit.h>
24 
25 namespace cuttlefish {
26 
27 class DiagnosticInformation {
28  public:
29   virtual ~DiagnosticInformation();
30   virtual std::vector<std::string> Diagnostics() const = 0;
31 
32   static void PrintAll(const std::vector<DiagnosticInformation*>&);
33 };
34 
35 template <auto Fn, typename R, typename... Args>
36 class DiagnosticInformationFn : public DiagnosticInformation {
37  public:
INJECT(DiagnosticInformationFn (Args...args))38   INJECT(DiagnosticInformationFn(Args... args))
39       : args_(std::forward_as_tuple(args...)) {}
40 
Diagnostics()41   std::vector<std::string> Diagnostics() const override {
42     if constexpr (std::is_same_v<R, std::vector<std::string>>) {
43       return std::apply(Fn, args_);
44     } else if constexpr (std::is_same_v<R, std::string>) {
45       return {std::apply(Fn, args_)};
46     } else {
47       static_assert(false, "Unexpected AutoDiagnostic return type");
48     }
49   }
50 
51  private:
52   std::tuple<Args...> args_;
53 };
54 
55 template <auto Fn1, typename Fn2>
56 struct DiagnosticInformationFnImpl;
57 
58 template <auto Fn, typename R, typename... Args>
59 struct DiagnosticInformationFnImpl<Fn, R (*)(Args...)> {
60   using Type = DiagnosticInformationFn<Fn, R, Args...>;
61 
62   static fruit::Component<
63       fruit::Required<typename std::remove_reference_t<Args>...>, Type>
64   Component() {
65     return fruit::createComponent()
66         .template addMultibinding<DiagnosticInformation, Type>();
67   }
68 };
69 
70 template <auto Fn>
71 using AutoDiagnostic = DiagnosticInformationFnImpl<Fn, decltype(Fn)>;
72 
73 }  // namespace cuttlefish
74