https://github.com/meircif/lumi-safe-ptr
Safe wrapper of C++ smart pointers
https://github.com/meircif/lumi-safe-ptr
cplusplus cplusplus-11 hacktoberfest header-only smart-pointers
Last synced: 11 months ago
JSON representation
Safe wrapper of C++ smart pointers
- Host: GitHub
- URL: https://github.com/meircif/lumi-safe-ptr
- Owner: meircif
- License: mit
- Created: 2023-07-24T07:22:01.000Z (over 2 years ago)
- Default Branch: master
- Last Pushed: 2023-08-01T15:07:07.000Z (over 2 years ago)
- Last Synced: 2025-01-29T14:53:49.123Z (about 1 year ago)
- Topics: cplusplus, cplusplus-11, hacktoberfest, header-only, smart-pointers
- Language: C++
- Homepage:
- Size: 22.5 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
- Code of conduct: .github/CODE_OF_CONDUCT.md
Awesome Lists containing this project
README
[](https://github.com/meircif/lumi-safe-ptr/actions)
# Safe C++ Smart Pointers Wrapper
The "Lumi" project goal is to provide tools for writing safe and efficient code.
This `lumi-safe-ptr` library provides C++ smart pointers wrappers that makes
them even more safe to use.
While C++ smart pointers help a lot in writing memory safe code - they still
have some pitfalls and problems that users can mistakenly fall into. These are
the issues that `lumi-safe-ptr` library solves:
* The infamous [`shared_ptr` circular reference issue](
#avoid-shared_ptr-circular-references)
* More [planned in the future...](#future-work)
## Installation and Usage
This is a header-only library with a single header file located in
[include/lumi-safe-ptr.hpp](include/lumi-safe-ptr.hpp).
This file can be simply downloaded and included by
`#include "lumi-safe-ptr.hpp"`. All macros start with `LUMI_` prefix and all
other symbols are inside `lumi::` namespace.
## Avoid shared_ptr circular references
This is the most known `shared_ptr` issue that is now avoidable 😃.
It can be solved using macros `LUMI_CYCLE_CHECK` and `LUMI_STRONG_PTR` that will
raise a compilation error when a circular reference is detected. Their usage is
simple:
1. Every `shared_ptr` member in a type should be declared using
`LUMI_STRONG_PTR(type) name;` (instead of `std::shared_ptr name;`).
2. Before Every type that uses `LUMI_STRONG_PTR` add (in the global scope)
`LUMI_CYCLE_CHECK(type, pointed_types...)`
For example:
```c++
#include "lumi-safe-ptr.hpp"
LUMI_CYCLE_CHECK(module::A, module::B, module::C)
namespace module {
class A {
LUMI_STRONG_PTR(B) b;
LUMI_STRONG_PTR(C) c;
};
}
```
## Future Work
More Issues that may be solved next as long as they can be technically done
using C++ syntax:
1. Access moved `unique_ptr`
2. Using unchecked `weak_ptr`
3. Deleting the raw pointers
4. Thread safety
5. Using non-heap pointers
6. Not using `make_shared`
7. Safe arrays of pointers
8. Derived to base class pointer
9. Garbage collecting pointer