/* * 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 #include #include namespace art { // Implementation details for some template querying. Don't look inside if you hate templates. namespace detail { template typename std::remove_reference::type& FakeReference(); // SupportsInsertionOperator::value will evaluate to a boolean, // whose value is true if the TStream class supports the << operator against T, // and false otherwise. template struct SupportsInsertionOperator { private: template static std::true_type InsertionOperatorTest(TStream& os, const T& value, std::remove_reference* = 0); // NOLINT [whitespace/operators] [3] template static std::false_type InsertionOperatorTest(TStream& os, const T& ... args); public: static constexpr bool value = decltype(InsertionOperatorTest(FakeReference(), std::declval()))::value; }; template struct SupportsEqualityOperatorImpl; template struct SupportsEqualityOperatorImpl { private: template static std::true_type EqualityOperatorTest(const TL& left, const TR& right, std::remove_reference* = 0); // NOLINT [whitespace/operators] [3] template static std::false_type EqualityOperatorTest(const TL& left, const T& ... args); public: static constexpr bool value = decltype(EqualityOperatorTest(std::declval(), std::declval()))::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 struct SupportsEqualityOperatorImpl { static constexpr bool value = true; }; // SupportsEqualityOperatorImpl::value will evaluate to a boolean, // whose value is true if T1 can be compared against T2 with ==, // and false otherwise. template struct SupportsEqualityOperator : // NOLINT [whitespace/labels] [4] SupportsEqualityOperatorImpl::value && std::is_floating_point::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 std::string ToStringAny(const T& value, typename std::enable_if< SupportsInsertionOperator::value>::type* = nullptr) { std::stringstream stream; stream << value; return stream.str(); } template std::string ToStringAny(const std::vector value, typename std::enable_if< SupportsInsertionOperator::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 std::string ToStringAny(const T&, typename std::enable_if< !SupportsInsertionOperator::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_