32 #ifndef DAL_NAMING_SYSTEM_H 33 #define DAL_NAMING_SYSTEM_H 59 typedef std::shared_ptr<const METHOD> pmethod;
66 pmethod method(
void)
const {
return pm_; }
67 double num(
void)
const {
return num_; }
68 int type(
void)
const {
return type_; }
69 parameter(
double e) : type_(0), num_(e), pm_(0) {}
70 parameter(pmethod p) : type_(1), num_(0.), pm_(p) {}
73 typedef std::deque<parameter> param_list;
74 typedef pmethod (* pfunction)(param_list &,
75 std::vector<pstatic_stored_object> &);
76 typedef pmethod (* pgenfunction)(std::string,
77 std::vector<pstatic_stored_object> &);
78 typedef size_t size_type;
83 std::map<std::string, size_type> suffixes;
84 std::vector<pfunction> functions;
85 std::vector<pgenfunction> genfunctions;
86 std::map<std::string, std::string> shorter_names;
87 std::map<std::string, std::string> aliases;
89 struct method_key :
virtual public static_stored_object_key {
92 virtual bool compare(
const static_stored_object_key &oo)
const {
93 const method_key &o =
dynamic_cast<const method_key &
>(oo);
94 if (name < o.name)
return true;
else return false;
96 method_key(
const std::string &name_) : name(name_) {}
99 int mns_lexem(
const std::string &s, size_type i, size_type &lenght);
100 pmethod method_(
const std::string &name, size_type &i,
bool throw_if_not_found);
107 void add_suffix(std::string name, pfunction pf);
108 void add_generic_function(pgenfunction pf);
109 std::string normative_name_of_method(pmethod pm)
const;
110 std::string shorter_name_of_method(pmethod pm)
const;
111 pmethod method(
const std::string &name, size_type &i,
112 bool throw_if_not_found =
true)
118 template <
class METHOD>
121 std::string tname = prefix +
'_' + name;
122 if (suffixes.find(tname) != suffixes.end()) {
123 functions[suffixes[tname]] = pf;
125 suffixes[tname] = functions.size();
126 functions.push_back(pf);
131 template <
class METHOD>
133 genfunctions.push_back(pf);
136 template <
class METHOD>
138 naming_system<METHOD>::pmethod pm)
const {
139 pstatic_stored_object_key k = key_of_stored_object(pm);
141 if (!k || !(p = dynamic_cast<const method_key *>(k.get())))
142 return prefix +
"_UNKNOWN";
146 template <
class METHOD> std::string
148 naming_system<METHOD>::pmethod pm)
const {
149 pstatic_stored_object_key k = key_of_stored_object(pm);
151 if (!k || !(p = dynamic_cast<const method_key *>(k.get())))
152 return prefix +
"_UNKNOWN";
153 const std::string &name(p->name);
154 std::map<std::string, std::string>::const_iterator
155 it = shorter_names.find(name);
156 if (it != shorter_names.end())
return it->second;
168 template <
class METHOD>
172 if (i >= s.size())
return 0;
174 if (isspace(c))
return 1;
175 if (isalpha(c) || c ==
'_') {
176 while (i < s.size() && (isalpha(s[i]) || s[i] ==
'_' || isdigit(s[i])))
180 if (isdigit(c) || c ==
'-' || c ==
'+') {
181 while (i < s.size() && (isdigit(s[i]) || s[i] ==
'e' || s[i] ==
'E' ||
182 s[i] ==
'.' || s[i] ==
'-' || s[i] ==
'+'))
186 if (c ==
'(')
return 4;
187 if (c ==
')')
return 5;
188 if (c ==
',')
return 6;
189 GMM_ASSERT1(
false,
"Invalid character on position " << i
190 <<
" of the string : " << s);
194 template <
class METHOD>
195 typename naming_system<METHOD>::pmethod
197 bool throw_if_not_found) {
202 size_type ind_suff = size_type(-1);
208 int lex = mns_lexem(name, i, l);
212 case 1 : i += l;
break;
214 suff = name.substr(i, l);
215 if (suffixes.find(suff) != suffixes.end())
216 ind_suff = suffixes[suff];
217 state = 1; i += l;
break;
218 default : error =
true;
223 case 4 : state = 2; i += l;
break;
224 default : isend =
true;
break;
229 case 1 : i += l;
break;
231 pm = method_(name, i, throw_if_not_found);
232 if (!(pm.get()))
return pm;
233 params.push_back(parameter(pm));
238 params.push_back(parameter(strtod(&(name[i]), &p)));
239 i += l;
if (p < &(name[i])) error =
true;
242 case 5 : i += l; isend =
true;
break;
243 default : error =
true;
248 case 1 : i += l;
break;
249 case 5 : i += l; isend =
true;
break;
250 case 6 : i += l; state = 2;
break;
251 default : error =
true;
255 GMM_ASSERT1(!error,
"Syntax error on position " << i
256 <<
" of the string : " << name);
258 std::stringstream norm_name(suff);
261 if (params.size() > 0) {
263 typename param_list::const_iterator it = params.begin(),
265 for (; it != ite; ++it) {
266 if ((*it).type() == 0) norm_name << (*it).num();
267 if ((*it).type() == 1)
268 norm_name << normative_name_of_method((*it).method());
269 if (it+1 != ite) norm_name <<
',';
273 auto pnname = std::make_shared<method_key>(norm_name.str());
275 if (aliases.find(norm_name.str()) != aliases.end())
276 pnname->name = aliases[norm_name.str()];
278 if (o)
return std::dynamic_pointer_cast<
const METHOD>(o);
280 std::vector<pstatic_stored_object> dependencies;
281 for (size_type k = 0; k < genfunctions.size() && pm.get() == 0; ++k) {
282 pm = (*(genfunctions[k]))(pnname->name, dependencies);
285 if (ind_suff == size_type(-1)) {
286 GMM_ASSERT1(!throw_if_not_found,
"Unknown method: "<<pnname->name);
289 pm = (*(functions[ind_suff]))(params, dependencies);
291 pstatic_stored_object_key k = key_of_stored_object(pm);
294 dal::PERMANENT_STATIC_OBJECT);
295 for (size_type j = 0; j < dependencies.size(); ++j)
299 std::string normname((dynamic_cast<const method_key *>(k.get()))->name);
300 aliases[pnname->name] = normname;
301 if (pnname->name.size() < normname.size()) {
302 if (shorter_names.find(normname) != shorter_names.end()) {
303 if (pnname->name.size() < shorter_names[normname].size())
304 shorter_names[normname] = pnname->name;
306 else shorter_names[normname] = pnname->name;
316 template <
class METHOD>
320 pstatic_stored_object_key pnname = std::make_shared<method_key>(name);
322 if (!o)
return false;
323 pm = std::dynamic_pointer_cast<
const METHOD>(o);
324 pstatic_stored_object_key k = key_of_stored_object(pm);
Stores interdependent getfem objects.
bool delete_method(std::string name)
deletion of static_stored_object in the naming system
void del_stored_object(const pstatic_stored_object &o, bool ignore_unstored)
Delete an object and the object which depend on it.
Tools for multithreaded, OpenMP and Boost based parallelization.
pstatic_stored_object search_stored_object(pstatic_stored_object_key k)
Gives a pointer to an object from a key pointer.
Associate a name to a method descriptor and store method descriptors.
void add_stored_object(pstatic_stored_object_key k, pstatic_stored_object o, permanence perm)
Add an object with two optional dependencies.
this is the above solutions for linux, but it still needs to be tested.
void add_dependency(pstatic_stored_object o1, pstatic_stored_object o2)
Add a dependency, object o1 will depend on object o2.