Commit ceef4721 authored by Scott Graham's avatar Scott Graham Committed by Commit Bot

base/numerics: Add #define to allow excluding ostream

Add a define to help make base/numerics suitable for use in constrained
environments.

BASE_NUMERICS_DISABLE_OSTREAM_OPERATORS removes a few uses of ostream.

This is motivated by attempting to use base/numerics in the Fuchsia
kernel. ostream is simply unavailable, but what's lost is fairly minor.

This is part of a downstream CL from johngro@:
https://fuchsia-review.googlesource.com/c/fuchsia/+/292396

Change-Id: I1d7a2651e50d8e62c143b048f253b1f4c32971cc
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1669874Reviewed-by: default avatarJustin Schuh <jschuh@chromium.org>
Commit-Queue: Scott Graham <scottmg@chromium.org>
Cr-Commit-Position: refs/heads/master@{#671366}
parent 4104e138
...@@ -192,12 +192,14 @@ constexpr ClampedNumeric<typename UnderlyingType<T>::type> MakeClampedNum( ...@@ -192,12 +192,14 @@ constexpr ClampedNumeric<typename UnderlyingType<T>::type> MakeClampedNum(
return value; return value;
} }
#if !BASE_NUMERICS_DISABLE_OSTREAM_OPERATORS
// Overload the ostream output operator to make logging work nicely. // Overload the ostream output operator to make logging work nicely.
template <typename T> template <typename T>
std::ostream& operator<<(std::ostream& os, const ClampedNumeric<T>& value) { std::ostream& operator<<(std::ostream& os, const ClampedNumeric<T>& value) {
os << static_cast<T>(value); os << static_cast<T>(value);
return os; return os;
} }
#endif
// These implement the variadic wrapper for the math operations. // These implement the variadic wrapper for the math operations.
template <template <typename, typename, typename> class M, template <template <typename, typename, typename> class M,
......
...@@ -8,7 +8,6 @@ ...@@ -8,7 +8,6 @@
#include <stddef.h> #include <stddef.h>
#include <limits> #include <limits>
#include <ostream>
#include <type_traits> #include <type_traits>
#include "base/numerics/safe_conversions_impl.h" #include "base/numerics/safe_conversions_impl.h"
...@@ -20,6 +19,10 @@ ...@@ -20,6 +19,10 @@
#define BASE_HAS_OPTIMIZED_SAFE_CONVERSIONS (0) #define BASE_HAS_OPTIMIZED_SAFE_CONVERSIONS (0)
#endif #endif
#if !BASE_NUMERICS_DISABLE_OSTREAM_OPERATORS
#include <ostream>
#endif
namespace base { namespace base {
namespace internal { namespace internal {
...@@ -308,12 +311,14 @@ constexpr StrictNumeric<typename UnderlyingType<T>::type> MakeStrictNum( ...@@ -308,12 +311,14 @@ constexpr StrictNumeric<typename UnderlyingType<T>::type> MakeStrictNum(
return value; return value;
} }
#if !BASE_NUMERICS_DISABLE_OSTREAM_OPERATORS
// Overload the ostream output operator to make logging work nicely. // Overload the ostream output operator to make logging work nicely.
template <typename T> template <typename T>
std::ostream& operator<<(std::ostream& os, const StrictNumeric<T>& value) { std::ostream& operator<<(std::ostream& os, const StrictNumeric<T>& value) {
os << static_cast<T>(value); os << static_cast<T>(value);
return os; return os;
} }
#endif
#define BASE_NUMERIC_COMPARISON_OPERATORS(CLASS, NAME, OP) \ #define BASE_NUMERIC_COMPARISON_OPERATORS(CLASS, NAME, OP) \
template <typename L, typename R, \ template <typename L, typename R, \
......
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