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
@sankurm

sankurm commented Aug 18, 2026

Copy link
Copy Markdown
Contributor Author

@4og All comments resolved. Trigger Copilot recheck? Thanks!

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

Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.

Suppressed comments (2)

score/concurrency/locked_ptr.h:278

  • The const&& overload discards the receiver's rvalue category by constraining and invoking the callable with const LockedPtr&. A callable that specifically accepts const LockedPtr&& is therefore rejected, and an overloaded callable unexpectedly takes its lvalue branch; forward std::move(*this) as the other ref-qualified monadic overloads do.
    template <typename Func,
              typename = std::enable_if_t<std::is_invocable_v<Func, const LockedPtr&>>,
              typename Ret = std::invoke_result_t<Func, const LockedPtr&>,
              typename = std::enable_if_t<score::cpp::is_optional_v<Ret>>>
    [[nodiscard]] auto and_then(Func&& f) const&& -> Ret

score/concurrency/locked_ptr.h:208

  • The new method name violates the repository rule that functions and methods use PascalCase (docs/cpp-style-guide.md:27). Rename the overload set to AndThen and update its tests and example call sites consistently.
    [[nodiscard]] auto and_then(Func&& f) & -> Ret

#ifndef SCORE_LIB_CONCURRENCY_TYPE_TRAITS_H
#define SCORE_LIB_CONCURRENCY_TYPE_TRAITS_H

#include <score/optional.hpp>
@Nikhil2206
Nikhil2206 self-requested a review August 21, 2026 06:56
@Nikhil2206

Copy link
Copy Markdown
Contributor

besides the copilot comments rest looks good me,
It would be helpful to detect future regression if we add "callback skipped test case"

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.

3 participants