Commit 5eb12825 by Maarten L. Hekkelman

Added category::find1<std::optional>

parent cfa46ec9
...@@ -3,6 +3,7 @@ Version 5.0.9 ...@@ -3,6 +3,7 @@ Version 5.0.9
- Added create_water to model - Added create_water to model
- Writing twin domain info in PDB files and more PDB fixes - Writing twin domain info in PDB files and more PDB fixes
- remove_atom improved (remove struct_conn records) - remove_atom improved (remove struct_conn records)
- Added a specialisation for category::find1<std::optional>
Version 5.0.8 Version 5.0.8
- implemented find_first, find_min, find_max and count in category - implemented find_first, find_min, find_max and count in category
......
...@@ -64,6 +64,12 @@ class multiple_results_error : public std::runtime_error ...@@ -64,6 +64,12 @@ class multiple_results_error : public std::runtime_error
}; };
// -------------------------------------------------------------------- // --------------------------------------------------------------------
// These should be moved elsewhere, one day.
template<typename _Tp> inline constexpr bool is_optional_v = false;
template<typename _Tp> inline constexpr bool is_optional_v<std::optional<_Tp>> = true;
// --------------------------------------------------------------------
class category class category
{ {
...@@ -299,7 +305,7 @@ class category ...@@ -299,7 +305,7 @@ class category
return find1<T>(cbegin(), std::move(cond), column); return find1<T>(cbegin(), std::move(cond), column);
} }
template <typename T> template <typename T, std::enable_if_t<not is_optional_v<T>, int> = 0>
T find1(const_iterator pos, condition &&cond, const char *column) const T find1(const_iterator pos, condition &&cond, const char *column) const
{ {
auto h = find<T>(pos, std::move(cond), column); auto h = find<T>(pos, std::move(cond), column);
...@@ -310,6 +316,20 @@ class category ...@@ -310,6 +316,20 @@ class category
return *h.begin(); return *h.begin();
} }
template <typename T, std::enable_if_t<is_optional_v<T>, int> = 0>
T find1(const_iterator pos, condition &&cond, const char *column) const
{
auto h = find<typename T::value_type>(pos, std::move(cond), column);
if (h.size() > 1)
throw multiple_results_error();
if (h.empty())
return {};
return *h.begin();
}
template <typename... Ts, typename... Cs, typename U = std::enable_if_t<sizeof...(Ts) != 1>> template <typename... Ts, typename... Cs, typename U = std::enable_if_t<sizeof...(Ts) != 1>>
std::tuple<Ts...> find1(condition &&cond, Cs... columns) const std::tuple<Ts...> find1(condition &&cond, Cs... columns) const
{ {
......
...@@ -299,17 +299,6 @@ quaternion_type<T> normalize(quaternion_type<T> q) ...@@ -299,17 +299,6 @@ quaternion_type<T> normalize(quaternion_type<T> q)
quaternion construct_from_angle_axis(float angle, point axis) quaternion construct_from_angle_axis(float angle, point axis)
{ {
// auto q = std::cos((angle * kPI / 180) / 2);
// auto s = std::sqrt(1 - q * q);
// axis.normalize();
// return normalize(quaternion{
// static_cast<float>(q),
// static_cast<float>(s * axis.m_x),
// static_cast<float>(s * axis.m_y),
// static_cast<float>(s * axis.m_z) });
angle = (angle * kPI / 180) / 2; angle = (angle * kPI / 180) / 2;
auto s = std::sin(angle); auto s = std::sin(angle);
auto c = std::cos(angle); auto c = std::cos(angle);
......
...@@ -3104,3 +3104,30 @@ _date today ...@@ -3104,3 +3104,30 @@ _date today
BOOST_TEST(db == db2); BOOST_TEST(db == db2);
} }
BOOST_AUTO_TEST_CASE(find1_opt_1)
{
using namespace cif::literals;
using namespace std::literals;
auto f = R"(data_TEST
#
loop_
_test.id
_test.name
_test.value
1 aap 1.0
2 noot 1.1
3 mies 1.2
)"_cf;
auto &db = f.front();
auto &test = db["test"];
auto v = test.find1<std::optional<float>>("id"_key == 1, "value");
BOOST_CHECK(v.has_value());
BOOST_TEST(*v == 1.0f);
v = test.find1<std::optional<float>>("id"_key == 4, "value");
BOOST_CHECK(v.has_value() == false);
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment