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 
24 #include <android-base/logging.h>
25 
26 namespace cuttlefish {
27 template <typename T>
Enum2Base(T t)28 constexpr 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 
33 namespace cuttlefish {
34 namespace confui {
35 template <typename Delim, typename... Args>
ArgsToStringWithDelim(Delim && delim,Args &&...args)36 std::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
50 template <typename... Args>
ArgsToString(Args &&...args)51 std::string ArgsToString(Args&&... args) {
52   return ArgsToStringWithDelim("", std::forward<Args>(args)...);
53 }
54 
IsOnlyDigits(const std::string & src)55 inline 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
68