Commit 9687a8ea by Zie Weaver Committed by Copybara-Service

Add a template gadget to detect whether a type supports the ostream operator.

I.e. for a type `Example`, if
```
std::ostream& operator<<(std::ostream&, const Example&);
```
is declared, `absl::HasOstreamOperator<Example>::value` is `true`.
PiperOrigin-RevId: 574637891
Change-Id: I123d8f35a6e3ea894745133f7d81b1610fb475d5
parent 7676c565
......@@ -307,6 +307,7 @@ set(ABSL_INTERNAL_DLL_FILES
"strings/internal/stringify_sink.h"
"strings/internal/stringify_sink.cc"
"strings/has_absl_stringify.h"
"strings/has_ostream_operator.h"
"strings/match.cc"
"strings/match.h"
"strings/numbers.cc"
......
......@@ -194,6 +194,31 @@ cc_test(
visibility = ["//visibility:private"],
deps = [
":strings",
"//absl/types:optional",
"@com_google_googletest//:gtest",
"@com_google_googletest//:gtest_main",
],
)
cc_library(
name = "has_ostream_operator",
hdrs = ["has_ostream_operator.h"],
copts = ABSL_DEFAULT_COPTS,
linkopts = ABSL_DEFAULT_LINKOPTS,
deps = [
"//absl/base:config",
],
)
cc_test(
name = "has_ostream_operator_test",
size = "small",
srcs = ["has_ostream_operator_test.cc"],
copts = ABSL_TEST_COPTS,
visibility = ["//visibility:private"],
deps = [
":has_ostream_operator",
"//absl/types:optional",
"@com_google_googletest//:gtest",
"@com_google_googletest//:gtest_main",
],
......
......@@ -103,6 +103,18 @@ absl_cc_library(
PUBLIC
)
absl_cc_library(
NAME
has_ostream_operator
HDRS
"has_ostream_operator.h"
COPTS
${ABSL_DEFAULT_COPTS}
DEPS
absl::config
PUBLIC
)
# Internal-only target, do not depend on directly.
absl_cc_library(
NAME
......@@ -162,12 +174,26 @@ absl_cc_test(
COPTS
${ABSL_TEST_COPTS}
DEPS
absl::optional
absl::strings
GTest::gmock_main
)
absl_cc_test(
NAME
has_ostream_operator_test
SRCS
"has_ostream_operator_test.cc"
COPTS
${ABSL_TEST_COPTS}
DEPS
absl::has_ostream_operator
absl::optional
GTest::gmock_main
)
absl_cc_test(
NAME
ascii_test
SRCS
"ascii_test.cc"
......
......@@ -17,6 +17,7 @@
#include <string>
#include "gtest/gtest.h"
#include "absl/types/optional.h"
namespace {
......@@ -32,6 +33,8 @@ TEST(HasAbslStringifyTest, Works) {
EXPECT_FALSE(absl::HasAbslStringify<std::string>::value);
EXPECT_FALSE(absl::HasAbslStringify<TypeWithoutAbslStringify>::value);
EXPECT_TRUE(absl::HasAbslStringify<TypeWithAbslStringify>::value);
EXPECT_FALSE(
absl::HasAbslStringify<absl::optional<TypeWithAbslStringify>>::value);
}
} // namespace
// Copyright 2023 The Abseil Authors
//
// 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
//
// https://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 ABSL_STRINGS_HAS_OSTREAM_OPERATOR_H_
#define ABSL_STRINGS_HAS_OSTREAM_OPERATOR_H_
#include <ostream>
#include <type_traits>
#include <utility>
#include "absl/base/config.h"
namespace absl {
ABSL_NAMESPACE_BEGIN
// Detects if type `T` supports streaming to `std::ostream`s with `operator<<`.
template <typename T, typename = void>
struct HasOstreamOperator : std::false_type {};
template <typename T>
struct HasOstreamOperator<
T, std::enable_if_t<std::is_same<
std::ostream&, decltype(std::declval<std::ostream&>()
<< std::declval<const T&>())>::value>>
: std::true_type {};
ABSL_NAMESPACE_END
} // namespace absl
#endif // ABSL_STRINGS_HAS_OSTREAM_OPERATOR_H_
// Copyright 2023 The Abseil Authors
//
// 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
//
// https://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.
#include "absl/strings/has_ostream_operator.h"
#include <ostream>
#include <string>
#include "gtest/gtest.h"
#include "absl/types/optional.h"
namespace {
struct TypeWithoutOstreamOp {};
struct TypeWithOstreamOp {
friend std::ostream& operator<<(std::ostream& os, const TypeWithOstreamOp&) {
return os;
}
};
TEST(HasOstreamOperatorTest, Works) {
EXPECT_TRUE(absl::HasOstreamOperator<int>::value);
EXPECT_TRUE(absl::HasOstreamOperator<std::string>::value);
EXPECT_FALSE(absl::HasOstreamOperator<absl::optional<int>>::value);
EXPECT_FALSE(absl::HasOstreamOperator<TypeWithoutOstreamOp>::value);
EXPECT_TRUE(absl::HasOstreamOperator<TypeWithOstreamOp>::value);
}
} // namespace
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment