Add new int_conv.hpp that provides a semantic_cast-like int_conv.

This commit is contained in:
King_DuckZ 2018-10-12 20:55:14 +01:00
parent f06c0a60dd
commit 7ceaf01a37

View file

@ -0,0 +1,58 @@
/* Copyright 2016-2018 Michele Santullo
* This file is part of "duckhandy".
*
* "duckhandy" is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* "duckhandy" is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with "duckhandy". If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef id890F1A22A90047BE9A9DCC8F7F057F91
#define id890F1A22A90047BE9A9DCC8F7F057F91
#include "implem/int_conv.hpp"
#include <type_traits>
#include <string>
#include <string_view>
namespace dhandy {
namespace implem {
template <typename T, typename F>
struct IntConv;
template <typename F>
struct IntConv<std::enable_if_t<std::is_integral_v<F>, std::string>, F> {
static std::string conv (const F& in) {
auto retval = dhandy::int_to_ary(in);
return std::string(retval.begin(), retval.end() - 1);
}
};
template <typename T>
struct IntConv<T, std::enable_if_t<std::is_integral_v<T>, std::string>> {
static T conv (const std::string& in) {
return dhandy::ary_to_int<T>(in.data(), in.data() + in.size());
}
};
template <typename T>
struct IntConv<T, std::enable_if_t<std::is_integral_v<T>, std::string_view>> {
static T conv (const std::string_view& in) {
return dhandy::ary_to_int<T>(in.data(), in.data() + in.size());
}
};
} //namespace implem
template <typename To, typename From>
inline To int_conv (const From& from) {
return implem::IntConv<To, From>::conv(from);
}
} //namespace dhandy
#endif