Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Fix sonar issues in ns3 bindings.
[simgrid.git] / include / xbt / Extendable.hpp
index d47298d..195f287 100644 (file)
@@ -1,4 +1,4 @@
-/* Copyright (c) 2015-2020. The SimGrid Team.
+/* Copyright (c) 2015-2022. The SimGrid Team.
  * All rights reserved.                                                     */
 
 /* This program is free software; you can redistribute it and/or modify it
@@ -7,6 +7,7 @@
 #ifndef SIMGRID_XBT_LIB_HPP
 #define SIMGRID_XBT_LIB_HPP
 
+#include "xbt/base.h" // XBT_ATTRIB_DEPRECATED_v334
 #include <cstddef>
 #include <limits>
 #include <vector>
@@ -24,7 +25,7 @@ class Extension {
   friend class Extendable<T>;
   explicit constexpr Extension(std::size_t id) : id_(id) {}
 public:
-  explicit constexpr Extension() {}
+  explicit constexpr Extension() = default;
   std::size_t id() const { return id_; }
   bool valid() const { return id_ != INVALID_ID; }
 };
@@ -48,7 +49,7 @@ template<class T>
 class Extendable {
 private:
   static std::vector<void(*)(void*)> deleters_;
-  std::vector<void*> extensions_{(deleters_.size() > 0 ? deleters_.size() : 1), nullptr};
+  std::vector<void*> extensions_{std::max<decltype(deleters_.size())>(1, deleters_.size()), nullptr};
 
 public:
   static size_t extension_create(void (*deleter)(void*))
@@ -69,7 +70,7 @@ public:
   {
     return Extension<T, U>(extension_create([](void* p) { delete static_cast<U*>(p); }));
   }
-  Extendable() {}
+  Extendable()                  = default;
   Extendable(const Extendable&) = delete;
   Extendable& operator=(const Extendable&) = delete;
   ~Extendable()
@@ -111,8 +112,12 @@ public:
   void set_data(void* data){
     extensions_[0]=data;
   }
-  void* get_data() const { return extensions_[0]; }
-  // Convenience extension access when the type has a associated EXTENSION ID:
+  template <typename D> D* get_data() const { return static_cast<D*>(extensions_[0]); }
+  XBT_ATTRIB_DEPRECATED_v334("Please use typed template Extendable::get_data<>()") void* get_data() const
+  {
+    return get_data<void>();
+  }
+  // Convenience extension access when the type has an associated EXTENSION ID:
   template <class U> U* extension() const { return extension<U>(U::EXTENSION_ID); }
   template<class U> void extension_set(U* p) { extension_set<U>(U::EXTENSION_ID, p); }
 };