Android-cuttlefish cvd tool
container.h
Go to the documentation of this file.
1//
2// Copyright (C) 2026 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 <string_view>
21#include <vector>
22
23#include "absl/strings/str_cat.h"
24#include "absl/strings/str_format.h"
25#include "absl/strings/str_join.h"
26
27#include "cuttlefish/pretty/numeric.h" // IWYU pragma: export
29#include "cuttlefish/pretty/string.h" // IWYU pragma: export
30
31namespace cuttlefish {
32
50 public:
51 template <typename Iterator, typename FmtIteratorFn>
53
54 template <typename Iterator, typename FmtIteratorFn>
56 FmtIteratorFn format_iterator_fn);
57
58 template <typename T, typename FmtMemberFn>
59 friend PrettyContainerType PrettyContainer(const T& container,
60 FmtMemberFn format_member_fn);
61
62 template <typename T>
63 friend PrettyContainerType PrettyContainer(const T& container);
64
65 template <typename Sink>
66 friend void AbslStringify(Sink& sink, const PrettyContainerType& pc) {
67 if (pc.members_.empty()) {
68 sink.Append("{}");
69 } else {
70 absl::Format(&sink, "{\n %v\n}", absl::StrJoin(pc.members_, ",\n "));
71 }
72 }
73
74 private:
76
77 void MemberInternal(std::string_view);
78
79 std::vector<std::string> members_;
80};
81
82template <typename Iterator, typename FmtIteratorFn>
84 FmtIteratorFn format_iterator_fn) {
86 for (auto it = begin; it != end; it++) {
87 pretty.MemberInternal(absl::StrCat(format_iterator_fn(it)));
88 }
89 return pretty;
90}
91
92template <typename Iterator, typename FmtIteratorFn>
94 return PrettyIterable(begin, end, [](const auto& iterator) {
95 return Pretty(*iterator, PrettyAdlPlaceholder());
96 });
97}
98
99template <typename T, typename FmtMemberFn>
101 FmtMemberFn format_member_fn) {
102 return PrettyIterable(std::begin(container), std::end(container),
103 [&format_member_fn](const auto& iterator) {
104 return format_member_fn(*iterator);
105 });
106}
107
108template <typename T>
110 return PrettyContainer(container, [](const auto& member) {
111 return Pretty(member, PrettyAdlPlaceholder());
112 });
113}
114
115std::ostream& operator<<(std::ostream& out, const PrettyContainerType&);
116
117// For libfmt
118std::string format_as(const PrettyContainerType& pc);
119
120} // namespace cuttlefish
Definition: container.h:49
friend PrettyContainerType PrettyContainer(const T &container, FmtMemberFn format_member_fn)
Definition: container.h:100
std::vector< std::string > members_
Definition: container.h:79
friend void AbslStringify(Sink &sink, const PrettyContainerType &pc)
Definition: container.h:66
friend PrettyContainerType PrettyIterable(Iterator begin, Iterator end)
Definition: container.h:93
void MemberInternal(std::string_view)
Definition: container.cc:29
std::set< Range >::iterator Iterator
Definition: disjoint_range_set.cc:66
Definition: alloc_driver.h:20
std::string Format(const TimeStamp &time_point)
Definition: instance_database_types.cpp:42
PrettyContainerType PrettyContainer(const T &container, FmtMemberFn format_member_fn)
Definition: container.h:100
PrettyStruct Pretty(AndroidBuild &build, PrettyAdlPlaceholder)
Definition: android_build.cc:72
PrettyContainerType PrettyIterable(Iterator begin, Iterator end, FmtIteratorFn format_iterator_fn)
Definition: container.h:83
std::ostream & operator<<(std::ostream &out, DeviceType device_type)
Definition: device_type.cpp:72
std::string_view format_as(DeviceType device_type)
Definition: device_type.cpp:76
Definition: pretty.h:20