Skip to content

Commit

Permalink
feat: Add polymorphic value like container (#855)
Browse files Browse the repository at this point in the history
This PR introduces a type `PolymorphicValue<T>` that behaves very similar to `std::unique_ptr<T>`, but preserves knowlegde of how to copy the value. In fact, `PolymorphicValue` only works with copyable types. 

This allows more convenient handling of interfaces, for types that are copyable and where copying does not have to be avoided. 
It looks like this:
```cpp
struct Base {};
struct Derived {};

std::unique_ptr<Base> up1 = std::make_unique<Derived>();
std::unique_ptr<Base> up2 = up1; // this does not work

Acts::PolymorphicValue<Base> pv1 = Acts::makePolymorphicValue<Derived>();
Acts::PolymorphicValue<Base> pv2 = pv1; // this makes a copy of the stored object
```

The reason why I'm adding this is that `pybind11` does not support `std::unique_ptr` as arguments/values on the C++ side, since python lacks a mechanism to release ownership, which would be required in this case. There's a limited number of cases where this is a problem, and where we don't actually care if we copy the stored value.
  • Loading branch information
paulgessinger committed Jun 25, 2021
1 parent c7ad7e8 commit a837633
Show file tree
Hide file tree
Showing 7 changed files with 980 additions and 5 deletions.

0 comments on commit a837633

Please sign in to comment.