Logo AND Algorithmique Numérique Distribuée

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