[libcamera-devel] libcamera: signal: Don't use reinterpret_cast<>() to perform downcasts

Message ID 20190126163257.10417-1-laurent.pinchart@ideasonboard.com
State Accepted
Commit eae59ca2cdc68236136685c52da17f6c37515d59
Headers show
Series
  • [libcamera-devel] libcamera: signal: Don't use reinterpret_cast<>() to perform downcasts
Related show

Commit Message

Laurent Pinchart Jan. 26, 2019, 4:32 p.m. UTC
Use static_cast<>() instead of reinterpret_cast<>() to perform
downcasts, as reinterpret_cast<>() isn't meant (and guaranteed to be
safe) for that purpose.

Signed-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
---
 include/libcamera/signal.h | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

Comments

Niklas Söderlund Jan. 27, 2019, 12:42 a.m. UTC | #1
Hi Laurent,

Thanks for your work.

On 2019-01-26 18:32:57 +0200, Laurent Pinchart wrote:
> Use static_cast<>() instead of reinterpret_cast<>() to perform
> downcasts, as reinterpret_cast<>() isn't meant (and guaranteed to be
> safe) for that purpose.
> 
> Signed-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>

Reviewed-by: Niklas Söderlund <niklas.soderlund@ragnatech.se>

> ---
>  include/libcamera/signal.h | 6 +++---
>  1 file changed, 3 insertions(+), 3 deletions(-)
> 
> diff --git a/include/libcamera/signal.h b/include/libcamera/signal.h
> index 0b437a486c08..c375b0a878af 100644
> --- a/include/libcamera/signal.h
> +++ b/include/libcamera/signal.h
> @@ -37,7 +37,7 @@ public:
>  	SlotMember(T *obj, void(T::*func)(Args...))
>  		: SlotBase<Args...>(obj), func_(func) { }
>  
> -	void invoke(Args... args) { (reinterpret_cast<T *>(this->obj_)->*func_)(args...); }
> +	void invoke(Args... args) { (static_cast<T *>(this->obj_)->*func_)(args...); }
>  
>  private:
>  	friend class Signal<Args...>;
> @@ -111,7 +111,7 @@ public:
>  			 * match, so we can safely cast to SlotMember<T, Args>.
>  			 */
>  			if (slot->obj_ == object &&
> -			    reinterpret_cast<SlotMember<T, Args...> *>(slot)->func_ == func) {
> +			    static_cast<SlotMember<T, Args...> *>(slot)->func_ == func) {
>  				iter = slots_.erase(iter);
>  				delete slot;
>  			} else {
> @@ -125,7 +125,7 @@ public:
>  		for (auto iter = slots_.begin(); iter != slots_.end(); ) {
>  			SlotBase<Args...> *slot = *iter;
>  			if (slot->obj_ == nullptr &&
> -			    reinterpret_cast<SlotStatic<Args...> *>(slot)->func_ == func) {
> +			    static_cast<SlotStatic<Args...> *>(slot)->func_ == func) {
>  				iter = slots_.erase(iter);
>  				delete slot;
>  			} else {
> -- 
> Regards,
> 
> Laurent Pinchart
> 
> _______________________________________________
> libcamera-devel mailing list
> libcamera-devel@lists.libcamera.org
> https://lists.libcamera.org/listinfo/libcamera-devel

Patch

diff --git a/include/libcamera/signal.h b/include/libcamera/signal.h
index 0b437a486c08..c375b0a878af 100644
--- a/include/libcamera/signal.h
+++ b/include/libcamera/signal.h
@@ -37,7 +37,7 @@  public:
 	SlotMember(T *obj, void(T::*func)(Args...))
 		: SlotBase<Args...>(obj), func_(func) { }
 
-	void invoke(Args... args) { (reinterpret_cast<T *>(this->obj_)->*func_)(args...); }
+	void invoke(Args... args) { (static_cast<T *>(this->obj_)->*func_)(args...); }
 
 private:
 	friend class Signal<Args...>;
@@ -111,7 +111,7 @@  public:
 			 * match, so we can safely cast to SlotMember<T, Args>.
 			 */
 			if (slot->obj_ == object &&
-			    reinterpret_cast<SlotMember<T, Args...> *>(slot)->func_ == func) {
+			    static_cast<SlotMember<T, Args...> *>(slot)->func_ == func) {
 				iter = slots_.erase(iter);
 				delete slot;
 			} else {
@@ -125,7 +125,7 @@  public:
 		for (auto iter = slots_.begin(); iter != slots_.end(); ) {
 			SlotBase<Args...> *slot = *iter;
 			if (slot->obj_ == nullptr &&
-			    reinterpret_cast<SlotStatic<Args...> *>(slot)->func_ == func) {
+			    static_cast<SlotStatic<Args...> *>(slot)->func_ == func) {
 				iter = slots_.erase(iter);
 				delete slot;
 			} else {