rjp/rjp++/include/rjp_util.hpp

95 lines
2.6 KiB
C++

/**
rjp++
Copyright (C) 2020 rexy712
This program 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.
This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef RJP_UTIL_HPP
#define RJP_UTIL_HPP
#include <rjp.h>
#include "integral.hpp"
namespace rjp{
namespace detail{
template<class To, class From, bool = std::is_same<std::remove_reference_t<To>,std::remove_reference_t<From>>::value>
struct convert_helper;
template<class To, class From>
struct convert_helper<To,From,false>{
static To perform(From&& t){
return To(std::move(t));
}
};
template<class To, class From>
struct convert_helper<To,From&,false>{
static To perform(const From& t){
return To(const_cast<RJP_value*>(t.raw()), false);
}
};
template<class To, class From>
struct convert_helper<To,From,true>{
static decltype(auto) perform(From&& t){
return std::forward<From>(t);
}
};
}
template<class To, class From>
To convert(From&& from){
return detail::convert_helper<To,From>::perform(std::forward<From>(from));
}
namespace detail{
template<class To, class From>
struct get_ref{
using type = std::remove_reference_t<To>;
};
template<class To, class From>
struct get_ref<To, From&>{
using type = std::remove_reference_t<To>&;
};
template<class To, class From>
struct get_ref<To, From&&>{
using type = std::remove_reference_t<To>&&;
};
template<class To, class From>
struct get_ref<To&&, From&>{
using type = To&&;
};
template<class To, class From>
struct get_ref<To&&, From&&>{
using type = To&&;
};
}
template<class To, class From>
decltype(auto) cast(From&& from){
return static_cast<typename detail::get_ref<To,From&&>::type>(std::forward<From>(from));
}
namespace detail{
template<class Val>
void set_to_underlying(RJP_value* val, typename Val::underlying_type);
template<>
void set_to_underlying<rjp::integer>(RJP_value* val, RJP_int i);
template<>
void set_to_underlying<rjp::dfloat>(RJP_value* val, RJP_float f);
template<>
void set_to_underlying<rjp::boolean>(RJP_value* val, RJP_bool b);
}
}
#endif