Skip to content

LockedPtr::transform implemented - #456

Open
sankurm wants to merge 1 commit into
eclipse-score:mainfrom
sankurm:locked_ptr_monadic_transform
Open

LockedPtr::transform implemented#456
sankurm wants to merge 1 commit into
eclipse-score:mainfrom
sankurm:locked_ptr_monadic_transform

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 transform.

This makes the following possible.

Synchronized<int> synced_int{42};
auto to_str = [](const LockedPtr<int>& lp) { return std::to_string(*lp); };

auto str = synced_int.lock()
                     .transform(to_str);
EXPECT_EQ(str, "42");

@github-actions

Copy link
Copy Markdown
Contributor

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

@sankurm
sankurm force-pushed the locked_ptr_monadic_transform branch from 612eded to 6c94c7c Compare August 11, 2026 11:00
@sankurm
sankurm temporarily deployed to workflow-approval August 11, 2026 11:00 — with GitHub Actions Inactive
@sankurm
sankurm temporarily deployed to workflow-approval August 11, 2026 11:00 — with GitHub Actions Inactive
@sankurm
sankurm temporarily deployed to workflow-approval August 11, 2026 11:00 — with GitHub Actions Inactive
@sankurm
sankurm temporarily deployed to workflow-approval August 11, 2026 11:00 — with GitHub Actions Inactive
@4og
4og requested a balanced review from Copilot August 13, 2026 12:38

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 monadic transformation support to LockedPtr.

Changes:

  • Adds lvalue, const-lvalue, and rvalue transform overloads.
  • Adds null and non-null transformation tests.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 5 comments.

File Description
score/concurrency/locked_ptr.h Implements transformation APIs.
score/concurrency/locked_ptr_test.cpp Tests transformation behavior.
Suppressed comments (3)

score/concurrency/locked_ptr.h:239

  • This overload also preserves top-level const, so its result type diverges from the linked transform contract and can reject const move-only return values. Strip top-level cv-qualification from the invocation result.
              typename FuncResult = std::invoke_result_t<Func, const LockedPtr&>,

score/concurrency/locked_ptr.h:260

  • The rvalue overload must also remove top-level cv-qualification; otherwise a callable returning a const move-only value cannot be wrapped even though transform should store the unqualified value type.
              typename FuncResult = std::invoke_result_t<Func, LockedPtr>,

score/concurrency/locked_ptr_test.cpp:58

  • These namespace-scope callable objects are also mutable globals, contrary to docs/cpp-style-guide.md:29 and MISRA 6.7.2. Use free functions or constexpr, k-prefixed lambda constants instead.
auto move_ptr = [](LPtr2IntW&& ptr) {
    return std::move(ptr);
};

auto get_obj = [](const LPtr2IntW& ptr) {

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

*/
template <typename Func,
typename = std::enable_if_t<std::is_invocable_v<Func, LockedPtr&>>,
typename FuncResult = std::invoke_result_t<Func, LockedPtr&>,
#include "score/concurrency/type_traits.h"
#include "score/concurrency/unlock_guard.h"

#include <score/optional.hpp>
Comment on lines +252 to +254
* The LockedPtr is moved into the callable by value, transferring lock ownership.
* Returns score::cpp::optional containing the result.
* @tparam Func Callable type that accepts LockedPtr by value and returns a non-void type.
typename = std::enable_if_t<std::is_invocable_v<Func, LockedPtr&>>,
typename FuncResult = std::invoke_result_t<Func, LockedPtr&>,
typename = std::enable_if_t<!std::is_void_v<FuncResult>>>
[[nodiscard]] auto transform(Func&& f) & -> score::cpp::optional<FuncResult>
Comment on lines +47 to +50
auto value_by_10 = [](LPtr2IntW& lp) {
return lp->value / 10.0;
};
auto cvalue_by_10 = [](const LPtr2IntW& lp) {
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