Appearance
shared_ptr with Custom Deleter
Overview
shared_ptr has several complex requirements:
- Reference counting: Track number of owners
- Control block: Separate storage for metadata (reference count)
- Copy semantics: Increase count when copying
- Destructor logic: Decrease count, delete when zero
- Exception safety: Proper cleanup even with exceptions
- Move optimization: Transfer without atomic operations
Your task is to implement a simplified version of shared_ptr that satisfies these requirements. Additionally, you should add a custom deleter to the shared_ptr to control how the resource is freed.
Implement a simplified version of std::shared_ptr called 'MySharedPtr' with support for custom deleters.
cpp
#include <utility>
#include <functional>
// TODO: Implement a simplified version of std::shared_ptr called 'MySharedPtr' with custom deleters
//
// Requirements:
// 1. Template class that manages shared ownership using reference counting
// 2. Control block structure to store pointer, reference count, and custom deleter
// 3. Constructor taking a raw pointer and custom deleter (default to delete)
// 4. Copy constructor and copy assignment (increment reference count)
// 5. Move constructor and move assignment (transfer without count change)
// 6. Destructor that decrements count and calls custom deleter when count reaches zero
// 7. Operator* and operator-> for accessing the managed object
// 8. get() method that returns the raw pointer
// 9. use_count() method that returns current reference count
// 10. reset() method that decrements current count and optionally takes new pointer with deleter
//
// Custom deleter support:
// - Function pointer deleters
// - Lambda deleters
// - Functor deleters
// - Default deleter (delete)
//
// This demonstrates:
// - Reference counting for shared ownership
// - Control block pattern with custom deleters
// - Complex copy/move semantics with resource sharing
// - Template class design for generic shared ownership
// - Custom deleter patterns for different resource types