Skip to content

Commit

Permalink
[Params] support new Itanium ABI for concepts https://reviews.llvm.or…
Browse files Browse the repository at this point in the history
  • Loading branch information
tttapa committed Feb 21, 2024
1 parent 69ad271 commit 12fec07
Show file tree
Hide file tree
Showing 8 changed files with 63 additions and 23 deletions.
8 changes: 4 additions & 4 deletions src/alpaqa/include/alpaqa/implementation/params/json.tpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ std::string join_sorted_keys(const auto &members) {

template <class T>
requires requires { attribute_table<T, json>::table; }
void set_param(T &t, const json &j) {
void set_param_default(T &t, const json &j) {
if (!j.is_object())
throw invalid_json_param("Invalid value " + to_string(j) +
" for type '" + demangled_typename(typeid(T)) +
Expand Down Expand Up @@ -79,7 +79,7 @@ void set_param(T &t, const json &j) {

template <class T>
requires requires { enum_table<T, json>::table; }
void set_param(T &t, const json &j) {
void set_param_default(T &t, const json &j) {
if (!j.is_string())
throw invalid_json_param("Invalid value " + to_string(j) +
" for enum '" + demangled_typename(typeid(T)) +
Expand All @@ -100,13 +100,13 @@ void set_param(T &t, const json &j) {

template <class T>
requires requires { enum_table<T, json>::table; }
void get_param(const T &t, json &j) {
void get_param_default(const T &t, json &j) {
j = enum_name(t);
}

template <class T>
requires requires { attribute_table<T, json>::table; }
void get_param(const T &t, json &s) {
void get_param_default(const T &t, json &s) {
s = json::object();
const auto &m = attribute_table<T, json>::table;
for (auto &&[k, v] : m)
Expand Down
4 changes: 2 additions & 2 deletions src/alpaqa/include/alpaqa/implementation/params/params.tpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ struct enum_accessor<T, ParamString> {
/// given by @p s.key.
template <class T>
requires requires { attribute_table<T, ParamString>::table; }
void set_param(T &t, ParamString s) {
void set_param_default(T &t, ParamString s) {
const auto &m = attribute_table<T, ParamString>::table;
auto [key, remainder] = split_key(s.key);
auto it = m.find(key);
Expand All @@ -71,7 +71,7 @@ void set_param(T &t, ParamString s) {
/// Set @p t to the value of @p s.value.
template <class T>
requires requires { enum_table<T, ParamString>::table; }
void set_param(T &t, ParamString s) {
void set_param_default(T &t, ParamString s) {
assert_key_empty<T>(s);
const auto &m = enum_table<T, ParamString>::table;
auto it = m.find(s.value);
Expand Down
25 changes: 19 additions & 6 deletions src/alpaqa/src/params/json.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ inline constexpr bool is_duration<std::chrono::duration<Rep, Period>> = true;

template <class Duration>
requires is_duration<Duration>
void set_param(Duration &t, const json &j) {
void set_param_default(Duration &t, const json &j) {
if (!j.is_string())
throw invalid_json_param(
"Invalid value " + to_string(j) + " for type '" +
Expand All @@ -60,7 +60,7 @@ void set_param(Duration &t, const json &j) {

template <class Duration>
requires is_duration<Duration>
void get_param(const Duration &t, json &s) {
void get_param_default(const Duration &t, json &s) {
namespace chr = std::chrono;
auto dur = t;
std::string result;
Expand Down Expand Up @@ -179,7 +179,7 @@ void ALPAQA_EXPORT set_param(std::string &t, const nlohmann::json &j) {

template <std::integral T>
requires(!std::same_as<T, bool>)
void set_param(T &t, const nlohmann::json &j) {
void set_param_default(T &t, const nlohmann::json &j) {
if (std::unsigned_integral<T> && !j.is_number_unsigned())
throw invalid_json_param("Invalid value " + to_string(j) +
" for type '" + demangled_typename(typeid(T)) +
Expand All @@ -194,7 +194,7 @@ void set_param(T &t, const nlohmann::json &j) {
}

template <std::floating_point T>
void set_param(T &t, const nlohmann::json &j) {
void set_param_default(T &t, const nlohmann::json &j) {
if (j.is_string()) {
if (j == "nan") {
t = std::numeric_limits<T>::quiet_NaN();
Expand Down Expand Up @@ -222,12 +222,12 @@ void set_param(T &t, const nlohmann::json &j) {
template <class T>
requires(std::integral<T> || std::same_as<T, bool> ||
std::same_as<T, std::string>)
void get_param(const T &t, nlohmann::json &j) {
void get_param_default(const T &t, nlohmann::json &j) {
j = t;
}

template <std::floating_point T>
void get_param(const T &t, nlohmann::json &j) {
void get_param_default(const T &t, nlohmann::json &j) {
if (std::isnan(t))
j = "nan";
else if (t == +std::numeric_limits<T>::infinity())
Expand All @@ -240,6 +240,19 @@ void get_param(const T &t, nlohmann::json &j) {

#include <alpaqa/params/structs.ipp>

// Because of the new name mangling for concepts (https://reviews.llvm.org/D147655)
// we need this to be an unconstrained function template. The actual constraints
// are in set_param_default, which is not exported. Fully specialized
// instantiations of set_param are still allowed.
template <class T>
void set_param(T &t, const json &j) {
set_param_default(t, j);
}
template <class T>
void get_param(const T &t, json &j) {
get_param_default(t, j);
}

template <class... Ts>
void set_param(util::detail::dummy<Ts...> &, const json &) {}
template <class... Ts>
Expand Down
13 changes: 11 additions & 2 deletions src/alpaqa/src/params/params.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ void ALPAQA_EXPORT set_param(std::string &v, ParamString s) {
template <class T>
requires((std::floating_point<T> || std::integral<T>) &&
!std::is_enum_v<T> && !std::is_same_v<T, bool>)
void set_param(T &f, ParamString s) {
void set_param_default(T &f, ParamString s) {
assert_key_empty<T>(s);
const auto *val_end = s.value.data() + s.value.size();
auto res = util::from_chars(s.value.data(), val_end, f);
Expand Down Expand Up @@ -141,7 +141,7 @@ inline constexpr bool is_duration<std::chrono::duration<Rep, Period>> = true;

template <class Duration>
requires is_duration<Duration>
void set_param(Duration &t, ParamString s) {
void set_param_default(Duration &t, ParamString s) {
assert_key_empty<Duration>(s);
try {
util::parse_duration(t = {}, s.value);
Expand All @@ -160,6 +160,15 @@ void set_param(Duration &t, ParamString s) {

#include <alpaqa/params/structs.ipp>

// Because of the new name mangling for concepts (https://reviews.llvm.org/D147655)
// we need this to be an unconstrained function template. The actual constraints
// are in set_param_default, which is not exported. Fully specialized
// instantiations of set_param are still allowed.
template <class T>
void set_param(T &t, ParamString s) {
set_param_default(t, s);
}

template <class... Ts>
void set_param(util::detail::dummy<Ts...> &, ParamString) {}

Expand Down
14 changes: 10 additions & 4 deletions src/interop/lbfgsb/src/lbfgsb-json.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,15 @@ namespace alpaqa::params {

#include <alpaqa/lbfgsb/lbfgsb-structs.ipp>

template void LBFGSB_ADAPTER_EXPORT set_param(lbfgsb::LBFGSBSolver::Params &,
const json &);
template void LBFGSB_ADAPTER_EXPORT
get_param(const lbfgsb::LBFGSBSolver::Params &, json &);
template <>
void LBFGSB_ADAPTER_EXPORT set_param(lbfgsb::LBFGSBSolver::Params &t,
const json &j) {
set_param_default(t, j);
}
template <>
void LBFGSB_ADAPTER_EXPORT get_param(const lbfgsb::LBFGSBSolver::Params &t,
json &j) {
get_param_default(t, j);
}

} // namespace alpaqa::params
7 changes: 5 additions & 2 deletions src/interop/lbfgsb/src/lbfgsb-params.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@ namespace alpaqa::params {

#include <alpaqa/lbfgsb/lbfgsb-structs.ipp>

template void LBFGSB_ADAPTER_EXPORT set_param(lbfgsb::LBFGSBSolver::Params &,
ParamString);
template <>
void LBFGSB_ADAPTER_EXPORT set_param(lbfgsb::LBFGSBSolver::Params &t,
ParamString s) {
set_param_default(t, s);
}

} // namespace alpaqa::params
10 changes: 8 additions & 2 deletions src/interop/qpalm/src/qpalm-json.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,13 @@ namespace alpaqa::params {

#include <alpaqa/qpalm/qpalm-structs.ipp>

template void QPALM_ADAPTER_EXPORT set_param(qpalm::Settings &, const json &);
template void QPALM_ADAPTER_EXPORT get_param(const qpalm::Settings &, json &);
template <>
void QPALM_ADAPTER_EXPORT set_param(qpalm::Settings &t, const json &j) {
set_param_default(t, j);
}
template <>
void QPALM_ADAPTER_EXPORT get_param(const qpalm::Settings &t, json &j) {
get_param_default(t, j);
}

} // namespace alpaqa::params
5 changes: 4 additions & 1 deletion src/interop/qpalm/src/qpalm-params.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ namespace alpaqa::params {

#include <alpaqa/qpalm/qpalm-structs.ipp>

template void QPALM_ADAPTER_EXPORT set_param(qpalm::Settings &, ParamString);
template <>
void QPALM_ADAPTER_EXPORT set_param(qpalm::Settings &t, ParamString s) {
set_param_default(t, s);
}

} // namespace alpaqa::params

0 comments on commit 12fec07

Please sign in to comment.