This project provides a simple implementation of smart shared and weak pointers in C++. The implementation consists of two main classes: SharedPtr and WeakPtr.
The SharedPtr class represents a shared pointer that manages the lifetime of a dynamically allocated object. It uses reference counting to keep track of the number of SharedPtr instances pointing to the same resource. The resource is deleted only when the last SharedPtr pointing to it is destroyed or reset.
The SharedPtr class provides the following functionalities:
- Constructing a
SharedPtrobject with a raw pointer to a resource. - Copy constructing a
SharedPtrobject from anotherSharedPtrobject. - Assigning a
SharedPtrobject to anotherSharedPtrobject. - Resetting the
SharedPtrobject to manage a new resource or nullptr. - Swapping the managed object with another
SharedPtrobject. - Accessing the managed resource using the dereference and arrow operators.
- Getting the number of
SharedPtrobjects managing the resource.
The WeakPtr class represents a weak pointer that provides a non-owning reference to a resource managed by a SharedPtr. Unlike SharedPtr, WeakPtr does not participate in ownership, and it does not prevent the resource from being deleted. It can be used to check the validity of the resource and obtain a SharedPtr object to it.
The WeakPtr class provides the following functionalities:
- Constructing a
WeakPtrobject. - Copy constructing a
WeakPtrobject from anotherWeakPtrobject or aSharedPtrobject. - Assigning a
WeakPtrobject to anotherWeakPtrobject or aSharedPtrobject. - Resetting the
WeakPtrobject to null. - Swapping the
WeakPtrobject with anotherWeakPtrobject. - Getting the number of
SharedPtrobjects managing the resource. - Checking if the resource has expired (i.e., no
SharedPtrobject managing it). - Attempting to obtain a
SharedPtrobject to the resource using theLockfunction.
An example usage of the SharedPtr and WeakPtr classes is provided in the main.cpp file. It demonstrates how to create, copy, assign, reset, and swap shared and weak pointers. The example includes a simple Test class, and it prints messages to illustrate the lifecycle and usage of the pointers.
To compile and run the example:
- Make sure you have a C++ compiler installed.
- Compile the source files using the command:
g++ -o main main.cpp - Run the executable:
./main
The output will show the creation and destruction of objects, as well as the usage of shared and weak pointers.
Feel free to explore the implementation in the Shared_Ptr.h and Weak_Ptr.h header files to understand the details of the classes.
This implementation provides a basic understanding of shared and weak pointers. However, it is not a complete replacement for the standard library std::shared_ptr and std::weak_ptr. Here are some potential future enhancements:
- Thread safety: Consider adding thread safety mechanisms to ensure correct behavior in multi-threaded environments.
- Additional functionality: Expand the implementation to include features like custom deleters, support for arrays, and other utility functions.
- Testing: Develop a comprehensive test suite to validate the implementation's correctness and edge cases.
Contributions to the project are welcome! If you have any ideas, bug reports, or feature requests, please feel free to submit them.
Enjoy exploring the smart shared and weak pointers implementation! If you have any questions or feedback, please feel free to reach out.