Skip to content

LockedPtr::and_then introduced - #458

Open
sankurm wants to merge 3 commits into
eclipse-score:mainfrom
sankurm:locked_ptr_monadic_and_then
Open

LockedPtr::and_then introduced#458
sankurm wants to merge 3 commits into
eclipse-score:mainfrom
sankurm:locked_ptr_monadic_and_then

Conversation

@sankurm

@sankurm sankurm commented Aug 11, 2026

Copy link
Copy Markdown
Contributor

Monadic interfaces help write functional code by supporting transform, and_then and or_else. This PR is to support and_then.

This makes the following possible.

auto maybe_number = [](const LockedPtr<std::string>& lp) -> std::optional<int> {
    int result{};
    auto [ptr, ec] = std::from_chars(lp->data(), lp->data() + lp->size(), result);
    if (ec == std::errc::invalid_argument || ec == std::errc::result_out_of_range)
    {
        return std::nullopt;
    }
    else
    {
        return result;
    }
};

auto output = [](int n) {
    std::cout << n << '\n';
    return std::nullopt;
};

Synchronized<std::string> synced_num{"42"};
Synchronized<std::string> synced_non_num{"GitHub"};

auto ignore1 = synced_num.lock()
                         .and_then(maybe_number)
                         .and_then(output);         //Prints 42

auto ignore2 = synced_non_num.lock()
                             .and_then(maybe_number)
                             .and_then(output);    //No output as this is not executed as string is not a number

@github-project-automation github-project-automation Bot moved this to In Progress in BAS - Baselibs FT Aug 11, 2026
@sankurm
sankurm temporarily deployed to workflow-approval August 11, 2026 12:04 — with GitHub Actions Inactive
@sankurm
sankurm temporarily deployed to workflow-approval August 11, 2026 12:04 — with GitHub Actions Inactive
@sankurm
sankurm deployed to workflow-approval August 11, 2026 12:04 — with GitHub Actions Active
@sankurm
sankurm deployed to workflow-approval August 11, 2026 12:04 — with GitHub Actions Active
@github-actions github-actions Bot added comp-concurrency Related to score/concurrency c++ C++ code labels Aug 11, 2026
@github-actions

Copy link
Copy Markdown
Contributor

The created documentation from the pull request is available at: docu-html

@4og
4og requested a balanced review from Copilot August 11, 2026 14:35

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Pull request overview

Adds LockedPtr::and_then for optional-based monadic chaining.

Changes:

  • Adds optional detection support.
  • Implements four value-category overloads.
  • Adds tests for success, empty, and null cases.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 4 comments.

File Description
score/concurrency/type_traits.h Adds optional type detection.
score/concurrency/locked_ptr.h Implements and_then.
score/concurrency/locked_ptr_test.cpp Tests the new overloads.
Suppressed comments (6)

score/concurrency/locked_ptr.h:227

  • Ret preserves cv/reference qualifiers, so this overload rejects callables returning const optional<T> or optional<T>&, unlike score::cpp::optional::and_then. Decay the invocation result so all supported optional return forms produce an optional value.
              typename Ret = std::invoke_result_t<Func, const LockedPtr&>,

score/concurrency/locked_ptr.h:248

  • Ret preserves cv/reference qualifiers, so this overload rejects callables returning const optional<T> or optional<T>&, unlike score::cpp::optional::and_then. Decay the invocation result so all supported optional return forms produce an optional value.
              typename Ret = std::invoke_result_t<Func, LockedPtr>,

score/concurrency/locked_ptr.h:269

  • Ret preserves cv/reference qualifiers, so this overload rejects callables returning const optional<T> or optional<T>&, unlike score::cpp::optional::and_then. Decay the invocation result so all supported optional return forms produce an optional value.
              typename Ret = std::invoke_result_t<Func, const LockedPtr&>,

score/concurrency/locked_ptr_test.cpp:53

  • This namespace-scope lambda object is a mutable global. Repository style requires fixed namespace-scope values to be const/constexpr and k-prefixed (docs/cpp-style-guide.md:29); use a constant or named helper function and update its call sites.
auto value_if_positive = [](LPtr2IntW& lp) -> score::cpp::optional<int> {

score/concurrency/locked_ptr_test.cpp:61

  • This namespace-scope lambda object is a mutable global. Repository style requires fixed namespace-scope values to be const/constexpr and k-prefixed (docs/cpp-style-guide.md:29); use a constant or named helper function and update its call sites.
auto cvalue_if_positive = [](const LPtr2IntW& lp) -> score::cpp::optional<int> {

score/concurrency/locked_ptr_test.cpp:69

  • This namespace-scope lambda object is a mutable global. Repository style requires fixed namespace-scope values to be const/constexpr and k-prefixed (docs/cpp-style-guide.md:29); use a constant or named helper function and update its call sites.
auto opt_move_get = [](LPtr2IntW lp) -> score::cpp::optional<IntWrapper*> {

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment thread score/concurrency/type_traits.h Outdated
Comment thread score/concurrency/locked_ptr.h
Comment thread score/concurrency/locked_ptr_test.cpp Outdated
Comment thread score/concurrency/locked_ptr.h Outdated
@sankurm
sankurm force-pushed the locked_ptr_monadic_and_then branch from 5a7302d to 1586ba2 Compare August 13, 2026 14:23
@sankurm
sankurm deployed to workflow-approval August 13, 2026 14:23 — with GitHub Actions Active
@sankurm
sankurm deployed to workflow-approval August 13, 2026 14:23 — with GitHub Actions Active
@sankurm
sankurm deployed to workflow-approval August 13, 2026 14:23 — with GitHub Actions Active
@sankurm
sankurm deployed to workflow-approval August 13, 2026 14:23 — with GitHub Actions Active
@github-actions

Copy link
Copy Markdown
Contributor

Documentation preview for this pull request is available at:
pr-458: https://eclipse-score.github.io/baselibs/pr-458/

@sankurm
sankurm requested a deployment to workflow-approval August 17, 2026 09:45 — with GitHub Actions Waiting
@sankurm
sankurm force-pushed the locked_ptr_monadic_and_then branch from e62e468 to c615da8 Compare August 17, 2026 10:38
@sankurm
sankurm deployed to workflow-approval August 17, 2026 10:38 — with GitHub Actions Active
@sankurm
sankurm deployed to workflow-approval August 17, 2026 10:38 — with GitHub Actions Active
@sankurm
sankurm deployed to workflow-approval August 17, 2026 10:38 — with GitHub Actions Active
@sankurm
sankurm deployed to workflow-approval August 17, 2026 10:38 — with GitHub Actions Active
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

c++ C++ code comp-concurrency Related to score/concurrency

Projects

Status: In Progress

Development

Successfully merging this pull request may close these issues.

2 participants