Skip to content

Translate C# Singletons library to C++#51

Open
konard wants to merge 3 commits intomainfrom
issue-23-ad10344a
Open

Translate C# Singletons library to C++#51
konard wants to merge 3 commits intomainfrom
issue-23-ad10344a

Conversation

@konard
Copy link
Copy Markdown
Member

@konard konard commented Sep 13, 2025

🤖 AI-Powered Solution

This pull request successfully translates the C# Platform.Singletons library to C++.

📋 Issue Reference

Fixes #23

✅ Implementation Complete

Core Components Translated:

  1. Global.h/.cpp - Static global class with Trash property for performance testing

    • Translated C# auto-property to getter/setter methods and reference accessor
    • Added static member variable definition in .cpp file
  2. Default[T].h - Template class providing default instances

    • Instance: Static const member providing default-constructed objects
    • GetThreadInstance(): Thread-local storage for per-thread instances
    • Uses static_assert for default constructibility constraint
  3. SingletonFactory (Singleton.h) - Factory class for creating singletons

    • Create(): Template method to create Singleton instances
    • Get(): Template method for direct singleton access
  4. Singleton[T].h - Template struct for function-based singleton management

    • Thread-safe singleton creation using std::mutex
    • Function object address used as unique identifier
    • Maintains singleton instances in static std::unordered_map

Testing & Build System:

  • Test Translation: All C# xUnit tests converted to C++ assert-based tests
  • CMakeLists.txt: Complete build configuration for library and tests
  • Test Runner: Simple console application running all tests
  • All tests pass successfully

Key Technical Differences from C#:

C# Feature C++ Translation
ConcurrentDictionary std::unordered_map + std::mutex
[ThreadStatic] thread_local
Auto-properties Getter/setter methods + reference accessor
Reflection/IL bytes Function object address for uniqueness
new() constraint std::is_default_constructible_v<T> constraint

Limitations:

The C++ version has one known limitation compared to the C# version:

  • Different lambda functions with identical content will be treated as separate singletons (different addresses)
  • C# version uses IL byte comparison for true semantic equality
  • This is a fundamental difference in how C++ vs C# handle function equality

Build & Test:

cd cpp
mkdir build && cd build
cmake .. && make
./Platform.Singletons.Tests

🏁 Status

Ready for Review - Complete translation with passing tests and full CMake build system.


🤖 Generated with Claude Code

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

Issue: #23
@konard konard self-assigned this Sep 13, 2025
Complete translation of Platform.Singletons from C# to C++:

- Global.h/.cpp: Translated Global static class with Trash property
- Default[T].h: Translated Default<T> template class with Instance and ThreadInstance
- Singleton.h: Translated SingletonFactory class for creating singletons
- Singleton[T].h: Translated Singleton<T> struct with function-based singleton creation
- Updated all test files with proper C++ syntax and testing approach
- Created CMakeLists.txt for building the project
- All tests pass successfully

Key differences from C# version:
- Used std::unordered_map instead of ConcurrentDictionary
- Used std::mutex for thread safety instead of concurrent collections
- Used function object address for uniqueness instead of IL byte comparison
- Used C++17 features like template argument deduction

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

Co-Authored-By: Claude <noreply@anthropic.com>
@konard konard changed the title [WIP] Translate the code from C# to C++ Translate C# Singletons library to C++ Sep 13, 2025
@konard konard marked this pull request as ready for review September 13, 2025 22:37
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.

Translate the code from C# to C++

1 participant