31 #ifndef DAL_NAMING_SYSTEM_H
32 #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 bool compare(
const static_stored_object_key &oo)
const override{
93 auto &o =
dynamic_cast<const method_key &
>(oo);
97 bool equal(
const static_stored_object_key &oo)
const override{
98 auto &o =
dynamic_cast<const method_key &
>(oo);
99 return name == o.name;
102 method_key(
const std::string &name_) : name(name_) {}
105 int mns_lexem(
const std::string &s, size_type i, size_type &lenght);
106 pmethod method_(
const std::string &name, size_type &i,
bool throw_if_not_found);
113 void add_suffix(std::string name, pfunction pf);
114 void add_generic_function(pgenfunction pf);
115 std::string normative_name_of_method(pmethod pm)
const;
116 std::string shorter_name_of_method(pmethod pm)
const;
117 pmethod method(
const std::string &name, size_type &i,
118 bool throw_if_not_found =
true)
124 template <
class METHOD>
127 std::string tname = prefix +
'_' + name;
128 if (suffixes.find(tname) != suffixes.end()) {
129 functions[suffixes[tname]] = pf;
131 suffixes[tname] = functions.size();
132 functions.push_back(pf);
137 template <
class METHOD>
138 void naming_system<METHOD>::add_generic_function(pgenfunction pf) {
139 genfunctions.push_back(pf);
142 template <
class METHOD>
143 std::string naming_system<METHOD>::normative_name_of_method(
typename
144 naming_system<METHOD>::pmethod pm)
const {
145 pstatic_stored_object_key k = key_of_stored_object(pm);
147 if (!k || !(p =
dynamic_cast<const method_key *
>(k.get())))
148 return prefix +
"_UNKNOWN";
152 template <
class METHOD> std::string
153 naming_system<METHOD>::shorter_name_of_method(
typename
154 naming_system<METHOD>::pmethod pm)
const {
155 pstatic_stored_object_key k = key_of_stored_object(pm);
157 if (!k || !(p =
dynamic_cast<const method_key *
>(k.get())))
158 return prefix +
"_UNKNOWN";
159 const std::string &name(p->name);
160 std::map<std::string, std::string>::const_iterator
161 it = shorter_names.find(name);
162 if (it != shorter_names.end())
return it->second;
174 template <
class METHOD>
175 int naming_system<METHOD>::mns_lexem(
const std::string &s,
size_type i,
178 if (i >= s.size())
return 0;
180 if (isspace(c))
return 1;
181 if (isalpha(c) || c ==
'_') {
182 while (i < s.size() && (isalpha(s[i]) || s[i] ==
'_' || isdigit(s[i])))
186 if (isdigit(c) || c ==
'-' || c ==
'+') {
187 while (i < s.size() && (isdigit(s[i]) || s[i] ==
'e' || s[i] ==
'E' ||
188 s[i] ==
'.' || s[i] ==
'-' || s[i] ==
'+'))
192 if (c ==
'(')
return 4;
193 if (c ==
')')
return 5;
194 if (c ==
',')
return 6;
195 GMM_ASSERT1(
false,
"Invalid character on position " << i
196 <<
" of the string : " << s);
200 template <
class METHOD>
201 typename naming_system<METHOD>::pmethod
202 naming_system<METHOD>::method_(
const std::string &name,
size_type &i,
203 bool throw_if_not_found) {
214 int lex = mns_lexem(name, i, l);
218 case 1 : i += l;
break;
220 suff = name.substr(i, l);
221 if (suffixes.find(suff) != suffixes.end())
222 ind_suff = suffixes[suff];
223 state = 1; i += l;
break;
224 default : error =
true;
229 case 4 : state = 2; i += l;
break;
230 default : isend =
true;
break;
235 case 1 : i += l;
break;
237 pm = method_(name, i, throw_if_not_found);
238 if (!(pm.get()))
return pm;
239 params.push_back(parameter(pm));
244 params.push_back(parameter(strtod(&(name[i]), &p)));
245 i += l;
if (p < &(name[i])) error =
true;
248 case 5 : i += l; isend =
true;
break;
249 default : error =
true;
254 case 1 : i += l;
break;
255 case 5 : i += l; isend =
true;
break;
256 case 6 : i += l; state = 2;
break;
257 default : error =
true;
261 GMM_ASSERT1(!error,
"Syntax error on position " << i
262 <<
" of the string : " << name);
264 std::stringstream norm_name(suff);
267 if (params.size() > 0) {
269 typename param_list::const_iterator it = params.begin(),
271 for (; it != ite; ++it) {
272 if ((*it).type() == 0) norm_name << (*it).num();
273 if ((*it).type() == 1)
274 norm_name << normative_name_of_method((*it).method());
275 if (it+1 != ite) norm_name <<
',';
279 auto pnname = std::make_shared<method_key>(norm_name.str());
281 if (aliases.find(norm_name.str()) != aliases.end())
282 pnname->name = aliases[norm_name.str()];
284 if (o)
return std::dynamic_pointer_cast<const METHOD>(o);
286 std::vector<pstatic_stored_object> dependencies;
287 for (
size_type k = 0; k < genfunctions.size() && pm.get() == 0; ++k) {
288 pm = (*(genfunctions[k]))(pnname->name, dependencies);
292 GMM_ASSERT1(!throw_if_not_found,
"Unknown method: "<<pnname->name);
295 pm = (*(functions[ind_suff]))(params, dependencies);
297 pstatic_stored_object_key k = key_of_stored_object(pm);
300 dal::PERMANENT_STATIC_OBJECT);
301 for (
size_type j = 0; j < dependencies.size(); ++j)
305 std::string normname((
dynamic_cast<const method_key *
>(k.get()))->name);
306 aliases[pnname->name] = normname;
307 if (pnname->name.size() < normname.size()) {
308 if (shorter_names.find(normname) != shorter_names.end()) {
309 if (pnname->name.size() < shorter_names[normname].size())
310 shorter_names[normname] = pnname->name;
312 else shorter_names[normname] = pnname->name;
322 template <
class METHOD>
326 pstatic_stored_object_key pnname = std::make_shared<method_key>(name);
328 if (!o)
return false;
329 pm = std::dynamic_pointer_cast<const METHOD>(o);
330 pstatic_stored_object_key k = key_of_stored_object(pm);
Associate a name to a method descriptor and store method descriptors.
bool delete_method(std::string name)
deletion of static_stored_object in the naming system
Identical to gmm::standard_locale, but does not change std::locale in multi-threaded sections of the ...
Stores interdependent getfem objects.
thread safe standard locale with RAII semantics
Tools for multithreaded, OpenMP and Boost based parallelization.
size_t size_type
used as the common size type in the library
void add_stored_object(pstatic_stored_object_key k, pstatic_stored_object o, permanence perm)
Add an object with two optional dependencies.
void add_dependency(pstatic_stored_object o1, pstatic_stored_object o2)
Add a dependency, object o1 will depend on object o2.
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.