Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge pull request #302 from mpoquet/rename-s4u-synchro-examples
[simgrid.git] / include / xbt / Extendable.hpp
index 81e7150..0f51c23 100644 (file)
@@ -1,4 +1,4 @@
-/* Copyright (c) 2015. The SimGrid Team.
+/* Copyright (c) 2015-2018. The SimGrid Team.
  * All rights reserved.                                                     */
 
 /* This program is free software; you can redistribute it and/or modify it
@@ -22,9 +22,9 @@ class Extension {
   static const std::size_t INVALID_ID = std::numeric_limits<std::size_t>::max();
   std::size_t id_;
   friend class Extendable<T>;
-  constexpr Extension(std::size_t id) : id_(id) {}
+  explicit constexpr Extension(std::size_t id) : id_(id) {}
 public:
-  constexpr Extension() : id_(INVALID_ID) {}
+  explicit constexpr Extension() : id_(INVALID_ID) {}
   std::size_t id() const { return id_; }
   bool valid() { return id_ != INVALID_ID; }
 };
@@ -41,14 +41,13 @@ public:
  *    with a notion of Host extended with another concept (such as mobility).
  * You could completely externalize these data with an associative map Host->EnergyHost.
  *    It would work, provided that you implement this classical feature correctly (and it would induce a little performance penalty).
- * Instead, you should add a new facet to the Host class, that happens to be Facetable.
+ * Instead, you should add a new extension to the Host class, that happens to be Extendable.
  *
  */
 template<class T>
 class Extendable {
 private:
   static std::vector<void(*)(void*)> deleters_;
-protected:
   std::vector<void*> extensions_;
 public:
   static size_t extension_create(void (*deleter)(void*))
@@ -65,7 +64,7 @@ public:
   template<class U> static
   Extension<T,U> extension_create()
   {
-    return 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()
@@ -77,7 +76,7 @@ public:
      * 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)
-      if (extensions_[i - 1] != nullptr)
+      if (extensions_[i - 1] != nullptr && deleters_[i - 1] != nullptr)
         deleters_[i - 1](extensions_[i - 1]);
   }