Skip to content

Migrate unique_ptr and shared_ptr uses to py::smart_holder - #442

Merged
linusheck merged 1 commit into
stormchecker:masterfrom
linusheck:smart-holder
Aug 21, 2026
Merged

Migrate unique_ptr and shared_ptr uses to py::smart_holder#442
linusheck merged 1 commit into
stormchecker:masterfrom
linusheck:smart-holder

Conversation

@linusheck

Copy link
Copy Markdown
Contributor

Fixes #303. I ran this script on the source code:

#!/usr/bin/env python3
import re
from pathlib import Path


def split_args(s):
    parts, start, depth = [], 0, 0
    for i, c in enumerate(s):
        depth += c in "<([{"
        depth -= c in ">)]}"
        if c == "," and depth == 0:
            parts.append(s[start:i])
            start = i + 1
    return parts + [s[start:]]


for path in Path("src").rglob("*"):
    if path.suffix not in {".cpp", ".h"}:
        continue
    text, pos = path.read_text(), 0
    while (start := text.find("py::class_<", pos)) >= 0:
        if text[text.rfind("\n", 0, start) + 1 : start].lstrip().startswith("//"):
            pos = start + 1
            continue
        end, depth = start + len("py::class_"), 0
        while end < len(text):
            depth += text[end] == "<"
            depth -= text[end] == ">"
            if depth == 0:
                break
            end += 1
        args = split_args(text[start + len("py::class_<") : end])
        args = [a for a in args if not re.fullmatch(r"\s*std::(?:shared|unique)_ptr<.*>\s*", a, re.S)]
        replacement = "py::classh<" + ",".join(args) + ">"
        text = text[:start] + replacement + text[end + 1 :]
        pos = start + len(replacement)
    text = re.sub(r"(?m)^PYBIND11_DECLARE_HOLDER_TYPE\(T, std::shared_ptr<T(?: const)?>\)\n\n?", "", text)
    path.write_text(text)

@volkm

volkm commented Aug 20, 2026

Copy link
Copy Markdown
Contributor

Documentation

@volkm volkm left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM.

@volkm

volkm commented Aug 20, 2026

Copy link
Copy Markdown
Contributor

@sjunges Any comments or things to consider before merging?

py::arg("model"), py::arg("formula"), py::arg("compute_scheduler") = false);

using PcaaWeightVectorChecker = storm::modelchecker::multiobjective::PcaaWeightVectorChecker<storm::models::sparse::Mdp<ValueType>>;
py::class_<PcaaWeightVectorChecker, std::unique_ptr<PcaaWeightVectorChecker>> weightedObjectiveMdpModelChecker(

@volkm volkm Aug 20, 2026

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems to be the only case where we used a unique_ptrinstead of a shared_ptr. I would argue that using the smart pointer holder here is still a good idea.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With the smart pointer, you mean the unique_ptr or the smart_holder? :)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Smart holder :)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not use the smart holder everywhere? I think the main problem could be on places where we have problems with the ownership?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We are now actually using the smart holder everywhere. I just wanted to point out that we originally used a unique_ptr here, in case this was relevant. But I believe it was not important.

@sjunges

sjunges commented Aug 21, 2026

Copy link
Copy Markdown
Contributor

LGTM

@linusheck linusheck changed the title Migrate to py::smart_holder Migrate unique_ptr and shared_ptr uses to py::smart_holder Aug 21, 2026
@linusheck
linusheck merged commit 4237e63 into stormchecker:master Aug 21, 2026
22 checks passed
@linusheck
linusheck deleted the smart-holder branch August 21, 2026 11:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Use py::smart_holder

3 participants