Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Choosing reference over pointer #314

Closed
shakthi-prashanth-m opened this issue Mar 12, 2018 · 2 comments
Closed

Choosing reference over pointer #314

shakthi-prashanth-m opened this issue Mar 12, 2018 · 2 comments
Assignees

Comments

@shakthi-prashanth-m
Copy link
Contributor

It is recommended to use references when we can, and pointers when we have to.
(Reference: https://isocpp.org/wiki/faq/references#refs-vs-ptrs)

We can replace Device *_parent to Device & _parent as I see that, its possible. We initialize while activating plugin and never modify it. Also, we have a method (below) that actually gives, a reference to a device, not pointer.

Device &device(uint64_t uuid);

Consider, below piece of application code:

Device &device = dc.device(); // we get reference to device
auto action = std::make_shared<Action>(&device); // why pass its address ?

// Why not pass it as-is?
auto action = std::make_shared<Action>(device);

I just noticed this.
@julianoes, was it done intentionally for some vital reason? If not, then we can find similar usages and replace them properly.

@julianoes
Copy link
Collaborator

I tend to agree. Thanks for writing that up @shakthi-prashanth-m. Do you agree @JonasVautherin?

@JonasVautherin
Copy link
Collaborator

Yes, I agree. I usually use references and smart pointers instead of raw pointers.

@shakthi-prashanth-m shakthi-prashanth-m self-assigned this Mar 12, 2018
shakthi-prashanth-m pushed a commit that referenced this issue Mar 13, 2018
Replace `Device *PluginImplBase::parent` => `Device &PluginImplBase::parent`.
And replaces everywhere as required.
shakthi-prashanth-m pushed a commit that referenced this issue Mar 13, 2018
Replaced wherever applicabale.
shakthi-prashanth-m pushed a commit that referenced this issue Mar 13, 2018
Smart pointers provide automatic resource management. In other words,
they implement "Resource Acquisition Is Initialialization programming idiom".
The main goal of this idiom is to ensure that resource acquisition occurs
at the same time that the object is initialized, so that all resources
for the object are created and made ready in one line of code.

`std::unique_ptr` is a smart pointer that owns and manages another object
through a pointer and disposes of that object when the unique_ptr goes out of scope.

Ref 1: https://docs.microsoft.com/en-us/cpp/cpp/smart-pointers-modern-cpp
Ref 2: http://en.cppreference.com/w/cpp/memory/unique_ptr
shakthi-prashanth-m pushed a commit that referenced this issue Mar 13, 2018
In `CMakeLists.txt`, include directory for example.h is added.
shakthi-prashanth-m pushed a commit that referenced this issue Mar 13, 2018
This replaces raw pointers used in connections and devices
by smart pointers. This avoids, freeing them in destructor and also
makes code clean and readable.
shakthi-prashanth-m pushed a commit that referenced this issue Mar 13, 2018
Updates all the plugin API description about passing device object,
instead of pointer to it.
JonasVautherin pushed a commit that referenced this issue Mar 14, 2018
Replace `Device *PluginImplBase::parent` => `Device &PluginImplBase::parent`.
And replaces everywhere as required.
JonasVautherin pushed a commit that referenced this issue Mar 14, 2018
Replaced wherever applicabale.
JonasVautherin pushed a commit that referenced this issue Mar 14, 2018
Smart pointers provide automatic resource management. In other words,
they implement "Resource Acquisition Is Initialialization programming idiom".
The main goal of this idiom is to ensure that resource acquisition occurs
at the same time that the object is initialized, so that all resources
for the object are created and made ready in one line of code.

`std::unique_ptr` is a smart pointer that owns and manages another object
through a pointer and disposes of that object when the unique_ptr goes out of scope.

Ref 1: https://docs.microsoft.com/en-us/cpp/cpp/smart-pointers-modern-cpp
Ref 2: http://en.cppreference.com/w/cpp/memory/unique_ptr
JonasVautherin pushed a commit that referenced this issue Mar 14, 2018
In `CMakeLists.txt`, include directory for example.h is added.
JonasVautherin pushed a commit that referenced this issue Mar 14, 2018
This replaces raw pointers used in connections and devices
by smart pointers. This avoids, freeing them in destructor and also
makes code clean and readable.
JonasVautherin pushed a commit that referenced this issue Mar 14, 2018
Updates all the plugin API description about passing device object,
instead of pointer to it.
JonasVautherin added a commit that referenced this issue Mar 14, 2018
JonasVautherin added a commit that referenced this issue Mar 15, 2018
rt-2pm2 pushed a commit to rt-2pm2/DronecodeSDK that referenced this issue Nov 27, 2018
Replace `Device *PluginImplBase::parent` => `Device &PluginImplBase::parent`.
And replaces everywhere as required.
rt-2pm2 pushed a commit to rt-2pm2/DronecodeSDK that referenced this issue Nov 27, 2018
Replaced wherever applicabale.
rt-2pm2 pushed a commit to rt-2pm2/DronecodeSDK that referenced this issue Nov 27, 2018
Smart pointers provide automatic resource management. In other words,
they implement "Resource Acquisition Is Initialialization programming idiom".
The main goal of this idiom is to ensure that resource acquisition occurs
at the same time that the object is initialized, so that all resources
for the object are created and made ready in one line of code.

`std::unique_ptr` is a smart pointer that owns and manages another object
through a pointer and disposes of that object when the unique_ptr goes out of scope.

Ref 1: https://docs.microsoft.com/en-us/cpp/cpp/smart-pointers-modern-cpp
Ref 2: http://en.cppreference.com/w/cpp/memory/unique_ptr
rt-2pm2 pushed a commit to rt-2pm2/DronecodeSDK that referenced this issue Nov 27, 2018
In `CMakeLists.txt`, include directory for example.h is added.
rt-2pm2 pushed a commit to rt-2pm2/DronecodeSDK that referenced this issue Nov 27, 2018
This replaces raw pointers used in connections and devices
by smart pointers. This avoids, freeing them in destructor and also
makes code clean and readable.
rt-2pm2 pushed a commit to rt-2pm2/DronecodeSDK that referenced this issue Nov 27, 2018
Updates all the plugin API description about passing device object,
instead of pointer to it.
rt-2pm2 pushed a commit to rt-2pm2/DronecodeSDK that referenced this issue Nov 27, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants