File indexing completed on 2025-08-28 08:27:10
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018 #pragma once
0019
0020 #include <memory>
0021 #include <ostream>
0022 #include <string>
0023 #include <type_traits>
0024 #include <utility>
0025
0026 #include "arrow/util/visibility.h"
0027
0028 namespace arrow {
0029 namespace util {
0030
0031 namespace detail {
0032
0033 class ARROW_EXPORT StringStreamWrapper {
0034 public:
0035 StringStreamWrapper();
0036 ~StringStreamWrapper();
0037
0038 std::ostream& stream() { return ostream_; }
0039 std::string str();
0040
0041 protected:
0042 std::unique_ptr<std::ostringstream> sstream_;
0043 std::ostream& ostream_;
0044 };
0045
0046 }
0047
0048 template <typename Head>
0049 void StringBuilderRecursive(std::ostream& stream, Head&& head) {
0050 if constexpr (std::is_floating_point_v<std::decay_t<Head>>) {
0051
0052 stream << std::to_string(head);
0053 } else {
0054 stream << head;
0055 }
0056 }
0057
0058 template <typename Head, typename... Tail>
0059 void StringBuilderRecursive(std::ostream& stream, Head&& head, Tail&&... tail) {
0060 StringBuilderRecursive(stream, std::forward<Head>(head));
0061 StringBuilderRecursive(stream, std::forward<Tail>(tail)...);
0062 }
0063
0064 template <typename... Args>
0065 std::string StringBuilder(Args&&... args) {
0066 detail::StringStreamWrapper ss;
0067 StringBuilderRecursive(ss.stream(), std::forward<Args>(args)...);
0068 return ss.str();
0069 }
0070
0071
0072 template <typename T>
0073 class ToStringOstreamable {
0074 public:
0075 ~ToStringOstreamable() {
0076 static_assert(
0077 std::is_same<decltype(std::declval<const T>().ToString()), std::string>::value,
0078 "ToStringOstreamable depends on the method T::ToString() const");
0079 }
0080
0081 private:
0082 const T& cast() const { return static_cast<const T&>(*this); }
0083
0084 friend inline std::ostream& operator<<(std::ostream& os, const ToStringOstreamable& t) {
0085 return os << t.cast().ToString();
0086 }
0087 };
0088
0089 }
0090 }