Android-cuttlefish cvd tool
utils.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 <algorithm>
19#include <sstream>
20#include <string>
21#include <type_traits>
22#include <vector>
23
25
26namespace cuttlefish {
27template <typename T>
28constexpr typename std::underlying_type_t<T> Enum2Base(T t) {
29 return static_cast<typename std::underlying_type_t<T>>(t);
30}
31} // end of namespace cuttlefish
32
33namespace cuttlefish {
34namespace confui {
35template <typename Delim, typename... Args>
36std::string ArgsToStringWithDelim(Delim&& delim, Args&&... args) {
37 std::stringstream ss;
38 ([&ss, &delim](auto& arg) { ss << arg << delim; }(args), ...);
39 auto result = ss.str();
40 std::string delim_str(delim);
41 if (!result.empty() && !delim_str.empty()) {
42 for (int i = 0; i < delim_str.size(); i++) {
43 result.pop_back();
44 }
45 }
46 return result;
47}
48
49// make t... to a single string with no blank in between
50template <typename... Args>
51std::string ArgsToString(Args&&... args) {
52 return ArgsToStringWithDelim("", std::forward<Args>(args)...);
53}
54
55inline bool IsOnlyDigits(const std::string& src) {
56 return std::all_of(src.begin(), src.end(),
57 [](int c) -> bool { return std::isdigit(c); });
58}
59
60// note that no () surrounding LOG(level) << "ConfUI:" is crucial
61#define ConfUiLog(LOG_LEVEL) LOG(LOG_LEVEL) << "ConfUI: "
62
63// TODO(kwstephenkim@google.com): make these look more like LOG(level)
64#define ConfUiCheck(cond) CHECK(cond) << "ConfUI: "
65
66} // end of namespace confui
67} // end of namespace cuttlefish
std::string ArgsToStringWithDelim(Delim &&delim, Args &&... args)
Definition: utils.h:36
std::string ArgsToString(Args &&... args)
Definition: utils.h:51
bool IsOnlyDigits(const std::string &src)
Definition: utils.h:55
Definition: alloc_utils.cpp:23
constexpr std::underlying_type_t< T > Enum2Base(T t)
Definition: utils.h:28
std::vector< std::string_view > Args
Definition: incremental.h:28