Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
[surf] Remove signal-related #defines
[simgrid.git] / include / xbt / Facetable.hpp
1 /* Copyright (c) 2015. 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 FacetLevel;
18 template<class T>          class Facetable;
19
20 template<class T, class U>
21 class FacetLevel {
22   static const std::size_t INVALID_ID = std::numeric_limits<std::size_t>::max();
23   std::size_t id_;
24   friend class Facetable<T>;
25   constexpr FacetLevel(std::size_t id) : id_(id) {}
26 public:
27   constexpr FacetLevel() : id_(INVALID_ID) {}
28   std::size_t id() const { return id_; }
29   bool valid() { return id_ != INVALID_ID; }
30 };
31
32 /** A Facetable is an object that you can extend with external facets.
33  *
34  * Facets 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 facet to the Host class, that happens to be Facetable.
45  *
46  */
47 template<class T>
48 class Facetable {
49 private:
50   static std::vector<void(*)(void*)> deleters_;
51 protected:
52   std::vector<void*> facets_;
53 public:
54   static size_t add_level(void (*deleter)(void*))
55   {
56     std::size_t res = deleters_.size();
57     deleters_.push_back(deleter);
58     return res;
59   }
60   template<class U>
61   static FacetLevel<T,U> add_level(void (*deleter)(void*))
62   {
63     return FacetLevel<T,U>(add_level(deleter));
64   }
65   template<class U> static
66   FacetLevel<T,U> add_level()
67   {
68     return add_level([](void* p){ delete static_cast<U*>(p); });
69   }
70   Facetable() : facets_(deleters_.size(), nullptr) {}
71   ~Facetable()
72   {
73     /* Call destructors in reverse order of their registrations
74      *
75      * The rationale for this, is that if a level B as been added after a
76      * facet A, the subsystem of B might depend on the subsystem on A and a
77      * facet of B might need to have the facet of A around when executing
78      * its cleanup function/destructor. */
79     for (std::size_t i = facets_.size(); i > 0; --i)
80       if (facets_[i - 1] != nullptr)
81         deleters_[i - 1](facets_[i - 1]);
82   }
83
84   // Type-unsafe versions of the facet access methods:
85   void* facet(std::size_t level)
86   {
87     if (level >= facets_.size())
88       return nullptr;
89     else
90       return facets_.at(level);
91   }
92   void set_facet(std::size_t level, void* value, bool use_dtor = true)
93   {
94     if (level >= facets_.size())
95       facets_.resize(level + 1, nullptr);
96     void* old_value = this->facet(level);
97     facets_.at(level) = value;
98     if (use_dtor && old_value != nullptr && deleters_[level])
99       deleters_[level](old_value);
100   }
101
102   // Type safe versions of the facet access methods:
103   template<class U>
104   U* facet(FacetLevel<T,U> level)
105   {
106     return static_cast<U*>(facet(level.id()));
107   }
108   template<class U>
109   void set_facet(FacetLevel<T,U> level, U* value, bool use_dtor = true)
110   {
111     set_facet(level.id(), value, use_dtor);
112   }
113
114   // Convnience facet access when the type has a associated LEVEL:
115   template<class U> U* facet()           { return facet<U>(U::LEVEL); }
116   template<class U> void set_facet(U* p) { set_facet<U>(U::LEVEL, p); }
117 };
118
119 template<class T>
120 std::vector<void(*)(void*)> Facetable<T>::deleters_ = {};
121
122 }
123 }
124
125 #endif