Skip to content
Snippets Groups Projects
Commit 4a63374d authored by akuesters's avatar akuesters Committed by Sam Yates
Browse files

Prepare arb::util::optional to be used for automatic conversion from/to pybind11::object (#738)

* Add value_type and emplace method to the optional class to support automatic conversion of arb::util::optional in python.
* Add unit test for arb::util::optional::emplace.
parent 2d0816a9
No related branches found
No related tags found
No related merge requests found
...@@ -107,6 +107,7 @@ namespace detail { ...@@ -107,6 +107,7 @@ namespace detail {
using const_rvalue_reference = typename data_type::const_rvalue_reference; using const_rvalue_reference = typename data_type::const_rvalue_reference;
public: public:
using value_type = X;
using reference = typename data_type::reference; using reference = typename data_type::reference;
using const_reference = typename data_type::const_reference; using const_reference = typename data_type::const_reference;
using pointer = typename data_type::pointer; using pointer = typename data_type::pointer;
...@@ -165,6 +166,15 @@ namespace detail { ...@@ -165,6 +166,15 @@ namespace detail {
} }
set = false; set = false;
} }
template <typename Y>
void emplace(Y&& y) {
if (set) {
data.destruct();
}
data.construct(std::forward<Y>(y));
set = true;
}
}; };
// type utilities // type utilities
......
...@@ -287,3 +287,18 @@ TEST(optional, just) { ...@@ -287,3 +287,18 @@ TEST(optional, just) {
EXPECT_EQ(3, o2.value()); EXPECT_EQ(3, o2.value());
EXPECT_EQ(4, o3.value()); EXPECT_EQ(4, o3.value());
} }
TEST(optional, emplace) {
optional<int> o1(7);
optional<std::array<double, 3>> o2{{22., 22., 22.}};
int x = 42;
std::array<double, 3> arr{{4.5, 7.1, 1.2}};
o1.emplace(x);
o2.emplace(arr);
EXPECT_EQ(42, o1.value());
EXPECT_EQ(4.5, o2.value()[0]);
EXPECT_EQ(7.1, o2.value()[1]);
EXPECT_EQ(1.2, o2.value()[2]);
}
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