Base dimensions refactored

-
This commit is contained in:
Mateusz Pusz
2019-10-15 19:03:45 +02:00
parent c3c5a740d9
commit d31d2cf439
9 changed files with 108 additions and 68 deletions
+54 -21
View File
@@ -24,31 +24,64 @@
namespace units {
// TODO gcc:92101
// Gated by the following gcc bug
// https://gcc.gnu.org/bugzilla/show_bug.cgi?id=92101
//
// template<typename CharT, std::size_t N>
// struct basic_fixed_string {
// CharT data_[N+1] = {};
template<typename CharT, std::size_t N>
struct basic_fixed_string {
CharT data_[N+1] = {};
// constexpr basic_fixed_string(const CharT (&txt)[N+1]) noexcept
// {
// for(std::size_t i = 0; i <= N; ++i)
// data_[i] = txt[i];
// }
// // auto operator==(const basic_fixed_string &) = default;
// [[nodiscard]] constexpr const CharT* c_str() const noexcept { return data_; }
// };
constexpr basic_fixed_string(const CharT (&txt)[N+1]) noexcept
{
for(std::size_t i = 0; i <= N; ++i)
data_[i] = txt[i];
}
// template<typename CharT, std::size_t N>
// basic_fixed_string(const CharT (&str)[N]) -> basic_fixed_string<CharT, N-1>;
[[nodiscard]] constexpr bool size() const noexcept { return N; }
[[nodiscard]] constexpr const CharT* c_str() const noexcept { return data_; }
// template<std::size_t N>
// using fixed_string = basic_fixed_string<char, N>;
// auto operator==(const basic_fixed_string &) = default;
[[nodiscard]] constexpr friend bool operator==(const basic_fixed_string& lhs, const basic_fixed_string& rhs) noexcept
{
for(size_t i = 0; i != size(lhs.data_); ++i)
if(lhs.name_[i] != rhs.data_[i])
return false;
return true;
}
template<typename CharT2, std::size_t N2>
[[nodiscard]] constexpr friend bool operator==(const basic_fixed_string&, const basic_fixed_string<CharT2, N2>&) noexcept
{
return false;
}
template<typename CharT2, std::size_t N2>
[[nodiscard]] constexpr friend bool operator<(const basic_fixed_string& lhs, const basic_fixed_string<CharT2, N2>& rhs) noexcept
{
using std::begin, std::end;
auto first1 = begin(lhs.data_);
auto first2 = begin(rhs.data_);
const auto last1 = std::prev(end(lhs.data_)); // do not waste time for '\0'
const auto last2 = std::prev(end(rhs.data_));
for(; (first1 != last1) && (first2 != last2); ++first1, (void)++first2 ) {
if(*first1 < *first2) return true;
if(*first2 < *first1) return false;
}
return first1 == last1 && first2 != last2;
}
};
template<typename CharT, std::size_t N>
basic_fixed_string(const CharT (&str)[N]) -> basic_fixed_string<CharT, N-1>;
template<std::size_t N>
using fixed_string = basic_fixed_string<char, N>;
// TODO gcc:92101
// hacked version to work with derived_unit
// https://gcc.gnu.org/bugzilla/show_bug.cgi?id=92101
template<typename CharT, CharT... Chars>
struct basic_fixed_string {
struct basic_fixed_string_hack {
static constexpr CharT txt[] = { Chars..., '\0' };
static constexpr const CharT* c_str() noexcept
@@ -60,7 +93,7 @@ namespace units {
inline namespace hacks {
template<typename T, T... chars>
constexpr basic_fixed_string<T, chars...> operator""_fs() { return {}; }
constexpr basic_fixed_string_hack<T, chars...> operator""_fs() { return {}; }
}