The nightly WTF

Submitted by 0xd34df00d on Sat, 02/16/2008 - 00:04

Начинаю публиковать избранные особо непонятные куски кода своего LeechCraft. Сегодня в роли жертвы - код, вычисляющий зависимости между плагинами.

  1. void PluginManager::CalculateDependencies ()
  2. {
  3. for (PluginsContainer_t::const_iterator i = Plugins_.begin (); i != Plugins_.end (); ++i)
  4. {
  5. QObject *pEntity = (*i)->instance ();
  6. IInfo *info = qobject_cast<IInfo*> (pEntity);
  7. QStringList needs = info->Needs (),
  8. uses = info->Uses ();
  9. DependenciesMet_ [i] = true;
  10.  
  11. if (!needs.isEmpty ())
  12. {
  13. for (PluginsContainer_t::const_iterator j = Plugins_.begin (); j != Plugins_.end (); ++j)
  14. {
  15. if (j == i)
  16. continue;
  17.  
  18. QObject *qpEntity = (*j)->instance ();
  19. IInfo *qinfo = qobject_cast<IInfo*> (qpEntity);
  20. QStringList qprovides = qinfo->Provides ();
  21. for (int k = 0; k < needs.size (); ++k)
  22. if (qprovides.contains (needs [i]))
  23. {
  24. info->SetProvider (qpEntity, needs [k]);
  25. needs.removeAt (k--);
  26. }
  27. }
  28. if (!needs.isEmpty ())
  29. {
  30. DependenciesMet_ [i] = false;
  31. FailedDependencies_ [i] = needs;
  32. qWarning () << Q_FUNC_INFO << "not all plugins providing needs of" << info->GetName () << "are found. The remaining ones are:" << needs;
  33. }
  34. }
  35. if (!uses.isEmpty ())
  36. {
  37. QMap<QString, bool> usesMet;
  38. for (int j = 0; j < uses.size (); ++j)
  39. usesMet [uses.at (j)] = false;
  40.  
  41. for (PluginsContainer_t::const_iterator j = Plugins_.begin (); j != Plugins_.end (); ++j)
  42. {
  43. if (j == i)
  44. continue;
  45.  
  46. QObject *qpEntity = (*j)->instance ();
  47. IInfo *qinfo = qobject_cast<IInfo*> (qpEntity);
  48. QStringList qprovides = qinfo->Provides ();
  49. for (int k = 0; k < uses.size (); ++k)
  50. if (qprovides.contains (uses [i]))
  51. {
  52. info->SetProvider (qpEntity, uses [k]);
  53. usesMet [uses [i]] = true;
  54. }
  55. }
  56. }
  57. }
  58. }