refactor: quantity_io added

This commit is contained in:
Mateusz Pusz
2020-12-28 15:18:36 +01:00
parent 008dec79ea
commit cff718301c
24 changed files with 144 additions and 41 deletions
+31 -17
View File
@@ -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
View 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
-15
View File
@@ -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
View 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