Rheolef  7.1
an efficient C++ finite element environment
persistent_table.h
Go to the documentation of this file.
1 #ifndef _RHEOLEF_PERSISTENT_TABLE_H
2 #define _RHEOLEF_PERSISTENT_TABLE_H
3 //
4 // This file is part of Rheolef.
5 //
6 // Copyright (C) 2000-2009 Pierre Saramito <Pierre.Saramito@imag.fr>
7 //
8 // Rheolef is free software; you can redistribute it and/or modify
9 // it under the terms of the GNU General Public License as published by
10 // the Free Software Foundation; either version 2 of the License, or
11 // (at your option) any later version.
12 //
13 // Rheolef is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 // GNU General Public License for more details.
17 //
18 // You should have received a copy of the GNU General Public License
19 // along with Rheolef; if not, write to the Free Software
20 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 //
22 // =========================================================================
23 // author: Pierre.Saramito@imag.fr
24 // date: 5 february 2019
25 
26 namespace rheolef {
75 } // namespace rheolef
76 
77 #include "rheolef/smart_pointer.h"
78 #include <unordered_map>
79 #include <string>
80 namespace rheolef {
81 
82 // [verbatim_persistent_table]
83 template<class A>
85  public:
86  static A load (const std::string& name);
87  static void unload (const std::string& name);
88  static bool verbose () { return _verbose; }
89  static void set_verbose (bool v) { _verbose = v; }
90  protected:
91  using loaded_map_type = std::unordered_map<std::string,void*>;
93  // data:
95  static bool _verbose;
96 };
97 // [verbatim_persistent_table]
98 
99 template<class A>
100 A
101 persistent_table<A>::load (const std::string& name) {
102  using base = typename A::base;
103  using rep = typename base::handled_type;
104  auto iter = get_loaded_map().find (name);
105  if (iter != get_loaded_map().end()) {
106  verbose() && std::cerr << "persistent_table: \""<<name<<"\" reused from table" << std::endl;
107  A a;
108  a.base::operator= (base((*iter).second,typename base::internal()));
109  return a;
110  }
111  verbose() && std::cerr << "persistent_table: \""<<name<<"\" created" << std::endl;
112  rep* ptr = rep::make_ptr (name);
113  A a;
114  a.base::operator= (ptr);
115  get_loaded_map().insert (std::make_pair(name, a.base::get_count()));
116  return a;
117 }
118 
119 template <class A>
120 void
121 persistent_table<A>::unload (const std::string& name) {
122  size_t status_erased = get_loaded_map().erase (name);
123  verbose() && std::cerr << "persistent_table: \""<<name<<"\" destroyed and erased from table" << std::endl;
124 }
125 // static declaration:
126 template<class A>
129 
130 template<class A>
131 bool
133 
134 } // namespace rheolef
135 
137 //
138 // example of persistent table
139 //
140 // author: Pierre.Saramito@imag.fr
141 //
142 // date: 5 february 2019
143 //
145 #ifdef _RHEOLEF_PERSISTENT_TABLE_EXAMPLE
146 //<verbatim:
147 #include "rheolef/persistent_table.h"
148 using namespace rheolef;
149 using namespace std;
150 
151 // [verbatim_persistent_table_tst]
152 struct A_rep {
153  public:
154  A_rep (const string& name1) : _name(name1) { /* long init */ }
155  ~A_rep();
156  string name() const { return _name; }
157  static A_rep* make_ptr (const string& name) { return new A_rep (name); }
158  // data:
159  protected:
160  string _name;
161 };
162 struct A : public smart_pointer_nocopy<A_rep>, public persistent_table<A> {
163  public:
164  using rep = A_rep;
165  using base = smart_pointer_nocopy<rep>;
166  A (const string& name = "");
167  string name() const { return base::data().name(); }
168 };
169 // implementation of members:
170 A::A (const string& name)
171  : base(),
172  persistent_table<A>()
173 {
174  if (name == "") return;
175  base::operator= (persistent_table<A>::load (name));
176 }
177 A_rep::~A_rep()
178 {
180 }
181 int main() {
182  persistent_table<A>::set_verbose (true); // trace table load/unload
183  A a("a"); // "a" created
184  {
185  A b("b"); // "b" created
186  A c("a"); // "a" reused from table
187  } // "b" destroyed and erased from table
188  {
189  A b("b"); // "b" created
190  A c("a"); // "a" reused from table
191  } // "b" destroyed and erased from table
192 } // "a" destroyed and erased from table
193 // [verbatim_persistent_table_tst]
194 
195 #endif // _RHEOLEF_PERSISTENT_TABLE_EXAMPLE
196 #endif // _RHEOLEF_PERSISTENT_TABLE_H
int main(int argc, char **argv)
Definition: csr.cc:270
see the persistent_table page for the full documentation
static void unload(const std::string &name)
static void set_verbose(bool v)
static loaded_map_type _loaded_map
std::unordered_map< std::string, void * > loaded_map_type
static loaded_map_type & get_loaded_map()
static A load(const std::string &name)
This file is part of Rheolef.