Android-cuttlefish cvd tool
cf_configs_common.h
Go to the documentation of this file.
1/*
2 * Copyright (C) 2022 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 <type_traits>
21#include <vector>
22
23#include <fmt/format.h>
24#include <fmt/ranges.h> // NOLINT(misc-include-cleaner): version difference
25#include <google/protobuf/message.h>
26#include <json/json.h>
27
29#include "cuttlefish/host/commands/cvd/cli/parser/load_config.pb.h"
30
31namespace cuttlefish {
32
33template <typename T>
34std::string GenerateFlag(const std::string& name, const T& value) {
35 return fmt::format("--{}={}", name, value);
36}
37
38template <typename T>
39std::string GenerateVecFlag(const std::string& name, const T& collection) {
40 return fmt::format("--{}={}", name, fmt::join(collection, ","));
41}
42
43template <typename T>
45 const std::string& name,
46 const cvd::config::EnvironmentSpecification& config, T callback) {
47 if constexpr (std::is_invocable_v<T, const cvd::config::Instance&, size_t>) {
48 std::vector<decltype(callback(config.instances()[0], 0))> values;
49 for (size_t i = 0; i < config.instances_size(); ++i) {
50 values.emplace_back(callback(config.instances(i), i));
51 }
52 return GenerateVecFlag(name, values);
53 } else {
54 std::vector<decltype(callback(config.instances()[0]))> values;
55 for (size_t i = 0; i < config.instances_size(); ++i) {
56 values.emplace_back(callback(config.instances(i)));
57 }
58 return GenerateVecFlag(name, values);
59 }
60}
61
62template <typename T>
64 const std::string& name,
65 const cvd::config::EnvironmentSpecification& config, T callback) {
66 std::vector<std::decay_t<decltype(*callback(config.instances()[0]))>> values;
67 for (const auto& instance : config.instances()) {
68 values.emplace_back(CF_EXPECT(callback(instance)));
69 }
70 return GenerateVecFlag(name, values);
71}
72
73std::vector<std::string> MergeResults(std::vector<std::string> first_list,
74 std::vector<std::string> scond_list);
75
76void MergeTwoJsonObjs(Json::Value& dst, const Json::Value& src);
77
78} // namespace cuttlefish
Definition: expected.h:86
#define CF_EXPECT(...)
Definition: result.h:414
EventFormat format
Definition: kernel_log_server.cc:57
Definition: alloc_utils.cpp:23
std::vector< std::string > MergeResults(std::vector< std::string > first_list, std::vector< std::string > scond_list)
Definition: cf_configs_common.cpp:28
std::string GenerateFlag(const std::string &name, const T &value)
Definition: cf_configs_common.h:34
void MergeTwoJsonObjs(Json::Value &dst, const Json::Value &src)
This function merges two json objects and override json tree in dst with src json keys.
Definition: cf_configs_common.cpp:44
std::string GenerateInstanceFlag(const std::string &name, const cvd::config::EnvironmentSpecification &config, T callback)
Definition: cf_configs_common.h:44
std::string GenerateVecFlag(const std::string &name, const T &collection)
Definition: cf_configs_common.h:39
Result< std::string > ResultInstanceFlag(const std::string &name, const cvd::config::EnvironmentSpecification &config, T callback)
Definition: cf_configs_common.h:63