Skip to content

Commit

Permalink
universal printer
Browse files Browse the repository at this point in the history
Signed-off-by: dentiny <dentinyhao@gmail.com>
  • Loading branch information
dentiny committed Mar 8, 2025
1 parent 9ed6ec6 commit 53c7780
Show file tree
Hide file tree
Showing 23 changed files with 1,478 additions and 0 deletions.
12 changes: 12 additions & 0 deletions src/ray/util/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,10 @@ ray_cc_library(
ray_cc_library(
name = "type_traits",
hdrs = ["type_traits.h"],
deps = [
"@com_google_absl//absl/meta:type_traits",
"@com_google_absl//absl/strings:str_format",
],
)

ray_cc_library(
Expand Down Expand Up @@ -373,3 +377,11 @@ ray_cc_library(
":logging",
],
)

ray_cc_library(
name = "visitor",
hdrs = ["visitor.h"],
deps = [
"@com_google_absl//absl/meta:type_traits",
],
)
156 changes: 156 additions & 0 deletions src/ray/util/strings/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
load("//bazel:ray.bzl", "ray_cc_library", "ray_cc_test")

package(default_visibility = ["//visibility:public"])

ray_cc_library(
name = "type_printer",
hdrs = ["type_printer.h"],
deps = [
":get_type_name",
"@boost//:core",
],
)

ray_cc_library(
name = "std_printer",
hdrs = ["std_printer.h"],
deps = [
":ostream_printer",
],
)

ray_cc_test(
name = "std_printer_test",
srcs = ["std_printer_test.cc"],
tags = ["team:core"],
deps = [
":std_printer",
"@com_google_absl//absl/strings:str_format",
"@com_google_googletest//:gtest_main",
],
)

ray_cc_library(
name = "debug_string_printer",
hdrs = ["debug_string_printer.h"],
)

ray_cc_test(
name = "debug_string_printer_test",
srcs = ["debug_string_printer_test.cc"],
tags = ["team:core"],
deps = [
":debug_string_printer",
"@com_google_googletest//:gtest_main",
],
)

ray_cc_library(
name = "absl_stringify_printer",
hdrs = ["absl_stringify_printer.h"],
deps = [
"@com_google_absl//absl/strings",
],
)

ray_cc_test(
name = "absl_stringify_printer_test",
srcs = ["absl_stringify_printer_test.cc"],
tags = ["team:core"],
deps = [
":absl_stringify_printer",
"@com_google_absl//absl/strings:str_format",
"@com_google_googletest//:gtest_main",
],
)

ray_cc_library(
name = "container_printer",
hdrs = ["container_printer.h"],
deps = [
":ostream_printer",
"//src/ray/util:type_traits",
],
)

ray_cc_test(
name = "container_printer_test",
srcs = ["container_printer_test.cc"],
tags = ["team:core"],
deps = [
":container_printer",
"//src/ray/util:type_traits",
"//src/ray/util:visitor",
"@com_google_googletest//:gtest_main",
],
)

ray_cc_library(
name = "ostream_printer",
hdrs = ["ostream_printer.h"],
)

ray_cc_test(
name = "ostream_printer_test",
srcs = ["ostream_printer_test.cc"],
tags = ["team:core"],
deps = [
":ostream_printer",
"@com_google_googletest//:gtest_main",
],
)

ray_cc_library(
name = "enum_printer",
hdrs = ["enum_printer.h"],
)

ray_cc_test(
name = "enum_printer_test",
srcs = ["enum_printer_test.cc"],
tags = ["team:core"],
deps = [
":enum_printer",
"@com_google_googletest//:gtest_main",
],
)

ray_cc_library(
name = "debug_printer",
hdrs = ["debug_printer.h"],
deps = [
":absl_stringify_printer",
":container_printer",
":debug_string_printer",
":enum_printer",
":ostream_printer",
":std_printer",
":type_printer",
"//src/ray/util:visitor",
],
)

ray_cc_test(
name = "debug_printer_test",
srcs = ["debug_printer_test.cc"],
tags = ["team:core"],
deps = [
":debug_printer",
"@com_google_googletest//:gtest_main",
],
)

ray_cc_library(
name = "get_type_name",
hdrs = ["get_type_name.h"],
)

ray_cc_test(
name = "get_type_name_test",
srcs = ["get_type_name_test.cc"],
tags = ["team:core"],
deps = [
":get_type_name",
"@com_google_googletest//:gtest_main",
],
)
47 changes: 47 additions & 0 deletions src/ray/util/strings/absl_stringify_printer.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// Copyright 2025 The Ray 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
//
// 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.

// Printer for types which defines `AbslStringify`.

#pragma once

#include <iostream>
#include <type_traits>

#include "absl/strings/str_cat.h"

namespace ray {

// TODO(hjiang): Later version of abseil contains abseil string defined trait.
template <typename T, typename = void, typename = void>
struct is_stringifyable : std::false_type {};

template <typename T, typename SinkType>
struct is_stringifyable<T,
SinkType,
std::void_t<decltype(AbslStringify(std::declval<SinkType &>(),
std::declval<const T &>()))>>
: std::true_type {};

template <typename T>
inline constexpr bool is_stringifyable_v = is_stringifyable<T, std::ostream>::value;

struct AbslStringifyPrinter {
template <typename T, std::enable_if_t<is_stringifyable_v<T>, int> = 0>
void operator()(std::ostream &os, const T &obj) const {
os << absl::StrCat(obj);
}
};

} // namespace ray
47 changes: 47 additions & 0 deletions src/ray/util/strings/absl_stringify_printer_test.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// Copyright 2025 The Ray 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
//
// 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.

#include "ray/util/strings/absl_stringify_printer.h"

#include <gtest/gtest.h>

#include "absl/strings/str_format.h"

namespace ray {

namespace {

struct Point {
template <typename Sink>
friend void AbslStringify(Sink &sink, const Point &p) {
absl::Format(&sink, "(%d, %d)", p.x, p.y);
}

int x;
int y;
};

TEST(AbslStringifyTest, BasicTest) {
Point point;
point.x = 10;
point.y = 20;

std::stringstream ss;
AbslStringifyPrinter{}(ss, point);
EXPECT_EQ(ss.str(), "(10, 20)");
}

} // namespace

} // namespace ray
Loading

0 comments on commit 53c7780

Please sign in to comment.