Skip to content

Conversation

@konard
Copy link
Member

@konard konard commented Sep 13, 2025

🚀 Implementation Complete

This PR implements NonMutableDelegate, a new delegate specialization that addresses issue #19 by avoiding std::shared_ptr overhead for objects with guaranteed lifetime.

📋 Issue Reference

Fixes #19

✨ Key Features

  • Performance Optimized: Uses raw pointers internally instead of std::shared_ptr to eliminate memory overhead
  • Multiple Construction Patterns: Supports all 4 requested use cases from the issue:
    1. Create from standard Delegate
    2. Create from std::shared_ptr<T> + method pointer
    3. Create from object reference + method pointer
    4. Create from std::unique_ptr<T> + method pointer
  • Complete Member Function Support: Handles both const and non-const member functions
  • Type Safety: Maintains compile-time type checking while optimizing runtime performance
  • Familiar Interface: Same API as standard Delegate for easy adoption

🔧 Implementation Details

Core Files Added:

  • cpp/Platform.Delegates/Platform.Delegates.NonMutableDelegate.h - Main implementation
  • cpp/Platform.Delegates.Tests/NonMutableDelegateTest.cpp - Comprehensive test suite
  • Updated Platform.Delegates.h to include new header

Architecture:

  • Template specialization following same pattern as existing Delegate
  • Uses type-erased wrapper classes for member function calls
  • Separate handling for const/non-const member functions
  • Raw pointer storage with user-controlled lifetime responsibility

📊 Use Cases Demonstrated

// Case #1: From standard delegate
auto obj = std::make_shared<MyClass>();
Delegate<void(int)> standardDelegate = {obj, &MyClass::method};
NonMutableDelegate<void(int)> delegate = standardDelegate;

// Case #2: From shared_ptr + method
auto obj = std::make_shared<MyClass>();
NonMutableDelegate<void(int)> delegate = {obj, &MyClass::method};

// Case #3: From object reference + method  
MyClass obj;
NonMutableDelegate<void(int)> delegate = {obj, &MyClass::method};

// Case #4: From unique_ptr + method
auto obj = std::make_unique<MyClass>();
NonMutableDelegate<void(int)> delegate = {obj, &MyClass::method};

⚠️ Key Trade-off

Benefit: Eliminates std::shared_ptr overhead for performance-critical scenarios
Responsibility: User must ensure object lifetime exceeds delegate usage (prevents dangling pointers through scope discipline)

✅ Testing

  • 17 comprehensive test cases covering all construction patterns, equality, assignment, and edge cases
  • Manual verification with standalone test program demonstrating all functionality
  • Compilation verified with g++ C++20 standard
  • All tests pass successfully

🎯 Solves the Core Problem

This implementation directly addresses the issue's core concern: "The main purpose of such a delegate is to avoid creating a std::shared_ptr. Since it can use only objects from its own visibility zone or from a higher one, there will be no problems with dangling pointers"

The NonMutableDelegate provides the requested performance optimization while maintaining type safety and familiar APIs.


🤖 Generated with Claude Code

Adding CLAUDE.md with task information for AI processing.
This file will be removed when the task is complete.

Issue: #19
@konard konard self-assigned this Sep 13, 2025
Add a new delegate specialization that avoids std::shared_ptr overhead for objects with guaranteed lifetime.

Key features:
- Creates delegates from object references, shared_ptr, unique_ptr, and standard delegates
- Uses raw pointers internally to avoid shared_ptr overhead
- Supports both const and non-const member functions
- Maintains same interface as standard Delegate
- Includes comprehensive test coverage

This addresses the need for performance-optimized delegates when object lifetime is guaranteed by scope, eliminating the memory overhead of shared_ptr while preserving type safety.

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
@konard konard changed the title [WIP] Create a new delegate specialization Implement NonMutableDelegate for issue #19 Sep 13, 2025
@konard konard marked this pull request as ready for review September 13, 2025 09:58
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.

Create a new delegate specialization

2 participants