Android-cuttlefish cvd tool
struct.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 <utility>
22#include <vector>
23
24#include "absl/strings/str_cat.h"
25#include "absl/strings/str_format.h"
26#include "absl/strings/str_join.h"
27
28#include "cuttlefish/pretty/numeric.h" // IWYU pragma: export
30#include "cuttlefish/pretty/string.h" // IWYU pragma: export
31
32namespace cuttlefish {
33
61 public:
62 explicit PrettyStruct(std::string_view name);
63
64 template <typename T>
65 PrettyStruct& Member(std::string_view name, const T& value) & {
67 absl::StrCat(name, ": ", Pretty(value, PrettyAdlPlaceholder())));
68 return *this;
69 }
70
71 template <typename T>
72 PrettyStruct Member(std::string_view name, const T& value) && {
73 this->Member(name, value);
74 return std::move(*this);
75 }
76
77 template <typename Sink>
78 friend void AbslStringify(Sink& sink, const PrettyStruct& ps) {
79 if (ps.members_.empty()) {
80 absl::Format(&sink, "%v {}", ps.name_);
81 } else {
82 absl::Format(&sink, "%v {\n %v\n}", ps.name_,
83 absl::StrJoin(ps.members_, ",\n "));
84 }
85 }
86
87 private:
88 void MemberInternal(std::string_view);
89
90 std::string name_;
91 std::vector<std::string> members_;
92};
93
94std::ostream& operator<<(std::ostream&, const PrettyStruct&);
95
96// For libfmt
97std::string format_as(const PrettyStruct&);
98
99} // namespace cuttlefish
Definition: struct.h:60
PrettyStruct Member(std::string_view name, const T &value) &&
Definition: struct.h:72
void MemberInternal(std::string_view)
Definition: struct.cc:29
PrettyStruct(std::string_view name)
Definition: struct.cc:27
std::string name_
Definition: struct.h:90
PrettyStruct & Member(std::string_view name, const T &value) &
Definition: struct.h:65
friend void AbslStringify(Sink &sink, const PrettyStruct &ps)
Definition: struct.h:78
std::vector< std::string > members_
Definition: struct.h:91
char * name
Definition: libf2fs.c:1403
Definition: alloc_driver.h:20
std::string Format(const TimeStamp &time_point)
Definition: instance_database_types.cpp:42
PrettyStruct Pretty(AndroidBuild &build, PrettyAdlPlaceholder)
Definition: android_build.cc:72
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