Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Test using macros for arguments checking, saves some duplicated lines.
[simgrid.git] / include / xbt / Extendable.hpp
index 785e337..37946b6 100644 (file)
@@ -1,4 +1,4 @@
-/* Copyright (c) 2015, 2017. The SimGrid Team.
+/* Copyright (c) 2015-2019. The SimGrid Team.
  * All rights reserved.                                                     */
 
 /* This program is free software; you can redistribute it and/or modify it
@@ -26,7 +26,7 @@ class Extension {
 public:
   explicit constexpr Extension() : id_(INVALID_ID) {}
   std::size_t id() const { return id_; }
-  bool valid() { return id_ != INVALID_ID; }
+  bool valid() const { return id_ != INVALID_ID; }
 };
 
 /** An Extendable is an object that you can extend with external elements.
@@ -52,9 +52,8 @@ private:
 public:
   static size_t extension_create(void (*deleter)(void*))
   {
-    std::size_t res = deleters_.size();
     deleters_.push_back(deleter);
-    return res;
+    return deleters_.size() - 1;
   }
   template<class U>
   static Extension<T,U> extension_create(void (*deleter)(void*))
@@ -67,6 +66,8 @@ public:
     return Extension<T, U>(extension_create([](void* p) { delete static_cast<U*>(p); }));
   }
   Extendable() : extensions_(deleters_.size(), nullptr) {}
+  Extendable(const Extendable&) = delete;
+  Extendable& operator=(const Extendable&) = delete;
   ~Extendable()
   {
     /* Call destructors in reverse order of their registrations
@@ -81,29 +82,22 @@ public:
   }
 
   // Type-unsafe versions of the facet access methods:
-  void* extension(std::size_t rank)
+  void* extension(std::size_t rank) const
   {
-    if (rank >= extensions_.size())
-      return nullptr;
-    else
-      return extensions_.at(rank);
+    return rank < extensions_.size() ? extensions_[rank] : nullptr;
   }
   void extension_set(std::size_t rank, void* value, bool use_dtor = true)
   {
     if (rank >= extensions_.size())
       extensions_.resize(rank + 1, nullptr);
     void* old_value = this->extension(rank);
-    extensions_.at(rank) = value;
+    extensions_[rank] = value;
     if (use_dtor && old_value != nullptr && deleters_[rank])
       deleters_[rank](old_value);
   }
 
   // Type safe versions of the facet access methods:
-  template<class U>
-  U* extension(Extension<T,U> rank)
-  {
-    return static_cast<U*>(extension(rank.id()));
-  }
+  template <class U> U* extension(Extension<T, U> rank) const { return static_cast<U*>(extension(rank.id())); }
   template<class U>
   void extension_set(Extension<T,U> rank, U* value, bool use_dtor = true)
   {
@@ -111,13 +105,11 @@ public:
   }
 
   // Convenience extension access when the type has a associated EXTENSION ID:
-  template<class U> U* extension()           { return extension<U>(U::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); }
 };
 
-template<class T>
-std::vector<void(*)(void*)> Extendable<T>::deleters_ = {};
-
+template <class T> std::vector<void (*)(void*)> Extendable<T>::deleters_;
 }
 }