67 #ifndef DAL_STATIC_STORED_OBJECTS_H__ 
   68 #define DAL_STATIC_STORED_OBJECTS_H__ 
   78 #include "getfem/getfem_arch_config.h" 
   82 #define DAL_STORED_OBJECT_DEBUG 0 
   87 #if DAL_STORED_OBJECT_DEBUG 
   94   class static_stored_object;
 
   95   void stored_debug_created(
const static_stored_object *o,
 
   96                             const std::string &name);
 
   97   void stored_debug_added(
const static_stored_object *o);
 
   98   void stored_debug_deleted(
const static_stored_object *o);
 
   99   void stored_debug_destroyed(
const static_stored_object *o,
 
  100                               const std::string &name);
 
  101 # define DAL_STORED_OBJECT_DEBUG_CREATED(o, name) stored_debug_created(o, name) 
  102 # define DAL_STORED_OBJECT_DEBUG_ADDED(o)   stored_debug_added(o) 
  103 # define DAL_STORED_OBJECT_DEBUG_DELETED(o) stored_debug_deleted(o) 
  104 # define DAL_STORED_OBJECT_DEBUG_DESTROYED(o, name) \ 
  105                                             stored_debug_destroyed(o, name) 
  107 # define DAL_STORED_OBJECT_DEBUG_CREATED(o, name) 
  108 # define DAL_STORED_OBJECT_DEBUG_ADDED(o) 
  109 # define DAL_STORED_OBJECT_DEBUG_DELETED(o) 
  110 # define DAL_STORED_OBJECT_DEBUG_DESTROYED(o, name) 
  113   enum permanence { PERMANENT_STATIC_OBJECT = 0, 
 
  114     STRONG_STATIC_OBJECT = 1,    
 
  115     STANDARD_STATIC_OBJECT = 2,  
 
  116     WEAK_STATIC_OBJECT = 3,      
 
  117     AUTODELETE_STATIC_OBJECT = 4 
 
  121   class static_stored_object_key {
 
  123     virtual bool compare(
const static_stored_object_key &) 
const = 0;
 
  124     virtual bool equal(
const static_stored_object_key &) 
const = 0;
 
  127     bool operator < (
const static_stored_object_key &o)
 const {
 
  129       if (
typeid(*this).before(
typeid(o))) 
return true;
 
  130       if (
typeid(o).before(
typeid(*
this))) 
return false;
 
  134     bool operator == (
const static_stored_object_key &o)
 const {
 
  135       if (
typeid(o)!=
typeid(*
this)) 
return false;
 
  139     bool operator != (
const static_stored_object_key &o)
 const {
 
  140       return !(*
this == o);
 
  143     virtual ~static_stored_object_key() {}
 
  146   template <
typename var_type>
 
  147   class simple_key : 
virtual public static_stored_object_key {
 
  150      bool compare(
const static_stored_object_key &oo)
 const override {
 
  151       auto &o = 
dynamic_cast<const simple_key &
>(oo);
 
  155     bool equal(
const static_stored_object_key &oo)
 const override {
 
  156       auto &o = 
dynamic_cast<const simple_key &
>(oo);
 
  159     simple_key(var_type aa) : a(aa) {}
 
  162 #define DAL_SIMPLE_KEY(class_name, var_type)                            \ 
  163   struct class_name : public dal::simple_key<var_type> {                \ 
  164   class_name(var_type aa) : dal::simple_key<var_type>(aa) {}            \ 
  167 #define DAL_DOUBLE_KEY(class_name, var_type1, var_type2)                \ 
  168   struct class_name :                                                   \ 
  169   public dal::simple_key<std::pair<var_type1,var_type2> > {             \ 
  170   class_name(var_type1 aa, var_type2 bb) :                              \ 
  171   dal::simple_key<std::pair<var_type1,var_type2> >                      \ 
  172   (std::make_pair(aa,bb)) {}                                            \ 
  175 #define DAL_TRIPLE_KEY(class_name, var_type1, var_type2, var_type3)     \ 
  176   struct class_name :                                                   \ 
  177   public dal::simple_key<std::pair<var_type1,                           \ 
  178                                    std::pair<var_type2,var_type3> > > { \ 
  179   class_name(var_type1 aa, var_type2 bb, var_type3 cc) :                \ 
  180     dal::simple_key<std::pair<var_type1,                                \ 
  181                               std::pair<var_type2, var_type3> > >       \ 
  182     (std::make_pair(aa,std::make_pair(bb,cc))) {}                       \ 
  185 #define DAL_FOUR_KEY(class_name,var_type1,var_type2,var_type3,var_type4)\ 
  186   struct class_name : public                                            \ 
  187   dal::simple_key<std::pair                                             \ 
  188                   <var_type1, std::pair<var_type2, std::pair            \ 
  189                                         <var_type3,var_type4> > > > {   \ 
  190     class_name(var_type1 aa, var_type2 bb, var_type3 cc,var_type4 dd) : \ 
  191       dal::simple_key<std::pair                                         \ 
  192                       <var_type1, std::pair<var_type2,                  \ 
  193                                             std::pair<var_type3,        \ 
  195       (std::make_pair(aa,std::make_pair(bb,std::make_pair(cc, dd)))) {} \ 
  199   typedef std::shared_ptr<const static_stored_object_key>
 
  200     pstatic_stored_object_key;
 
  208   typedef std::shared_ptr<const static_stored_object> pstatic_stored_object;
 
  210   pstatic_stored_object_key key_of_stored_object(pstatic_stored_object o);
 
  215   pstatic_stored_object search_stored_object_on_all_threads(pstatic_stored_object_key k);
 
  221   void add_dependency(pstatic_stored_object o1, pstatic_stored_object o2);
 
  224   bool del_dependency(pstatic_stored_object o1, pstatic_stored_object o2);
 
  228                          permanence perm = STANDARD_STATIC_OBJECT);
 
  232     pstatic_stored_object dep1,
 
  233     permanence perm = STANDARD_STATIC_OBJECT) {
 
  240     pstatic_stored_object dep1, pstatic_stored_object dep2,
 
  241     permanence perm = STANDARD_STATIC_OBJECT) {
 
  249                     pstatic_stored_object dep1, pstatic_stored_object dep2,
 
  250                     pstatic_stored_object dep3,
 
  251                     permanence perm = STANDARD_STATIC_OBJECT) {
 
  260                     pstatic_stored_object dep1, pstatic_stored_object dep2,
 
  261                     pstatic_stored_object dep3, pstatic_stored_object dep4,
 
  262                     permanence perm = STANDARD_STATIC_OBJECT) {
 
  272                          bool ignore_unstored=
false);
 
  285     bool ignore_unstored);
 
  293     pstatic_stored_object p;
 
  294     std::atomic_bool valid;
 
  295     const permanence perm;
 
  296     std::set<pstatic_stored_object> dependent_object;
 
  297     std::set<pstatic_stored_object> dependencies;
 
  299       : p(o), perm(perma) {valid = 
true;}
 
  301       : perm(STANDARD_STATIC_OBJECT) {valid = 
true;}
 
  303       : p(enr_o.p), perm(enr_o.perm), dependent_object(enr_o.dependent_object),
 
  304       dependencies(enr_o.dependencies){valid = 
static_cast<bool>(enr_o.perm);}
 
  311     pstatic_stored_object_key p;
 
  313     { 
return (*p) < (*(o.p)); }
 
  321     public std::map<enr_static_stored_object_key, enr_static_stored_object> {
 
  323     typedef std::map<pstatic_stored_object,pstatic_stored_object_key>
 
  328     pstatic_stored_object
 
  329       search_stored_object(pstatic_stored_object_key k) 
const;
 
  330     bool has_dependent_objects(pstatic_stored_object o) 
const;
 
  331     bool exists_stored_object(pstatic_stored_object o) 
const;
 
  333     void add_stored_object(pstatic_stored_object_key k, pstatic_stored_object o,
 
  336     iterator iterator_of_object_(pstatic_stored_object o);
 
  340     bool del_dependency_(pstatic_stored_object o1,
 
  341     pstatic_stored_object o2);
 
  345     bool del_dependent_(pstatic_stored_object o1,
 
  346     pstatic_stored_object o2);
 
  350     bool add_dependency_(pstatic_stored_object o1,
 
  351     pstatic_stored_object o2);
 
  355     bool add_dependent_(pstatic_stored_object o1,
 
  356     pstatic_stored_object o2);
 
  357     void basic_delete_(std::list<pstatic_stored_object> &to_delete);
 
  359     getfem::lock_factory locks_;
 
  360     stored_key_tab stored_keys_;
 
  366   template<
typename OBJECT_TYPE>
 
  368     std::list<pstatic_stored_object> delete_object_list;
 
  371       for(
auto &&pair : stored_objects){
 
  372         auto p_object = std::dynamic_pointer_cast<const OBJECT_TYPE>(pair.second.p);
 
  373         if(p_object != 
nullptr) delete_object_list.push_back(pair.second.p);
 
  379       filter_objects(stored_objects);
 
  382       for(
size_t thread = 0; thread < singleton<stored_object_tab>::num_threads(); ++thread)
 
  385         filter_objects(stored_objects);
 
static T & instance()
Instance from the current thread.
base class for static stored objects
defines and typedefs for namespace dal
A simple singleton implementation.
Tools for multithreaded, OpenMP and Boost based parallelization.
void del_stored_objects(std::list< pstatic_stored_object > &to_delete, bool ignore_unstored)
Delete a list of objects and their dependencies.
void add_stored_object(pstatic_stored_object_key k, pstatic_stored_object o, permanence perm)
Add an object with two optional dependencies.
void test_stored_objects(void)
Test the validity of the whole global storage.
void add_dependency(pstatic_stored_object o1, pstatic_stored_object o2)
Add a dependency, object o1 will depend on object o2.
bool del_dependency(pstatic_stored_object o1, pstatic_stored_object o2)
remove a dependency.
size_t nb_stored_objects(void)
Return the number of stored objects (for debugging purpose).
void list_stored_objects(std::ostream &ost)
Show a list of stored objects (for debugging purpose).
void del_stored_object(const pstatic_stored_object &o, bool ignore_unstored)
Delete an object and the object which depend on it.
pstatic_stored_object search_stored_object(pstatic_stored_object_key k)
Gives a pointer to an object from a key pointer.
bool exists_stored_object(pstatic_stored_object o)
Test if an object is stored.
void delete_specific_type_stored_objects(bool all_threads=false)
delete all the specific type of stored objects
Pointer to a key with a coherent order.
Pointer to an object with the dependencies.
stored_object_tab()
STATIC_STORED_TAB ----------------------------------------------------—.