Skip to content

Commit d1fbb48

Browse files
committed
Make Storable::cloneStorable return shared_ptr.
While unique_ptr is a better return type for a factory method, a unique_ptr return cannot be implemented in terms of a shared_ptr return (though the reverse is true), and our existing classes cannot be modified to clone by unique_ptr without running into a pybind11 bug (pybind/pybind11#1138). At present it looks like the pybind11 team is leaning towards forbidding such code rather than supporting unique_ptr to shared_ptr conversion.
1 parent 2b30436 commit d1fbb48

File tree

5 files changed

+9
-7
lines changed

5 files changed

+9
-7
lines changed

include/lsst/afw/typehandling/PolymorphicValue.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,8 @@ class PolymorphicValue final {
137137
std::size_t hash_value() const;
138138

139139
private:
140-
std::unique_ptr<Storable> _value;
140+
// unique_ptr would be more appropriate, but Storable::cloneStorable must return shared_ptr
141+
std::shared_ptr<Storable> _value;
141142
};
142143

143144
} // namespace typehandling

include/lsst/afw/typehandling/Storable.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,8 @@ class Storable : public table::io::Persistable {
7373
* @note If this class supports a `clone` operation, the two should behave
7474
* identically except for the formal return type.
7575
*/
76-
virtual std::unique_ptr<Storable> cloneStorable() const;
76+
// Return shared_ptr to work around https://github.com/pybind/pybind11/issues/1138
77+
virtual std::shared_ptr<Storable> cloneStorable() const;
7778

7879
/**
7980
* Create a string representation of this object (optional operation).

include/lsst/afw/typehandling/test.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ class SimpleStorable : public Storable {
5959
public:
6060
virtual ~SimpleStorable() = default;
6161

62-
std::unique_ptr<Storable> cloneStorable() const override { return std::make_unique<SimpleStorable>(); }
62+
std::shared_ptr<Storable> cloneStorable() const override { return std::make_unique<SimpleStorable>(); }
6363

6464
std::string toString() const override { return "Simplest possible representation"; }
6565

@@ -80,7 +80,7 @@ class ComplexStorable final : public SimpleStorable {
8080
return *this;
8181
}
8282

83-
std::unique_ptr<Storable> cloneStorable() const override {
83+
std::shared_ptr<Storable> cloneStorable() const override {
8484
return std::make_unique<ComplexStorable>(storage);
8585
}
8686

src/typehandling/PolymorphicValue.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,12 @@ namespace typehandling {
3232

3333
PolymorphicValue::PolymorphicValue(Storable const& value) : _value(value.cloneStorable()) {}
3434
// No move-constructor, because putting a pointer to Storable&& into a
35-
// unique_ptr is safe only if the object was dynamically allocated
35+
// shared_ptr is safe only if the object was dynamically allocated
3636

3737
PolymorphicValue::~PolymorphicValue() noexcept = default;
3838

3939
PolymorphicValue::PolymorphicValue(PolymorphicValue const& other)
40-
: _value(other._value ? other._value->cloneStorable() : std::unique_ptr<Storable>()) {}
40+
: _value(other._value ? other._value->cloneStorable() : nullptr) {}
4141
PolymorphicValue::PolymorphicValue(PolymorphicValue&&) = default; // other._value emptied
4242

4343
PolymorphicValue& PolymorphicValue::operator=(PolymorphicValue const& other) {

src/typehandling/Storable.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ namespace typehandling {
3333

3434
Storable::~Storable() noexcept {}
3535

36-
std::unique_ptr<Storable> Storable::cloneStorable() const {
36+
std::shared_ptr<Storable> Storable::cloneStorable() const {
3737
throw LSST_EXCEPT(UnsupportedOperationException, "Cloning is not supported.");
3838
}
3939

0 commit comments

Comments
 (0)