Skip to content

Commit

Permalink
Рефакторинг refalrts-dynamic (#266)
Browse files Browse the repository at this point in the history
На первый взгляд получилось более громоздко, но на самом деле рефакторинг
упрощает последующие коммиты.
  • Loading branch information
Mazdaywik committed Feb 5, 2020
1 parent 7e42789 commit ffc4cec
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 23 deletions.
44 changes: 23 additions & 21 deletions src/lib/refalrts-dynamic.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,10 @@ bool refalrts::Module::has_alias(const std::string& alias) const {
return std::find(m_aliases.begin(), m_aliases.end(), alias) != m_aliases.end();
}

bool refalrts::Module::with_stat(const refalrts::api::stat *stat) const {
return api::stat_compare(m_stat, stat) == 0;
}

inline void refalrts::Module::del_ref() {
assert(m_refcounter > 0);
--m_refcounter;
Expand Down Expand Up @@ -733,20 +737,24 @@ refalrts::Domain::ModuleStorage::~ModuleStorage() {
}
}

refalrts::Module *
refalrts::Domain::ModuleStorage::operator[](
const refalrts::api::stat *stat
template <typename Attr>
refalrts::Module *refalrts::Domain::ModuleStorage::find_by_attr(
Attr attr, bool (refalrts::Module::*attr_checker)(Attr attr) const
) const {
ModuleList::const_iterator p = m_modules.begin();
while (p != m_modules.end() && api::stat_compare((*p)->stat(), stat) != 0) {

while (p != m_modules.end() && ! ((*p)->*attr_checker)(attr)) {
++p;
}

if (p != m_modules.end()) {
return *p;
} else {
return 0;
}
return p != m_modules.end() ? *p : 0;
}

refalrts::Module *
refalrts::Domain::ModuleStorage::find(
const refalrts::api::stat *stat
) const {
return find_by_attr(stat, &Module::with_stat);
}

refalrts::RefalFunction *
Expand All @@ -764,24 +772,18 @@ refalrts::Domain::ModuleStorage::operator[](
}

refalrts::Module *
refalrts::Domain::ModuleStorage::find_by_alias(const std::string& name) const {
ModuleList::const_iterator p = m_modules.begin();

while (p != m_modules.end() && ! (*p)->has_alias(name)) {
++p;
}

return p != m_modules.end() ? *p : 0;
refalrts::Domain::ModuleStorage::find(const std::string& alias) const {
return find_by_attr<const std::string&>(alias, &Module::has_alias);
}

refalrts::Module *refalrts::Domain::ModuleStorage::load_module(
const std::string& name, refalrts::Domain::Stack *stack,
LoadModuleEvent event, void *callback_data,
refalrts::NativeModule *main_module
) {
Module *module = m_domain->m_storage.find_by_alias(name);
Module *module = m_domain->m_storage.find(name);
if (! module) {
module = find_by_alias(name);
module = find(name);
}

if (module) {
Expand Down Expand Up @@ -910,8 +912,8 @@ refalrts::Domain::ModuleStorage::find_known(
) const {
Module *result;
return
(result = m_domain->m_storage[stat], result != 0) ? result :
(result = (*this)[stat], result != 0) ? result :
(result = m_domain->m_storage.find(stat), result != 0) ? result :
(result = find(stat), result != 0) ? result :
stack != 0 && (result = stack->contain(stat), result != 0) ? result :
0;
}
Expand Down
10 changes: 8 additions & 2 deletions src/lib/refalrts-dynamic.h
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,7 @@ class Module {
}

bool has_alias(const std::string& alias) const;
bool with_stat(const api::stat *stat) const;

ModuleRepresentant *representant() const {
return m_representant;
Expand Down Expand Up @@ -263,10 +264,15 @@ class Domain {
ModuleStorage(Domain *domain);
~ModuleStorage();

Module *operator[](const api::stat *stat) const;
template <typename Attr>
Module *find_by_attr(
Attr attr, bool (Module::*attr_checker)(Attr attr) const
) const;

Module *find(const api::stat *stat) const;
RefalFunction *operator[](const RefalFuncName& name) const;

Module *find_by_alias(const std::string& name) const;
Module *find(const std::string& alias) const;

Module *load_module(
const std::string& name, Stack *stack,
Expand Down

0 comments on commit ffc4cec

Please sign in to comment.