Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-08-28 08:27:10

0001 // Licensed to the Apache Software Foundation (ASF) under one
0002 // or more contributor license agreements.  See the NOTICE file
0003 // distributed with this work for additional information
0004 // regarding copyright ownership.  The ASF licenses this file
0005 // to you under the Apache License, Version 2.0 (the
0006 // "License"); you may not use this file except in compliance
0007 // with the License.  You may obtain a copy of the License at
0008 //
0009 //   http://www.apache.org/licenses/LICENSE-2.0
0010 //
0011 // Unless required by applicable law or agreed to in writing,
0012 // software distributed under the License is distributed on an
0013 // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
0014 // KIND, either express or implied.  See the License for the
0015 // specific language governing permissions and limitations
0016 // under the License. template <typename T>
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 }  // namespace detail
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     // Avoid losing precision when printing floating point numbers
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 /// CRTP helper for declaring string representation. Defines operator<<
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 }  // namespace util
0090 }  // namespace arrow