57 #include "include_base_utils.h" 58 #include "common/util.h" 70 template <class T, typename std::enable_if<std::is_same<T, string>::value, T>::type* =
nullptr>
71 boost::optional<T> reconcile(
const boost::optional<T>& val1,
const boost::optional<T>& val2, boost::optional<bool> resolve_defined, boost::optional<bool> resolve_true, boost::optional<bool> resolve_max,
const string& err_msg =
"") {
74 if (val1 == val2)
return val1;
77 if (val1 == boost::none || val2 == boost::none) {
78 if (resolve_defined != boost::none && *resolve_defined ==
false)
return boost::none;
79 else return val1 == boost::none ? val2 : val1;
82 throw runtime_error(
string(
"Cannot reconcile strings: ") + to_string(val1) +
string(
" vs ") + to_string(val2) + (!err_msg.empty() ? string(
". ") + err_msg : string(
"")));
84 template <class T, typename std::enable_if<std::is_same<T, string>::value, T>::type* =
nullptr>
85 boost::optional<T> reconcile(
const boost::optional<T>& val1,
const boost::optional<T>& val2,
const string& err_msg =
"") {
86 return reconcile(val1, val2, boost::none, boost::none, boost::none, err_msg);
89 template <class T, typename std::enable_if<std::is_integral<T>::value, T>::type* =
nullptr>
90 boost::optional<T> reconcile(
const boost::optional<T>& val1,
const boost::optional<T>& val2, boost::optional<bool> resolve_defined, boost::optional<bool> resolve_true, boost::optional<bool> resolve_max,
const string& err_msg =
"") {
93 if (val1 == val2)
return val1;
96 if (val1 == boost::none || val2 == boost::none) {
97 if (resolve_defined != boost::none && *resolve_defined ==
false)
return boost::none;
98 else return val1 == boost::none ? val2 : val1;
102 if (resolve_max != boost::none) {
103 return *resolve_max ? max(*val1, *val2) : min(*val1, *val2);
107 throw runtime_error(
string(
"Cannot reconcile integrals: ") + to_string(val1) +
string(
" vs ") + to_string(val2) + (!err_msg.empty() ? string(
". ") + err_msg : string(
"")));
109 template <class T, typename std::enable_if<std::is_integral<T>::value, T>::type* =
nullptr>
110 boost::optional<T> reconcile(
const boost::optional<T>& val1,
const boost::optional<T>& val2,
const string& err_msg =
"") {
111 return reconcile(val1, val2, boost::none, boost::none, boost::none, err_msg);
114 template <class T, typename std::enable_if<std::is_same<T, bool>::value, T>::type>
115 boost::optional<T> reconcile(
const boost::optional<T>& val1,
const boost::optional<T>& val2, boost::optional<bool> resolve_defined, boost::optional<bool> resolve_true, boost::optional<bool> resolve_max,
const string& err_msg =
"") {
118 if (val1 == val2)
return val1;
121 if (val1 == boost::none || val2 == boost::none) {
122 if (resolve_defined != boost::none && *resolve_defined ==
false)
return boost::none;
123 else return val1 == boost::none ? val2 : val1;
127 if (resolve_true != boost::none) {
128 return val1 == resolve_true ? val1 : val2;
130 throw runtime_error(
string(
"Cannot reconcile booleans: ") + to_string(val1) +
string(
" vs ") + to_string(val2) + (!err_msg.empty() ? string(
". ") + err_msg : string(
"")));
133 template <class T, typename std::enable_if<std::is_same<T, bool>::value, T>::type>
134 boost::optional<T> reconcile(
const boost::optional<T>& val1,
const boost::optional<T>& val2,
const string& err_msg =
"") {
135 return reconcile(val1, val2, boost::none, boost::none, boost::none, err_msg);
139 vector<T> reconcile(
const vector<T>& v1,
const vector<T>& v2,
const string& err_msg =
"") {
142 if (v1 == v2)
return v1;
145 if (v1.empty())
return v2;
146 if (v2.empty())
return v1;
149 throw runtime_error(
"Cannot reconcile vectors" + (!err_msg.empty() ? string(
". ") + err_msg : string(
"")));
Definition: gen_utils.h:63