lin
2025-08-01 633231e833e21d5b8b1c00cb15aedb62b3b78e8f
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
/*
 * Copyright (C) 2015 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
 
#ifndef ART_CMDLINE_DETAIL_CMDLINE_PARSER_DETAIL_H_
#define ART_CMDLINE_DETAIL_CMDLINE_PARSER_DETAIL_H_
 
#include <sstream>
#include <string>
#include <vector>
 
namespace art {
// Implementation details for some template querying. Don't look inside if you hate templates.
namespace detail {
template <typename T>
typename std::remove_reference<T>::type& FakeReference();
 
// SupportsInsertionOperator<T, TStream>::value will evaluate to a boolean,
// whose value is true if the TStream class supports the << operator against T,
// and false otherwise.
template <typename T2, typename TStream2 = std::ostream>
struct SupportsInsertionOperator {
 private:
  template <typename TStream, typename T>
  static std::true_type InsertionOperatorTest(TStream& os, const T& value,
                                              std::remove_reference<decltype(os << value)>* = 0);  // NOLINT [whitespace/operators] [3]
 
  template <typename TStream, typename ... T>
  static std::false_type InsertionOperatorTest(TStream& os, const T& ... args);
 
 public:
  static constexpr bool value =
      decltype(InsertionOperatorTest(FakeReference<TStream2>(), std::declval<T2>()))::value;
};
 
template <typename TLeft, typename TRight = TLeft, bool IsFloatingPoint = false>
struct SupportsEqualityOperatorImpl;
 
template <typename TLeft, typename TRight>
struct SupportsEqualityOperatorImpl<TLeft, TRight, false> {
 private:
  template <typename TL, typename TR>
  static std::true_type EqualityOperatorTest(const TL& left, const TR& right,
                                             std::remove_reference<decltype(left == right)>* = 0);  // NOLINT [whitespace/operators] [3]
 
  template <typename TL, typename ... T>
  static std::false_type EqualityOperatorTest(const TL& left, const T& ... args);
 
 public:
  static constexpr bool value =
      decltype(EqualityOperatorTest(std::declval<TLeft>(), std::declval<TRight>()))::value;
};
 
// Partial specialization when TLeft/TRight are both floating points.
// This is a work-around because decltype(floatvar1 == floatvar2)
// will not compile with clang:
// error: comparing floating point with == or != is unsafe [-Werror,-Wfloat-equal]
template <typename TLeft, typename TRight>
struct SupportsEqualityOperatorImpl<TLeft, TRight, true> {
  static constexpr bool value = true;
};
 
// SupportsEqualityOperatorImpl<T1, T2>::value will evaluate to a boolean,
// whose value is true if T1 can be compared against T2 with ==,
// and false otherwise.
template <typename TLeft, typename TRight = TLeft>
struct SupportsEqualityOperator :  // NOLINT [whitespace/labels] [4]
    SupportsEqualityOperatorImpl<TLeft, TRight,
                                 std::is_floating_point<TLeft>::value
                                 && std::is_floating_point<TRight>::value> {
};
 
// Convert any kind of type to an std::string, even if there's no
// serialization support for it. Unknown types get converted to an
// an arbitrary value.
//
// Meant for printing user-visible errors or unit test failures only.
template <typename T>
std::string ToStringAny(const T& value,
                        typename std::enable_if<
                            SupportsInsertionOperator<T>::value>::type* = nullptr) {
  std::stringstream stream;
  stream << value;
  return stream.str();
}
 
template <typename T>
std::string ToStringAny(const std::vector<T> value,
                        typename std::enable_if<
                            SupportsInsertionOperator<T>::value>::type* = nullptr) {
  std::stringstream stream;
  stream << "vector{";
 
  for (size_t i = 0; i < value.size(); ++i) {
    stream << ToStringAny(value[i]);
 
    if (i != value.size() - 1) {
      stream << ',';
    }
  }
 
  stream << "}";
  return stream.str();
}
 
template <typename T>
std::string ToStringAny(const T&,
                        typename std::enable_if<
                            !SupportsInsertionOperator<T>::value>::type* = nullptr
) {
  return std::string("(unknown type [no operator<< implemented] for )");
}
}  // namespace detail  // NOLINT [readability/namespace] [5]
}  // namespace art
 
#endif  // ART_CMDLINE_DETAIL_CMDLINE_PARSER_DETAIL_H_