Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/dropbox/nn
Non-nullable pointers for C++
https://github.com/dropbox/nn
Last synced: 18 days ago
JSON representation
Non-nullable pointers for C++
- Host: GitHub
- URL: https://github.com/dropbox/nn
- Owner: dropbox
- License: apache-2.0
- Archived: true
- Created: 2015-07-07T17:59:21.000Z (over 9 years ago)
- Default Branch: master
- Last Pushed: 2017-06-23T23:32:33.000Z (over 7 years ago)
- Last Synced: 2024-07-31T22:45:15.302Z (3 months ago)
- Language: C++
- Size: 16.6 KB
- Stars: 314
- Watchers: 34
- Forks: 33
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: license.txt
Awesome Lists containing this project
README
# `nn`: Non-nullable pointers for C++
`nn` is a type that helps you enforce at compile time the contract that a given pointer
can't be null. It wraps around raw pointers or smart pointers, and works particularly
well out of the box with `std::unique_ptr` and `std::shared_ptr`.Here's an example:
```cpp
class Widget : public WidgetBase {
public:
Widget(nn_shared_ptr gadget) : m_gadget(move(gadget)) {}
// ...
private:
nn_shared_ptr m_gadget;
};// nn_make_unique and nn_make_shared always return non-null values
nn_unique_ptr my_widget = nn_make_unique(nn_make_shared());// but what if we have a pointer already and we don't know if it's null?
shared_ptr this_might_be_null = ...
my_widget = nn_make_unique(NN_CHECK_ASSERT(this_might_be_null));// implicit nn_unique_ptr -> nn_shared_ptr works just like unique_ptr -> shared_ptr
nn_shared_ptr shared_widget = move(my_widget);// the `nn` implicitly casts away if needed
void save_ownership_somewhere(shared_ptr);
save_ownership_somewhere(shared_widget);// implicit upcasts work too
nn_shared_ptr base_ptr = shared_widget;
```Compile-time checking helps find bugs sooner. At Dropbox we use `nn` pervasively in our
cross-platform C++ codebase.## Compatibility
nn is C++11 compatible. Tested with GCC 4.8, clang 3.6+ and MSVS 2015.