Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
give a set_data() and get_data() to all xbt::Extendable structures
authorMartin Quinson <martin.quinson@ens-rennes.fr>
Sat, 14 Sep 2019 19:43:21 +0000 (21:43 +0200)
committerMartin Quinson <martin.quinson@ens-rennes.fr>
Sat, 14 Sep 2019 20:25:54 +0000 (22:25 +0200)
This is for C users, and it will unify all versions of the same
feature all over the place.

include/xbt/Extendable.hpp

index 37946b6..d95d186 100644 (file)
@@ -52,6 +52,9 @@ private:
 public:
   static size_t extension_create(void (*deleter)(void*))
   {
 public:
   static size_t extension_create(void (*deleter)(void*))
   {
+    if (deleters_.empty()) { // Save space for void* user data
+      deleters_.push_back(nullptr);
+    }
     deleters_.push_back(deleter);
     return deleters_.size() - 1;
   }
     deleters_.push_back(deleter);
     return deleters_.size() - 1;
   }
@@ -65,7 +68,7 @@ public:
   {
     return Extension<T, U>(extension_create([](void* p) { delete static_cast<U*>(p); }));
   }
   {
     return Extension<T, U>(extension_create([](void* p) { delete static_cast<U*>(p); }));
   }
-  Extendable() : extensions_(deleters_.size(), nullptr) {}
+  Extendable() : extensions_((deleters_.size() > 0 ? deleters_.size() : 1), nullptr) {}
   Extendable(const Extendable&) = delete;
   Extendable& operator=(const Extendable&) = delete;
   ~Extendable()
   Extendable(const Extendable&) = delete;
   Extendable& operator=(const Extendable&) = delete;
   ~Extendable()
@@ -76,7 +79,7 @@ public:
      * an extension A, the subsystem of B might depend on the subsystem on A and
      * an extension of B might need to have the extension of A around when executing
      * its cleanup function/destructor. */
      * an extension A, the subsystem of B might depend on the subsystem on A and
      * an extension of B might need to have the extension of A around when executing
      * its cleanup function/destructor. */
-    for (std::size_t i = extensions_.size(); i > 0; --i)
+    for (std::size_t i = extensions_.size(); i > 1; --i) // rank=0 is the spot of user's void*
       if (extensions_[i - 1] != nullptr && deleters_[i - 1] != nullptr)
         deleters_[i - 1](extensions_[i - 1]);
   }
       if (extensions_[i - 1] != nullptr && deleters_[i - 1] != nullptr)
         deleters_[i - 1](extensions_[i - 1]);
   }
@@ -103,7 +106,13 @@ public:
   {
     extension_set(rank.id(), value, use_dtor);
   }
   {
     extension_set(rank.id(), value, use_dtor);
   }
-
+  // void* version, for C users and nostalgics
+  void set_data(void* data){
+    extensions_[0]=data;
+  }
+  void* get_data(){
+    return extensions_[0];
+  }
   // Convenience extension access when the type has a 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); }
   // Convenience extension access when the type has a 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); }