chromium/third_party/abseil-cpp/absl/random/internal/iostream_state_saver.h

// Copyright 2017 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_RANDOM_INTERNAL_IOSTREAM_STATE_SAVER_H_
#define ABSL_RANDOM_INTERNAL_IOSTREAM_STATE_SAVER_H_

#include <cmath>
#include <iostream>
#include <limits>
#include <type_traits>

#include "absl/meta/type_traits.h"
#include "absl/numeric/int128.h"

namespace absl {
ABSL_NAMESPACE_BEGIN
namespace random_internal {

// The null_state_saver does nothing.
template <typename T>
class null_state_saver {};

// ostream_state_saver is a RAII object to save and restore the common
// basic_ostream flags used when implementing `operator <<()` on any of
// the absl random distributions.
template <typename OStream>
class ostream_state_saver {};

#if defined(__NDK_MAJOR__) && __NDK_MAJOR__ < 16
#define ABSL_RANDOM_INTERNAL_IOSTREAM_HEXFLOAT
#else
#define ABSL_RANDOM_INTERNAL_IOSTREAM_HEXFLOAT
#endif

template <typename CharT, typename Traits>
ostream_state_saver<std::basic_ostream<CharT, Traits>> make_ostream_state_saver(
    std::basic_ostream<CharT, Traits>& os,  // NOLINT(runtime/references)
    std::ios_base::fmtflags flags = std::ios_base::dec | std::ios_base::left |
#if ABSL_RANDOM_INTERNAL_IOSTREAM_HEXFLOAT
                                    std::ios_base::fixed |
#endif
                                    std::ios_base::scientific) {}

template <typename T>
typename absl::enable_if_t<!std::is_base_of<std::ios_base, T>::value,
                           null_state_saver<T>>
make_ostream_state_saver(T& is,  // NOLINT(runtime/references)
                         std::ios_base::fmtflags flags = std::ios_base::dec) {}

// stream_precision_helper<type>::kPrecision returns the base 10 precision
// required to stream and reconstruct a real type exact binary value through
// a binary->decimal->binary transition.
template <typename T>
struct stream_precision_helper {};

template <>
struct stream_precision_helper<float> {};
template <>
struct stream_precision_helper<double> {};
template <>
struct stream_precision_helper<long double> {};

// istream_state_saver is a RAII object to save and restore the common
// std::basic_istream<> flags used when implementing `operator >>()` on any of
// the absl random distributions.
template <typename IStream>
class istream_state_saver {};

template <typename CharT, typename Traits>
istream_state_saver<std::basic_istream<CharT, Traits>> make_istream_state_saver(
    std::basic_istream<CharT, Traits>& is,  // NOLINT(runtime/references)
    std::ios_base::fmtflags flags = std::ios_base::dec |
                                    std::ios_base::scientific |
                                    std::ios_base::skipws) {}

template <typename T>
typename absl::enable_if_t<!std::is_base_of<std::ios_base, T>::value,
                           null_state_saver<T>>
make_istream_state_saver(T& is,  // NOLINT(runtime/references)
                         std::ios_base::fmtflags flags = std::ios_base::dec) {}

// stream_format_type<T> is a helper struct to convert types which
// basic_iostream cannot output as decimal numbers into types which
// basic_iostream can output as decimal numbers. Specifically:
// * signed/unsigned char-width types are converted to int.
// * TODO(lar): __int128 => uint128, except there is no operator << yet.
//
template <typename T>
struct stream_format_type
    : public std::conditional<(sizeof(T) == sizeof(char)), int, T> {};

// stream_u128_helper allows us to write out either absl::uint128 or
// __uint128_t types in the same way, which enables their use as internal
// state of PRNG engines.
template <typename T>
struct stream_u128_helper;

template <>
struct stream_u128_helper<absl::uint128> {};

#ifdef ABSL_HAVE_INTRINSIC_INT128
template <>
struct stream_u128_helper<__uint128_t> {};
#endif

template <typename FloatType, typename IStream>
inline FloatType read_floating_point(IStream& is) {}

}  // namespace random_internal
ABSL_NAMESPACE_END
}  // namespace absl

#endif  // ABSL_RANDOM_INTERNAL_IOSTREAM_STATE_SAVER_H_