Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
c85201fec69806c60baab318bc980696dc6b26eb
[simgrid.git] / include / xbt / Extendable.hpp
1 /* Copyright (c) 2015-2019. The SimGrid Team.
2  * All rights reserved.                                                     */
3
4 /* This program is free software; you can redistribute it and/or modify it
5  * under the terms of the license (GNU LGPL) which comes with this package. */
6
7 #ifndef SIMGRID_XBT_LIB_HPP
8 #define SIMGRID_XBT_LIB_HPP
9
10 #include <cstddef>
11 #include <limits>
12 #include <vector>
13
14 namespace simgrid {
15 namespace xbt {
16
17 template<class T, class U> class Extension;
18 template<class T>          class Extendable;
19
20 template<class T, class U>
21 class Extension {
22   static const std::size_t INVALID_ID = std::numeric_limits<std::size_t>::max();
23   std::size_t id_;
24   friend class Extendable<T>;
25   explicit constexpr Extension(std::size_t id) : id_(id) {}
26 public:
27   explicit constexpr Extension() : id_(INVALID_ID) {}
28   std::size_t id() const { return id_; }
29   bool valid() const { return id_ != INVALID_ID; }
30 };
31
32 /** An Extendable is an object that you can extend with external elements.
33  *
34  * An Extension is one dimension of such extension. They are similar to the concept of mixins, that is, a set of behavior that is injected into a class without derivation.
35  *
36  * Imagine that you want to write a plugin dealing with the energy in SimGrid.
37  * You will have to store some information about each and every host.
38  *
39  * You could modify the Host class directly (but your code will soon become messy).
40  * You could create a class EnergyHost deriving Host, but it is not easily combinable
41  *    with a notion of Host extended with another concept (such as mobility).
42  * You could completely externalize these data with an associative map Host->EnergyHost.
43  *    It would work, provided that you implement this classical feature correctly (and it would induce a little performance penalty).
44  * Instead, you should add a new extension to the Host class, that happens to be Extendable.
45  *
46  */
47 template<class T>
48 class Extendable {
49 private:
50   static std::vector<void(*)(void*)> deleters_;
51   std::vector<void*> extensions_;
52 public:
53   static size_t extension_create(void (*deleter)(void*))
54   {
55     if (deleters_.empty()) { // Save space for void* user data
56       deleters_.push_back(nullptr);
57     }
58     deleters_.push_back(deleter);
59     return deleters_.size() - 1;
60   }
61   template<class U>
62   static Extension<T,U> extension_create(void (*deleter)(void*))
63   {
64     return Extension<T,U>(extension_create(deleter));
65   }
66   template<class U> static
67   Extension<T,U> extension_create()
68   {
69     return Extension<T, U>(extension_create([](void* p) { delete static_cast<U*>(p); }));
70   }
71   Extendable() : extensions_((deleters_.size() > 0 ? deleters_.size() : 1), nullptr) {}
72   Extendable(const Extendable&) = delete;
73   Extendable& operator=(const Extendable&) = delete;
74   ~Extendable()
75   {
76     /* Call destructors in reverse order of their registrations
77      *
78      * The rationale for this, is that if an extension B as been added after
79      * an extension A, the subsystem of B might depend on the subsystem on A and
80      * an extension of B might need to have the extension of A around when executing
81      * its cleanup function/destructor. */
82     for (std::size_t i = extensions_.size(); i > 1; --i) // rank=0 is the spot of user's void*
83       if (extensions_[i - 1] != nullptr && deleters_[i - 1] != nullptr)
84         deleters_[i - 1](extensions_[i - 1]);
85   }
86
87   // Type-unsafe versions of the facet access methods:
88   void* extension(std::size_t rank) const
89   {
90     return rank < extensions_.size() ? extensions_[rank] : nullptr;
91   }
92   void extension_set(std::size_t rank, void* value, bool use_dtor = true)
93   {
94     if (rank >= extensions_.size())
95       extensions_.resize(rank + 1, nullptr);
96     void* old_value = this->extension(rank);
97     extensions_[rank] = value;
98     if (use_dtor && old_value != nullptr && deleters_[rank])
99       deleters_[rank](old_value);
100   }
101
102   // Type safe versions of the facet access methods:
103   template <class U> U* extension(Extension<T, U> rank) const { return static_cast<U*>(extension(rank.id())); }
104   template<class U>
105   void extension_set(Extension<T,U> rank, U* value, bool use_dtor = true)
106   {
107     extension_set(rank.id(), value, use_dtor);
108   }
109   // void* version, for C users and nostalgics
110   void set_data(void* data){
111     extensions_[0]=data;
112   }
113   void* get_data() const { return extensions_[0]; }
114   // Convenience extension access when the type has a associated EXTENSION ID:
115   template <class U> U* extension() const { return extension<U>(U::EXTENSION_ID); }
116   template<class U> void extension_set(U* p) { extension_set<U>(U::EXTENSION_ID, p); }
117 };
118
119 template <class T> std::vector<void (*)(void*)> Extendable<T>::deleters_;
120 }
121 }
122
123 #endif