mirror of
https://github.com/mpusz/mp-units.git
synced 2025-07-31 19:04:27 +02:00
refactor: quantity_io added
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
|
||||
#include <units/quantity_io.h>
|
||||
#include "./length.h"
|
||||
#include "./acceleration.h"
|
||||
#include "./density.h"
|
||||
|
@@ -24,6 +24,7 @@
|
||||
#include <units/physical/si/derived/resistance.h>
|
||||
#include <units/physical/si/base/time.h>
|
||||
#include <units/math.h>
|
||||
#include <units/quantity_io.h>
|
||||
#include "./voltage.h"
|
||||
#include <iostream>
|
||||
|
||||
|
@@ -17,6 +17,7 @@
|
||||
|
||||
#include <iostream>
|
||||
#include <units/format.h>
|
||||
#include <units/quantity_io.h>
|
||||
|
||||
#include "./length.h"
|
||||
#include "./volume.h"
|
||||
|
@@ -18,6 +18,7 @@
|
||||
|
||||
#include "./units_str.h"
|
||||
#include "./length.h"
|
||||
#include <units/quantity_io.h>
|
||||
#include <iostream>
|
||||
|
||||
/*
|
||||
|
@@ -1,4 +1,5 @@
|
||||
|
||||
#include <units/quantity_io.h>
|
||||
#include <iostream>
|
||||
#include "./timer.h"
|
||||
|
||||
|
@@ -23,6 +23,7 @@
|
||||
#include <units/physical/si/cgs/derived/speed.h>
|
||||
#include <units/physical/si/international/derived/speed.h>
|
||||
#include <units/physical/si/derived/speed.h>
|
||||
#include <units/quantity_io.h>
|
||||
#include <iostream>
|
||||
|
||||
namespace {
|
||||
|
@@ -7,6 +7,7 @@
|
||||
#include <units/physical/si/base/time.h>
|
||||
#include <units/physical/si/derived/volume.h>
|
||||
#include <units/format.h>
|
||||
#include <units/quantity_io.h>
|
||||
#include <cassert>
|
||||
|
||||
namespace {
|
||||
|
@@ -25,6 +25,7 @@
|
||||
#include <units/physical/si/base/time.h>
|
||||
#include <units/physical/si/derived/voltage.h>
|
||||
#include <units/math.h>
|
||||
#include <units/quantity_io.h>
|
||||
#include <iostream>
|
||||
|
||||
int main()
|
||||
|
@@ -25,6 +25,7 @@
|
||||
#include <units/physical/si/typographic/base/length.h>
|
||||
#include <units/physical/si/us/base/length.h>
|
||||
#include <units/physical/si/derived/volume.h>
|
||||
#include <units/quantity_io.h>
|
||||
#include <iostream>
|
||||
|
||||
namespace {
|
||||
|
@@ -17,6 +17,7 @@
|
||||
|
||||
#include <units/physical/si/base/length.h>
|
||||
#include <units/format.h>
|
||||
#include <units/quantity_io.h>
|
||||
#include <iostream>
|
||||
|
||||
/*
|
||||
|
@@ -20,7 +20,7 @@
|
||||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
// SOFTWARE.
|
||||
|
||||
#include <units/quantity.h>
|
||||
#include <units/quantity_io.h>
|
||||
#include <units/physical/si/prefixes.h>
|
||||
#include <iostream>
|
||||
|
||||
|
@@ -25,6 +25,7 @@
|
||||
#include <units/physical/si/base/length.h>
|
||||
#include <units/physical/si/derived/energy.h>
|
||||
#include <units/physical/si/derived/torque.h>
|
||||
#include <units/quantity_io.h>
|
||||
#include <iostream>
|
||||
|
||||
using namespace units;
|
||||
|
@@ -23,6 +23,7 @@
|
||||
#include <units/format.h>
|
||||
#include <units/physical/si/international/derived/speed.h>
|
||||
#include <units/physical/si/derived/speed.h>
|
||||
#include <units/quantity_io.h>
|
||||
#include <iostream>
|
||||
|
||||
using namespace units::physical;
|
||||
|
@@ -24,13 +24,14 @@
|
||||
#include <units/physical/si/derived/force.h>
|
||||
#include <units/physical/si/derived/energy.h>
|
||||
#include <units/format.h>
|
||||
#include <units/quantity_io.h>
|
||||
#include <linear_algebra.hpp>
|
||||
#include <iostream>
|
||||
|
||||
namespace {
|
||||
namespace STD_LA {
|
||||
|
||||
template<class ET, class OT>
|
||||
std::ostream& operator<<(std::ostream& os, const std::math::vector<ET, OT>& v)
|
||||
std::ostream& operator<<(std::ostream& os, const vector<ET, OT>& v)
|
||||
{
|
||||
os << "|";
|
||||
for (auto i = 0U; i < v.size(); ++i) {
|
||||
@@ -41,7 +42,7 @@ std::ostream& operator<<(std::ostream& os, const std::math::vector<ET, OT>& v)
|
||||
}
|
||||
|
||||
template<class ET, class OT>
|
||||
std::ostream& operator<<(std::ostream& os, const std::math::matrix<ET, OT>& v)
|
||||
std::ostream& operator<<(std::ostream& os, const matrix<ET, OT>& v)
|
||||
{
|
||||
for (auto i = 0U; i < v.rows(); ++i) {
|
||||
os << "|";
|
||||
@@ -53,6 +54,9 @@ std::ostream& operator<<(std::ostream& os, const std::math::matrix<ET, OT>& v)
|
||||
return os;
|
||||
}
|
||||
|
||||
} // namespace STD_LA
|
||||
|
||||
namespace {
|
||||
|
||||
using namespace units::physical;
|
||||
using namespace units::physical::si::literals;
|
||||
@@ -345,7 +349,7 @@ void quantity_of_matrix_tests()
|
||||
quantity_of_matrix_divide_by_scalar();
|
||||
}
|
||||
|
||||
}
|
||||
} // namespace
|
||||
|
||||
int main()
|
||||
{
|
||||
|
@@ -21,6 +21,7 @@
|
||||
// SOFTWARE.
|
||||
|
||||
#include <units/physical/si/derived/acceleration.h>
|
||||
#include <units/quantity_io.h>
|
||||
#include <cmath>
|
||||
#include <compare>
|
||||
#include <iostream>
|
||||
|
@@ -26,6 +26,7 @@
|
||||
#include <units/physical/si/derived/speed.h>
|
||||
#include <units/physical/si/constants.h>
|
||||
#include <units/math.h>
|
||||
#include <units/quantity_io.h>
|
||||
#include <iostream>
|
||||
|
||||
namespace {
|
||||
|
@@ -21,6 +21,7 @@
|
||||
// SOFTWARE.
|
||||
|
||||
#include <units/physical/si/derived/speed.h>
|
||||
#include <units/quantity_io.h>
|
||||
#include <iostream>
|
||||
|
||||
namespace {
|
||||
|
48
src/include/units/bits/external/fixed_string.h
vendored
48
src/include/units/bits/external/fixed_string.h
vendored
@@ -23,11 +23,11 @@
|
||||
#pragma once
|
||||
|
||||
#include <units/bits/external/hacks.h>
|
||||
#include <algorithm>
|
||||
#include <cstdlib>
|
||||
#include <ostream>
|
||||
#include <compare>
|
||||
|
||||
// TODO use <algorithm> when moved to C++20 modules (parsing takes too long for each translation unit)
|
||||
|
||||
namespace units {
|
||||
|
||||
/**
|
||||
@@ -52,14 +52,15 @@ struct basic_fixed_string {
|
||||
}
|
||||
|
||||
[[nodiscard]] constexpr std::size_t size() const noexcept { return N; }
|
||||
[[nodiscard]] constexpr const CharT* c_str() const noexcept { return data_; }
|
||||
[[nodiscard]] constexpr const CharT& operator[](std::size_t index) const noexcept { return data_[index]; }
|
||||
[[nodiscard]] constexpr CharT operator[](std::size_t index) noexcept { return data_[index]; }
|
||||
[[nodiscard]] constexpr const CharT* data() const noexcept { return data_; }
|
||||
[[nodiscard]] constexpr const CharT* c_str() const noexcept { return data(); }
|
||||
[[nodiscard]] constexpr const CharT& operator[](std::size_t index) const noexcept { return data()[index]; }
|
||||
[[nodiscard]] constexpr CharT operator[](std::size_t index) noexcept { return data()[index]; }
|
||||
|
||||
[[nodiscard]] constexpr iterator begin() noexcept { return std::begin(data_); }
|
||||
[[nodiscard]] constexpr const_iterator begin() const noexcept { return std::begin(data_); }
|
||||
[[nodiscard]] constexpr iterator end() noexcept { return std::end(data_); }
|
||||
[[nodiscard]] constexpr const_iterator end() const noexcept { return std::end(data_); }
|
||||
[[nodiscard]] constexpr iterator begin() noexcept { return data(); }
|
||||
[[nodiscard]] constexpr const_iterator begin() const noexcept { return data(); }
|
||||
[[nodiscard]] constexpr iterator end() noexcept { return data() + size(); }
|
||||
[[nodiscard]] constexpr const_iterator end() const noexcept { return data() + size(); }
|
||||
|
||||
template<std::size_t N2>
|
||||
[[nodiscard]] constexpr friend basic_fixed_string<CharT, N + N2> operator+(
|
||||
@@ -75,7 +76,14 @@ struct basic_fixed_string {
|
||||
|
||||
[[nodiscard]] constexpr bool operator==(const basic_fixed_string& other) const
|
||||
{
|
||||
return std::ranges::equal(*this, other);
|
||||
if (size() != other.size())
|
||||
return false;
|
||||
for (size_t i = 0; i != size(); ++i) {
|
||||
if ((*this)[i] != other[i])
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
// return std::ranges::equal(*this, other);
|
||||
}
|
||||
|
||||
template<std::size_t N2>
|
||||
@@ -84,14 +92,20 @@ struct basic_fixed_string {
|
||||
template<std::size_t N2>
|
||||
[[nodiscard]] friend constexpr auto operator<=>(const basic_fixed_string& lhs, const basic_fixed_string<CharT, N2>& rhs)
|
||||
{
|
||||
return std::lexicographical_compare_three_way(lhs.begin(), lhs.end(), rhs.begin(), rhs.end());
|
||||
}
|
||||
auto first1 = lhs.begin();
|
||||
const auto last1 = lhs.end();
|
||||
auto first2 = rhs.begin();
|
||||
const auto last2 = rhs.end();
|
||||
auto comp = std::compare_three_way();
|
||||
|
||||
template<class Traits>
|
||||
friend std::basic_ostream<CharT, Traits>& operator<<(std::basic_ostream<CharT, Traits>& os,
|
||||
const basic_fixed_string& txt)
|
||||
{
|
||||
return os << txt.c_str();
|
||||
for (; first1 != last1 && first2 != last2; ++first1, ++first2)
|
||||
if (auto cmp = comp(*first1, *first2); cmp != 0)
|
||||
return cmp;
|
||||
|
||||
return first1 != last1 ? std::strong_ordering::greater :
|
||||
first2 != last2 ? std::strong_ordering::less :
|
||||
std::strong_ordering::equal;
|
||||
// return std::lexicographical_compare_three_way(lhs.begin(), lhs.end(), rhs.begin(), rhs.end());
|
||||
}
|
||||
};
|
||||
|
||||
|
36
src/include/units/bits/external/fixed_string_io.h
vendored
Normal file
36
src/include/units/bits/external/fixed_string_io.h
vendored
Normal file
@@ -0,0 +1,36 @@
|
||||
// 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 <units/bits/external/fixed_string.h>
|
||||
#include <ostream>
|
||||
|
||||
namespace units {
|
||||
|
||||
template<typename CharT, class Traits, std::size_t N>
|
||||
std::basic_ostream<CharT, Traits>& operator<<(std::basic_ostream<CharT, Traits>& os, const basic_fixed_string<CharT, N>& txt)
|
||||
{
|
||||
return os << txt.c_str();
|
||||
}
|
||||
|
||||
} // namespace units
|
@@ -314,21 +314,6 @@ public:
|
||||
#endif
|
||||
|
||||
[[nodiscard]] friend constexpr bool operator==(const quantity& lhs, const quantity& rhs) = default;
|
||||
|
||||
template<class CharT, class Traits>
|
||||
friend std::basic_ostream<CharT, Traits>& operator<<(std::basic_ostream<CharT, Traits>& os, const quantity& q)
|
||||
requires requires { os << q.count(); }
|
||||
{
|
||||
if(os.width()) {
|
||||
// std::setw() applies to the whole quantity output so it has to be first put into std::string
|
||||
std::basic_ostringstream<CharT, Traits> s;
|
||||
detail::to_stream(s, q);
|
||||
return os << s.str();
|
||||
}
|
||||
|
||||
detail::to_stream(os, q);
|
||||
return os;
|
||||
}
|
||||
};
|
||||
|
||||
// CTAD
|
||||
|
46
src/include/units/quantity_io.h
Normal file
46
src/include/units/quantity_io.h
Normal file
@@ -0,0 +1,46 @@
|
||||
|
||||
// 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 <units/bits/external/fixed_string_io.h>
|
||||
#include <units/quantity.h>
|
||||
|
||||
namespace units {
|
||||
|
||||
template<typename CharT, typename Traits, typename D, typename U, typename Rep>
|
||||
std::basic_ostream<CharT, Traits>& operator<<(std::basic_ostream<CharT, Traits>& os, const quantity<D, U, Rep>& q)
|
||||
requires requires { os << q.count(); }
|
||||
{
|
||||
if(os.width()) {
|
||||
// std::setw() applies to the whole quantity output so it has to be first put into std::string
|
||||
std::basic_ostringstream<CharT, Traits> s;
|
||||
detail::to_stream(s, q);
|
||||
return os << s.str();
|
||||
}
|
||||
|
||||
detail::to_stream(os, q);
|
||||
return os;
|
||||
}
|
||||
|
||||
} // namespace units
|
@@ -21,6 +21,7 @@
|
||||
// SOFTWARE.
|
||||
|
||||
#include <units/data/data.h>
|
||||
#include <units/quantity_io.h>
|
||||
#include <catch2/catch.hpp>
|
||||
#include <sstream>
|
||||
|
||||
|
@@ -20,10 +20,11 @@
|
||||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
// SOFTWARE.
|
||||
|
||||
#include "units/format.h"
|
||||
#include "units/math.h"
|
||||
#include "units/physical/si/si.h"
|
||||
#include "units/physical/si/cgs/cgs.h"
|
||||
#include <units/format.h>
|
||||
#include <units/math.h>
|
||||
#include <units/physical/si/si.h>
|
||||
#include <units/physical/si/cgs/cgs.h>
|
||||
#include <units/quantity_io.h>
|
||||
#include <catch2/catch.hpp>
|
||||
#include <iomanip>
|
||||
#include <sstream>
|
||||
|
@@ -21,6 +21,7 @@
|
||||
// SOFTWARE.
|
||||
|
||||
#include <units/physical/si/derived/speed.h>
|
||||
#include <units/quantity_io.h>
|
||||
#include <iostream>
|
||||
|
||||
using namespace units;
|
||||
|
Reference in New Issue
Block a user