mirror of
https://github.com/mpusz/mp-units.git
synced 2025-06-24 08:41:34 +02:00
refactor: ostream.h
header file made deprecated
This commit is contained in:
@ -34,7 +34,6 @@ import std;
|
||||
#ifdef MP_UNITS_MODULES
|
||||
import mp_units;
|
||||
#else
|
||||
#include <mp-units/ostream.h>
|
||||
#include <mp-units/systems/cgs.h>
|
||||
#include <mp-units/systems/international.h>
|
||||
#include <mp-units/systems/isq.h>
|
||||
|
@ -29,7 +29,6 @@ import std;
|
||||
import mp_units;
|
||||
#else
|
||||
#include <mp-units/math.h>
|
||||
#include <mp-units/ostream.h>
|
||||
#include <mp-units/systems/isq/electromagnetism.h>
|
||||
#include <mp-units/systems/si.h>
|
||||
#endif
|
||||
|
@ -25,7 +25,6 @@ import std;
|
||||
#ifdef MP_UNITS_MODULES
|
||||
import mp_units;
|
||||
#else
|
||||
#include <mp-units/ostream.h>
|
||||
#include <mp-units/systems/iau.h>
|
||||
#include <mp-units/systems/imperial.h>
|
||||
#include <mp-units/systems/international.h> // IWYU pragma: keep
|
||||
|
@ -35,7 +35,6 @@ import std;
|
||||
import mp_units.core;
|
||||
#else
|
||||
#include <mp-units/framework.h>
|
||||
#include <mp-units/ostream.h>
|
||||
#endif
|
||||
|
||||
using namespace mp_units;
|
||||
|
@ -36,7 +36,6 @@ import std;
|
||||
#ifdef MP_UNITS_MODULES
|
||||
import mp_units;
|
||||
#else
|
||||
#include <mp-units/ostream.h>
|
||||
#include <mp-units/systems/international.h>
|
||||
#include <mp-units/systems/isq.h>
|
||||
#include <mp-units/systems/si.h>
|
||||
|
@ -35,7 +35,6 @@ import std;
|
||||
import mp_units;
|
||||
#else
|
||||
#include <mp-units/framework.h>
|
||||
#include <mp-units/ostream.h>
|
||||
#include <mp-units/systems/isq/space_and_time.h>
|
||||
#include <mp-units/systems/si.h>
|
||||
#endif
|
||||
|
@ -28,7 +28,6 @@ import std;
|
||||
#ifdef MP_UNITS_MODULES
|
||||
import mp_units;
|
||||
#else
|
||||
#include <mp-units/ostream.h>
|
||||
#include <mp-units/systems/isq_angle.h>
|
||||
#include <mp-units/systems/si.h>
|
||||
#endif
|
||||
|
@ -30,7 +30,6 @@ import std;
|
||||
import mp_units;
|
||||
#else
|
||||
#include <mp-units/math.h>
|
||||
#include <mp-units/ostream.h>
|
||||
#include <mp-units/systems/isq/mechanics.h>
|
||||
#include <mp-units/systems/isq/space_and_time.h>
|
||||
#include <mp-units/systems/natural.h>
|
||||
|
@ -90,6 +90,7 @@ if(NOT ${projectPrefix}API_FREESTANDING)
|
||||
FILES
|
||||
include/mp-units/bits/fmt.h
|
||||
include/mp-units/bits/format.h
|
||||
include/mp-units/bits/ostream.h
|
||||
include/mp-units/bits/requires_hosted.h
|
||||
include/mp-units/ext/format.h
|
||||
include/mp-units/cartesian_vector.h
|
||||
|
58
src/core/include/mp-units/bits/ostream.h
Normal file
58
src/core/include/mp-units/bits/ostream.h
Normal file
@ -0,0 +1,58 @@
|
||||
|
||||
// The MIT License (MIT)
|
||||
//
|
||||
// Copyright (c) 2018 Mateusz Pusz
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
// of this software and associated documentation files (the "Software"), to deal
|
||||
// in the Software without restriction, including without limitation the rights
|
||||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
// copies of the Software, and to permit persons to whom the Software is
|
||||
// furnished to do so, subject to the following conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be included in all
|
||||
// copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
// SOFTWARE.
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <mp-units/bits/requires_hosted.h>
|
||||
//
|
||||
#include <mp-units/bits/module_macros.h>
|
||||
|
||||
#ifndef MP_UNITS_IN_MODULE_INTERFACE
|
||||
#ifdef MP_UNITS_IMPORT_STD
|
||||
import std;
|
||||
#else
|
||||
#include <cstdint>
|
||||
#include <sstream>
|
||||
#endif
|
||||
#endif
|
||||
|
||||
namespace mp_units::detail {
|
||||
|
||||
template<typename CharT, class Traits, std::invocable<std::basic_ostream<CharT, Traits>&> F>
|
||||
std::basic_ostream<CharT, Traits>& to_stream(std::basic_ostream<CharT, Traits>& os, const F& func)
|
||||
{
|
||||
if (os.width()) {
|
||||
// std::setw() applies to the whole output so it has to be first put into std::string
|
||||
std::basic_ostringstream<CharT, Traits> oss;
|
||||
oss.flags(os.flags());
|
||||
oss.imbue(os.getloc());
|
||||
oss.precision(os.precision());
|
||||
func(oss);
|
||||
return os << std::move(oss).str();
|
||||
}
|
||||
|
||||
func(os);
|
||||
return os;
|
||||
}
|
||||
|
||||
} // namespace mp_units::detail
|
@ -30,7 +30,6 @@
|
||||
#if MP_UNITS_HOSTED
|
||||
#include <mp-units/cartesian_vector.h>
|
||||
#include <mp-units/math.h>
|
||||
#include <mp-units/ostream.h>
|
||||
#include <mp-units/random.h>
|
||||
#endif
|
||||
// IWYU pragma: end_exports
|
||||
|
@ -33,6 +33,10 @@
|
||||
#include <mp-units/framework/dimension_concepts.h>
|
||||
#include <mp-units/framework/symbol_text.h>
|
||||
#include <mp-units/framework/symbolic_expression.h>
|
||||
#if MP_UNITS_HOSTED
|
||||
#include <mp-units/bits/format.h>
|
||||
#include <mp-units/bits/ostream.h>
|
||||
#endif
|
||||
|
||||
#ifndef MP_UNITS_IN_MODULE_INTERFACE
|
||||
#include <mp-units/ext/contracts.h>
|
||||
@ -314,12 +318,22 @@ MP_UNITS_EXPORT template<dimension_symbol_formatting fmt = dimension_symbol_form
|
||||
return detail::dimension_symbol_result<fmt, CharT, D>.view();
|
||||
}
|
||||
|
||||
#if MP_UNITS_HOSTED
|
||||
|
||||
MP_UNITS_EXPORT template<typename CharT, typename Traits, Dimension D>
|
||||
std::basic_ostream<CharT, Traits>& operator<<(std::basic_ostream<CharT, Traits>& os, D d)
|
||||
{
|
||||
return detail::to_stream(os, [&](std::basic_ostream<CharT, Traits>& oss) {
|
||||
dimension_symbol_to<CharT>(std::ostream_iterator<CharT>(oss), d);
|
||||
});
|
||||
}
|
||||
|
||||
#endif // MP_UNITS_HOSTED
|
||||
|
||||
} // namespace mp_units
|
||||
|
||||
#if MP_UNITS_HOSTED
|
||||
|
||||
#include <mp-units/bits/format.h>
|
||||
|
||||
//
|
||||
// Grammar
|
||||
//
|
||||
|
@ -36,6 +36,10 @@
|
||||
#include <mp-units/framework/representation_concepts.h>
|
||||
#include <mp-units/framework/unit_concepts.h>
|
||||
#include <mp-units/framework/value_cast.h>
|
||||
#if MP_UNITS_HOSTED
|
||||
#include <mp-units/bits/format.h>
|
||||
#include <mp-units/bits/ostream.h>
|
||||
#endif
|
||||
|
||||
#ifndef MP_UNITS_IN_MODULE_INTERFACE
|
||||
#include <mp-units/ext/contracts.h>
|
||||
@ -45,6 +49,9 @@ import std;
|
||||
#include <compare> // IWYU pragma: export
|
||||
#include <limits>
|
||||
#include <utility>
|
||||
#if MP_UNITS_HOSTED
|
||||
#include <locale>
|
||||
#endif
|
||||
#endif
|
||||
#endif
|
||||
|
||||
@ -647,6 +654,25 @@ template<QuantityLike Q>
|
||||
explicit(quantity_like_traits<Q>::explicit_import) quantity(Q)
|
||||
-> quantity<quantity_like_traits<Q>::reference, typename quantity_like_traits<Q>::rep>;
|
||||
|
||||
#if MP_UNITS_HOSTED
|
||||
|
||||
template<typename CharT, typename Traits, auto R, typename Rep>
|
||||
std::basic_ostream<CharT, Traits>& operator<<(std::basic_ostream<CharT, Traits>& os, const quantity<R, Rep>& q)
|
||||
requires requires { os << q.numerical_value_ref_in(q.unit); }
|
||||
{
|
||||
return detail::to_stream(os, [&](std::basic_ostream<CharT, Traits>& oss) {
|
||||
if constexpr (is_same_v<Rep, std::uint8_t> || is_same_v<Rep, std::int8_t>)
|
||||
// promote the value to int
|
||||
oss << +q.numerical_value_ref_in(q.unit);
|
||||
else
|
||||
oss << q.numerical_value_ref_in(q.unit);
|
||||
if constexpr (space_before_unit_symbol<get_unit(R)>) oss << " ";
|
||||
oss << q.unit;
|
||||
});
|
||||
}
|
||||
|
||||
#endif // MP_UNITS_HOSTED
|
||||
|
||||
MP_UNITS_EXPORT_END
|
||||
|
||||
} // namespace mp_units
|
||||
@ -737,9 +763,6 @@ public:
|
||||
|
||||
#if MP_UNITS_HOSTED
|
||||
|
||||
#include <mp-units/bits/format.h>
|
||||
#include <locale>
|
||||
|
||||
//
|
||||
// Grammar
|
||||
//
|
||||
|
@ -40,6 +40,10 @@
|
||||
#include <mp-units/framework/unit_concepts.h>
|
||||
#include <mp-units/framework/unit_magnitude.h>
|
||||
#include <mp-units/framework/unit_symbol_formatting.h>
|
||||
#if MP_UNITS_HOSTED
|
||||
#include <mp-units/bits/format.h>
|
||||
#include <mp-units/bits/ostream.h>
|
||||
#endif
|
||||
|
||||
#ifndef MP_UNITS_IN_MODULE_INTERFACE
|
||||
#include <mp-units/ext/contracts.h>
|
||||
@ -913,12 +917,21 @@ MP_UNITS_EXPORT template<unit_symbol_formatting fmt = unit_symbol_formatting{},
|
||||
return detail::unit_symbol_result<fmt, CharT, U>.view();
|
||||
}
|
||||
|
||||
#if MP_UNITS_HOSTED
|
||||
|
||||
MP_UNITS_EXPORT template<typename CharT, typename Traits, Unit U>
|
||||
std::basic_ostream<CharT, Traits>& operator<<(std::basic_ostream<CharT, Traits>& os, U u)
|
||||
{
|
||||
return detail::to_stream(
|
||||
os, [&](std::basic_ostream<CharT, Traits>& oss) { unit_symbol_to<CharT>(std::ostream_iterator<CharT>(oss), u); });
|
||||
}
|
||||
|
||||
#endif // MP_UNITS_HOSTED
|
||||
|
||||
} // namespace mp_units
|
||||
|
||||
#if MP_UNITS_HOSTED
|
||||
|
||||
#include <mp-units/bits/format.h>
|
||||
|
||||
//
|
||||
// Grammar
|
||||
//
|
||||
|
@ -23,82 +23,4 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <mp-units/bits/requires_hosted.h>
|
||||
//
|
||||
#include <mp-units/bits/module_macros.h>
|
||||
#include <mp-units/framework/quantity.h>
|
||||
#include <mp-units/framework/unit.h>
|
||||
|
||||
#ifndef MP_UNITS_IN_MODULE_INTERFACE
|
||||
#ifdef MP_UNITS_IMPORT_STD
|
||||
import std;
|
||||
#else
|
||||
#include <cstdint>
|
||||
#include <sstream>
|
||||
#endif
|
||||
#endif
|
||||
|
||||
namespace mp_units {
|
||||
|
||||
namespace detail {
|
||||
|
||||
template<typename CharT, class Traits, Dimension D>
|
||||
void to_stream_impl(std::basic_ostream<CharT, Traits>& os, D d)
|
||||
{
|
||||
dimension_symbol_to<CharT>(std::ostream_iterator<CharT>(os), d);
|
||||
}
|
||||
|
||||
template<typename CharT, class Traits, Unit U>
|
||||
void to_stream_impl(std::basic_ostream<CharT, Traits>& os, U u)
|
||||
{
|
||||
unit_symbol_to<CharT>(std::ostream_iterator<CharT>(os), u);
|
||||
}
|
||||
|
||||
template<typename CharT, class Traits, auto R, typename Rep>
|
||||
void to_stream_impl(std::basic_ostream<CharT, Traits>& os, const quantity<R, Rep>& q)
|
||||
requires requires { os << q.numerical_value_ref_in(q.unit); }
|
||||
{
|
||||
if constexpr (is_same_v<Rep, std::uint8_t> || is_same_v<Rep, std::int8_t>)
|
||||
// promote the value to int
|
||||
os << +q.numerical_value_ref_in(q.unit);
|
||||
else
|
||||
os << q.numerical_value_ref_in(q.unit);
|
||||
if constexpr (space_before_unit_symbol<get_unit(R)>) os << " ";
|
||||
to_stream_impl(os, get_unit(R));
|
||||
}
|
||||
|
||||
template<typename CharT, class Traits, typename T>
|
||||
std::basic_ostream<CharT, Traits>& to_stream(std::basic_ostream<CharT, Traits>& os, const T& val)
|
||||
requires requires { detail::to_stream_impl(os, val); }
|
||||
{
|
||||
if (os.width()) {
|
||||
// std::setw() applies to the whole output so it has to be first put into std::string
|
||||
std::basic_ostringstream<CharT, Traits> oss;
|
||||
oss.flags(os.flags());
|
||||
oss.imbue(os.getloc());
|
||||
oss.precision(os.precision());
|
||||
detail::to_stream_impl(oss, val);
|
||||
return os << std::move(oss).str();
|
||||
}
|
||||
|
||||
detail::to_stream_impl(os, val);
|
||||
return os;
|
||||
}
|
||||
|
||||
template<typename OStream, typename T>
|
||||
constexpr bool is_mp_units_stream = requires(OStream os, T v) { detail::to_stream_impl(os, v); };
|
||||
|
||||
} // namespace detail
|
||||
|
||||
MP_UNITS_EXPORT_BEGIN
|
||||
|
||||
template<typename CharT, typename Traits, typename T>
|
||||
std::basic_ostream<CharT, Traits>& operator<<(std::basic_ostream<CharT, Traits>& os, const T& val)
|
||||
requires detail::is_mp_units_stream<std::basic_ostream<CharT, Traits>, T>
|
||||
{
|
||||
return detail::to_stream(os, val);
|
||||
}
|
||||
|
||||
MP_UNITS_EXPORT_END
|
||||
|
||||
} // namespace mp_units
|
||||
#warning "This header file is deprecated and does not have to be included anymore."
|
||||
|
@ -29,7 +29,6 @@ import std;
|
||||
#ifdef MP_UNITS_MODULES
|
||||
import mp_units;
|
||||
#else
|
||||
#include <mp-units/ostream.h> // IWYU pragma: keep
|
||||
#include <mp-units/systems/isq/space_and_time.h>
|
||||
#include <mp-units/systems/si.h>
|
||||
#endif
|
||||
|
@ -34,7 +34,6 @@ import std;
|
||||
#ifdef MP_UNITS_MODULES
|
||||
import mp_units;
|
||||
#else
|
||||
#include <mp-units/ostream.h> // IWYU pragma: keep
|
||||
#include <mp-units/random.h>
|
||||
#include <mp-units/systems/isq/space_and_time.h>
|
||||
#include <mp-units/systems/si.h>
|
||||
|
@ -40,7 +40,6 @@ import std;
|
||||
import mp_units;
|
||||
#else
|
||||
#include <mp-units/cartesian_vector.h>
|
||||
#include <mp-units/ostream.h> // IWYU pragma: keep
|
||||
#include <mp-units/systems/cgs.h>
|
||||
#include <mp-units/systems/isq/electromagnetism.h>
|
||||
#include <mp-units/systems/isq/mechanics.h>
|
||||
|
@ -33,7 +33,6 @@ import std;
|
||||
import mp_units;
|
||||
#else
|
||||
#include <mp-units/math.h>
|
||||
#include <mp-units/ostream.h> // IWYU pragma: keep
|
||||
#include <mp-units/systems/isq/mechanics.h>
|
||||
#include <mp-units/systems/isq/space_and_time.h>
|
||||
#include <mp-units/systems/si.h>
|
||||
|
@ -32,7 +32,6 @@ import std;
|
||||
import mp_units;
|
||||
#else
|
||||
#include <mp-units/math.h>
|
||||
#include <mp-units/ostream.h> // IWYU pragma: keep
|
||||
#include <mp-units/systems/angular.h>
|
||||
#include <mp-units/systems/isq/space_and_time.h>
|
||||
#include <mp-units/systems/si.h>
|
||||
|
@ -34,7 +34,6 @@ import std;
|
||||
#ifdef MP_UNITS_MODULES
|
||||
import mp_units;
|
||||
#else
|
||||
#include <mp-units/ostream.h> // IWYU pragma: keep
|
||||
#include <mp-units/systems/angular.h>
|
||||
#endif
|
||||
|
||||
|
@ -30,7 +30,6 @@ import std;
|
||||
#ifdef MP_UNITS_MODULES
|
||||
import mp_units;
|
||||
#else
|
||||
#include <mp-units/ostream.h>
|
||||
#include <mp-units/systems/isq.h>
|
||||
#include <mp-units/systems/si.h>
|
||||
#endif
|
||||
|
Reference in New Issue
Block a user