32 #ifndef DAL_NAMING_SYSTEM_H
33 #define DAL_NAMING_SYSTEM_H
60 typedef std::shared_ptr<const METHOD> pmethod;
67 pmethod method(
void)
const {
return pm_; }
68 double num(
void)
const {
return num_; }
69 int type(
void)
const {
return type_; }
70 parameter(
double e) : type_(0), num_(e), pm_(0) {}
71 parameter(pmethod p) : type_(1), num_(0.), pm_(p) {}
74 typedef std::deque<parameter> param_list;
75 typedef pmethod (* pfunction)(param_list &,
76 std::vector<pstatic_stored_object> &);
77 typedef pmethod (* pgenfunction)(std::string,
78 std::vector<pstatic_stored_object> &);
79 typedef size_t size_type;
84 std::map<std::string, size_type> suffixes;
85 std::vector<pfunction> functions;
86 std::vector<pgenfunction> genfunctions;
87 std::map<std::string, std::string> shorter_names;
88 std::map<std::string, std::string> aliases;
90 struct method_key :
virtual public static_stored_object_key {
93 bool compare(
const static_stored_object_key &oo)
const override{
94 auto &o =
dynamic_cast<const method_key &
>(oo);
98 bool equal(
const static_stored_object_key &oo)
const override{
99 auto &o =
dynamic_cast<const method_key &
>(oo);
100 return name == o.name;
103 method_key(
const std::string &name_) : name(name_) {}
106 int mns_lexem(
const std::string &s, size_type i, size_type &lenght);
107 pmethod method_(
const std::string &name, size_type &i,
bool throw_if_not_found);
114 void add_suffix(std::string name, pfunction pf);
115 void add_generic_function(pgenfunction pf);
116 std::string normative_name_of_method(pmethod pm)
const;
117 std::string shorter_name_of_method(pmethod pm)
const;
118 pmethod method(
const std::string &name, size_type &i,
119 bool throw_if_not_found =
true)
125 template <
class METHOD>
128 std::string tname = prefix +
'_' + name;
129 if (suffixes.find(tname) != suffixes.end()) {
130 functions[suffixes[tname]] = pf;
132 suffixes[tname] = functions.size();
133 functions.push_back(pf);
138 template <
class METHOD>
139 void naming_system<METHOD>::add_generic_function(pgenfunction pf) {
140 genfunctions.push_back(pf);
143 template <
class METHOD>
144 std::string naming_system<METHOD>::normative_name_of_method(
typename
145 naming_system<METHOD>::pmethod pm)
const {
146 pstatic_stored_object_key k = key_of_stored_object(pm);
148 if (!k || !(p =
dynamic_cast<const method_key *
>(k.get())))
149 return prefix +
"_UNKNOWN";
153 template <
class METHOD> std::string
154 naming_system<METHOD>::shorter_name_of_method(
typename
155 naming_system<METHOD>::pmethod pm)
const {
156 pstatic_stored_object_key k = key_of_stored_object(pm);
158 if (!k || !(p =
dynamic_cast<const method_key *
>(k.get())))
159 return prefix +
"_UNKNOWN";
160 const std::string &name(p->name);
161 std::map<std::string, std::string>::const_iterator
162 it = shorter_names.find(name);
163 if (it != shorter_names.end())
return it->second;
175 template <
class METHOD>
176 int naming_system<METHOD>::mns_lexem(
const std::string &s,
size_type i,
179 if (i >= s.size())
return 0;
181 if (isspace(c))
return 1;
182 if (isalpha(c) || c ==
'_') {
183 while (i < s.size() && (isalpha(s[i]) || s[i] ==
'_' || isdigit(s[i])))
187 if (isdigit(c) || c ==
'-' || c ==
'+') {
188 while (i < s.size() && (isdigit(s[i]) || s[i] ==
'e' || s[i] ==
'E' ||
189 s[i] ==
'.' || s[i] ==
'-' || s[i] ==
'+'))
193 if (c ==
'(')
return 4;
194 if (c ==
')')
return 5;
195 if (c ==
',')
return 6;
196 GMM_ASSERT1(
false,
"Invalid character on position " << i
197 <<
" of the string : " << s);
201 template <
class METHOD>
202 typename naming_system<METHOD>::pmethod
203 naming_system<METHOD>::method_(
const std::string &name,
size_type &i,
204 bool throw_if_not_found) {
215 int lex = mns_lexem(name, i, l);
219 case 1 : i += l;
break;
221 suff = name.substr(i, l);
222 if (suffixes.find(suff) != suffixes.end())
223 ind_suff = suffixes[suff];
224 state = 1; i += l;
break;
225 default : error =
true;
230 case 4 : state = 2; i += l;
break;
231 default : isend =
true;
break;
236 case 1 : i += l;
break;
238 pm = method_(name, i, throw_if_not_found);
239 if (!(pm.get()))
return pm;
240 params.push_back(parameter(pm));
245 params.push_back(parameter(strtod(&(name[i]), &p)));
246 i += l;
if (p < &(name[i])) error =
true;
249 case 5 : i += l; isend =
true;
break;
250 default : error =
true;
255 case 1 : i += l;
break;
256 case 5 : i += l; isend =
true;
break;
257 case 6 : i += l; state = 2;
break;
258 default : error =
true;
262 GMM_ASSERT1(!error,
"Syntax error on position " << i
263 <<
" of the string : " << name);
265 std::stringstream norm_name(suff);
268 if (params.size() > 0) {
270 typename param_list::const_iterator it = params.begin(),
272 for (; it != ite; ++it) {
273 if ((*it).type() == 0) norm_name << (*it).num();
274 if ((*it).type() == 1)
275 norm_name << normative_name_of_method((*it).method());
276 if (it+1 != ite) norm_name <<
',';
280 auto pnname = std::make_shared<method_key>(norm_name.str());
282 if (aliases.find(norm_name.str()) != aliases.end())
283 pnname->name = aliases[norm_name.str()];
285 if (o)
return std::dynamic_pointer_cast<const METHOD>(o);
287 std::vector<pstatic_stored_object> dependencies;
288 for (
size_type k = 0; k < genfunctions.size() && pm.get() == 0; ++k) {
289 pm = (*(genfunctions[k]))(pnname->name, dependencies);
293 GMM_ASSERT1(!throw_if_not_found,
"Unknown method: "<<pnname->name);
296 pm = (*(functions[ind_suff]))(params, dependencies);
298 pstatic_stored_object_key k = key_of_stored_object(pm);
301 dal::PERMANENT_STATIC_OBJECT);
302 for (
size_type j = 0; j < dependencies.size(); ++j)
306 std::string normname((
dynamic_cast<const method_key *
>(k.get()))->name);
307 aliases[pnname->name] = normname;
308 if (pnname->name.size() < normname.size()) {
309 if (shorter_names.find(normname) != shorter_names.end()) {
310 if (pnname->name.size() < shorter_names[normname].size())
311 shorter_names[normname] = pnname->name;
313 else shorter_names[normname] = pnname->name;
323 template <
class METHOD>
327 pstatic_stored_object_key pnname = std::make_shared<method_key>(name);
329 if (!o)
return false;
330 pm = std::dynamic_pointer_cast<const METHOD>(o);
331 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.