LP-92 Remove Feed Forward : flight / ground
[librepilot.git] / ground / gcs / src / libs / extensionsystem / pluginmanager.cpp
blobc41a6a5ffa7fabeb01f66e25df81e32ea850e216
1 /**
2 ******************************************************************************
4 * @file pluginmanager.cpp
5 * @author The OpenPilot Team, http://www.openpilot.org Copyright (C) 2010.
6 * Parts by Nokia Corporation (qt-info@nokia.com) Copyright (C) 2009.
7 * @brief
8 * @see The GNU Public License (GPL) Version 3
9 * @defgroup
10 * @{
12 *****************************************************************************/
14 * This program is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation; either version 3 of the License, or
17 * (at your option) any later version.
19 * This program is distributed in the hope that it will be useful, but
20 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
21 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
22 * for more details.
24 * You should have received a copy of the GNU General Public License along
25 * with this program; if not, write to the Free Software Foundation, Inc.,
26 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
29 #include "pluginmanager.h"
30 #include "pluginmanager_p.h"
31 #include "pluginspec.h"
32 #include "pluginspec_p.h"
33 #include "optionsparser.h"
34 #include "iplugin.h"
36 #include <QtCore/QMetaProperty>
37 #include <QtCore/QDir>
38 #include <QtCore/QTextStream>
39 #include <QtCore/QWriteLocker>
40 #include <QtDebug>
41 #ifdef WITH_TESTS
42 #include <QTest>
43 #endif
45 typedef QList<ExtensionSystem::PluginSpec *> PluginSpecSet;
47 enum { debugLeaks = 0 };
49 /*!
50 \namespace ExtensionSystem
51 \brief The ExtensionSystem namespace provides classes that belong to the core plugin system.
53 The basic extension system contains of the plugin manager and its supporting classes,
54 and the IPlugin interface that must be implemented by plugin providers.
57 /*!
58 \namespace ExtensionSystem::Internal
59 \internal
62 /*!
63 \class ExtensionSystem::PluginManager
64 \mainclass
66 \brief Core plugin system that manages the plugins, their life cycle and their registered objects.
68 The plugin manager is used for the following tasks:
69 \list
70 \o Manage plugins and their state
71 \o Manipulate a 'common object pool'
72 \endlist
74 \section1 Plugins
75 Plugins consist of an xml descriptor file, and of a library that contains a Qt plugin
76 (declared via Q_EXPORT_PLUGIN) that must derive from the IPlugin class.
77 The plugin manager is used to set a list of file system directories to search for
78 plugins, retrieve information about the state of these plugins, and to load them.
80 Usually the application creates a PluginManager instance and initiates the loading.
81 \code
82 ExtensionSystem::PluginManager *manager = new ExtensionSystem::PluginManager();
83 manager->setPluginPaths(QStringList() << "plugins"); // 'plugins' and subdirs will be searched for plugins
84 manager->loadPlugins(); // try to load all the plugins
85 \endcode
86 Additionally it is possible to directly access to the plugin specifications
87 (the information in the descriptor file), and the plugin instances (via PluginSpec),
88 and their state.
90 \section1 Object Pool
91 Plugins (and everybody else) can add objects to a common 'pool' that is located in
92 the plugin manager. Objects in the pool must derive from QObject, there are no other
93 prerequisites. All objects of a specified type can be retrieved from the object pool
94 via the getObjects() and getObject() methods. They are aware of Aggregation::Aggregate, i.e.
95 these methods use the Aggregation::query methods instead of a qobject_cast to determine
96 the matching objects.
98 Whenever the state of the object pool changes a corresponding signal is emitted by the plugin manager.
100 A common usecase for the object pool is that a plugin (or the application) provides
101 an "extension point" for other plugins, which is a class / interface that can
102 be implemented and added to the object pool. The plugin that provides the
103 extension point looks for implementations of the class / interface in the object pool.
104 \code
105 // plugin A provides a "MimeTypeHandler" extension point
106 // in plugin B:
107 MyMimeTypeHandler *handler = new MyMimeTypeHandler();
108 ExtensionSystem::PluginManager::instance()->addObject(handler);
109 // in plugin A:
110 QList<MimeTypeHandler *> mimeHandlers =
111 ExtensionSystem::PluginManager::instance()->getObjects<MimeTypeHandler>();
112 \endcode
114 \bold Note: The object pool manipulating functions are thread-safe.
118 \fn void PluginManager::objectAdded(QObject *obj)
119 Signal that \a obj has been added to the object pool.
123 \fn void PluginManager::aboutToRemoveObject(QObject *obj)
124 Signal that \a obj will be removed from the object pool.
128 \fn void PluginManager::pluginsChanged()
129 Signal that the list of available plugins has changed.
131 \sa plugins()
135 \fn T *PluginManager::getObject() const
136 Retrieve the object of a given type from the object pool.
137 This method is aware of Aggregation::Aggregate, i.e. it uses
138 the Aggregation::query methods instead of qobject_cast to
139 determine the type of an object.
140 If there are more than one object of the given type in
141 the object pool, this method will choose an arbitrary one of them.
143 \sa addObject()
147 \fn QList<T *> PluginManager::getObjects() const
148 Retrieve all objects of a given type from the object pool.
149 This method is aware of Aggregation::Aggregate, i.e. it uses
150 the Aggregation::query methods instead of qobject_cast to
151 determine the type of an object.
153 \sa addObject()
156 using namespace ExtensionSystem;
157 using namespace ExtensionSystem::Internal;
159 static bool lessThanByPluginName(const PluginSpec *one, const PluginSpec *two)
161 return one->name() < two->name();
164 PluginManager *PluginManager::m_instance = 0;
167 \fn PluginManager *PluginManager::instance()
168 Get the unique plugin manager instance.
170 PluginManager *PluginManager::instance()
172 return m_instance;
176 \fn PluginManager::PluginManager()
177 Create a plugin manager. Should be done only once per application.
179 PluginManager::PluginManager()
180 : d(new PluginManagerPrivate(this)), m_allPluginsLoaded(false)
182 m_instance = this;
186 \fn PluginManager::~PluginManager()
187 \internal
189 PluginManager::~PluginManager()
191 delete d;
192 d = 0;
196 \fn void PluginManager::addObject(QObject *obj)
197 Add the given object \a obj to the object pool, so it can be retrieved again from the pool by type.
198 The plugin manager does not do any memory management - added objects
199 must be removed from the pool and deleted manually by whoever is responsible for the object.
201 Emits the objectAdded() signal.
203 \sa PluginManager::removeObject()
204 \sa PluginManager::getObject()
205 \sa PluginManager::getObjects()
207 void PluginManager::addObject(QObject *obj)
209 d->addObject(obj);
213 \fn void PluginManager::removeObject(QObject *obj)
214 Emits aboutToRemoveObject() and removes the object \a obj from the object pool.
215 \sa PluginManager::addObject()
217 void PluginManager::removeObject(QObject *obj)
219 d->removeObject(obj);
223 \fn QList<QObject *> PluginManager::allObjects() const
224 Retrieve the list of all objects in the pool, unfiltered.
225 Usually clients do not need to call this.
226 \sa PluginManager::getObject()
227 \sa PluginManager::getObjects()
229 QList<QObject *> PluginManager::allObjects() const
231 return d->allObjects;
235 \fn void PluginManager::loadPlugins()
236 Tries to load all the plugins that were previously found when
237 setting the plugin search paths. The plugin specs of the plugins
238 can be used to retrieve error and state information about individual plugins.
240 \sa setPluginPaths()
241 \sa plugins()
243 void PluginManager::loadPlugins()
245 return d->loadPlugins();
249 \fn QStringList PluginManager::pluginPaths() const
250 The list of paths were the plugin manager searches for plugins.
252 \sa setPluginPaths()
254 QStringList PluginManager::pluginPaths() const
256 return d->pluginPaths;
260 \fn void PluginManager::setPluginPaths(const QStringList &paths)
261 Sets the plugin search paths, i.e. the file system paths where the plugin manager
262 looks for plugin descriptions. All given \a paths and their sub directory trees
263 are searched for plugin xml description files.
265 \sa pluginPaths()
266 \sa loadPlugins()
268 void PluginManager::setPluginPaths(const QStringList &paths)
270 d->setPluginPaths(paths);
274 \fn QString PluginManager::fileExtension() const
275 The file extension of plugin description files.
276 The default is "xml".
278 \sa setFileExtension()
280 QString PluginManager::fileExtension() const
282 return d->extension;
286 \fn void PluginManager::setFileExtension(const QString &extension)
287 Sets the file extension of plugin description files.
288 The default is "xml".
289 At the moment this must be called before setPluginPaths() is called.
290 // ### TODO let this + setPluginPaths read the plugin specs lazyly whenever loadPlugins() or plugins() is called.
292 void PluginManager::setFileExtension(const QString &extension)
294 d->extension = extension;
298 \fn QStringList PluginManager::arguments() const
299 The arguments left over after parsing (Neither startup nor plugin
300 arguments). Typically, this will be the list of files to open.
302 QStringList PluginManager::arguments() const
304 return d->arguments;
308 \fn QList<PluginSpec *> PluginManager::plugins() const
309 List of all plugin specifications that have been found in the plugin search paths.
310 This list is valid directly after the setPluginPaths() call.
311 The plugin specifications contain the information from the plugins' xml description files
312 and the current state of the plugins. If a plugin's library has been already successfully loaded,
313 the plugin specification has a reference to the created plugin instance as well.
315 \sa setPluginPaths()
317 QList<PluginSpec *> PluginManager::plugins() const
319 return d->pluginSpecs;
323 \fn bool PluginManager::parseOptions(const QStringList &args, const QMap<QString, bool> &appOptions, QMap<QString, QString> *foundAppOptions, QString *errorString)
324 Takes the list of command line options in \a args and parses them.
325 The plugin manager itself might process some options itself directly (-noload <plugin>), and
326 adds options that are registered by plugins to their plugin specs.
327 The caller (the application) may register itself for options via the \a appOptions list, containing pairs
328 of "option string" and a bool that indicates if the option requires an argument.
329 Application options always override any plugin's options.
331 \a foundAppOptions is set to pairs of ("option string", "argument") for any application options that were found.
332 The command line options that were not processed can be retrieved via the arguments() method.
333 If an error occurred (like missing argument for an option that requires one), \a errorString contains
334 a descriptive message of the error.
336 Returns if there was an error.
338 bool PluginManager::parseOptions(const QStringList &args,
339 const QMap<QString, bool> &appOptions,
340 QMap<QString, QString> *foundAppOptions,
341 QString *errorString)
343 OptionsParser options(args, appOptions, foundAppOptions, errorString, d);
345 return options.parse();
349 static inline void indent(QTextStream &str, int indent)
351 const QChar blank = QLatin1Char(' ');
353 for (int i = 0; i < indent; i++) {
354 str << blank;
358 static inline void formatOption(QTextStream &str,
359 const QString &opt, const QString &parm, const QString &description,
360 int optionIndentation, int descriptionIndentation)
362 int remainingIndent = descriptionIndentation - optionIndentation - opt.size();
364 indent(str, optionIndentation);
365 str << opt;
366 if (!parm.isEmpty()) {
367 str << " <" << parm << '>';
368 remainingIndent -= 3 + parm.size();
370 indent(str, qMax(0, remainingIndent));
371 str << description << '\n';
375 \fn static PluginManager::formatOptions(QTextStream &str, int optionIndentation, int descriptionIndentation)
377 Format the startup options of the plugin manager for command line help.
380 void PluginManager::formatOptions(QTextStream &str, int optionIndentation, int descriptionIndentation)
382 formatOption(str, QLatin1String(OptionsParser::NO_LOAD_OPTION),
383 QLatin1String("plugin"), QLatin1String("Do not load <plugin>"),
384 optionIndentation, descriptionIndentation);
388 \fn PluginManager::formatPluginOptions(QTextStream &str, int optionIndentation, int descriptionIndentation) const
390 Format the plugin options of the plugin specs for command line help.
393 void PluginManager::formatPluginOptions(QTextStream &str, int optionIndentation, int descriptionIndentation) const
395 typedef PluginSpec::PluginArgumentDescriptions PluginArgumentDescriptions;
396 // Check plugins for options
397 const PluginSpecSet::const_iterator pcend = d->pluginSpecs.constEnd();
398 for (PluginSpecSet::const_iterator pit = d->pluginSpecs.constBegin(); pit != pcend; ++pit) {
399 const PluginArgumentDescriptions pargs = (*pit)->argumentDescriptions();
400 if (!pargs.empty()) {
401 str << "\nPlugin: " << (*pit)->name() << '\n';
402 const PluginArgumentDescriptions::const_iterator acend = pargs.constEnd();
403 for (PluginArgumentDescriptions::const_iterator ait = pargs.constBegin(); ait != acend; ++ait) {
404 formatOption(str, ait->name, ait->parameter, ait->description, optionIndentation, descriptionIndentation);
411 \fn PluginManager::formatPluginVersions(QTextStream &str) const
413 Format the version of the plugin specs for command line help.
416 void PluginManager::formatPluginVersions(QTextStream &str) const
418 const PluginSpecSet::const_iterator cend = d->pluginSpecs.constEnd();
420 for (PluginSpecSet::const_iterator it = d->pluginSpecs.constBegin(); it != cend; ++it) {
421 const PluginSpec *ps = *it;
422 str << " " << ps->name() << ' ' << ps->version() << ' ' << ps->description() << '\n';
426 void PluginManager::startTests()
428 #ifdef WITH_TESTS
429 foreach(PluginSpec * pluginSpec, d->testSpecs) {
430 const QMetaObject *mo = pluginSpec->plugin()->metaObject();
431 QStringList methods;
433 methods.append("arg0");
434 // We only want slots starting with "test"
435 for (int i = mo->methodOffset(); i < mo->methodCount(); ++i) {
436 if (QByteArray(mo->method(i).methodSignature()).startsWith("test")) {
437 QString method = QString::fromLatin1(mo->method(i).methodSignature());
438 methods.append(method.left(method.size() - 2));
441 QTest::qExec(pluginSpec->plugin(), methods);
443 #endif
447 * \fn bool PluginManager::runningTests() const
448 * \internal
450 bool PluginManager::runningTests() const
452 return !d->testSpecs.isEmpty();
456 * \fn QString PluginManager::testDataDirectory() const
457 * \internal
459 QString PluginManager::testDataDirectory() const
461 QByteArray ba = qgetenv("QTCREATOR_TEST_DIR");
462 QString s = QString::fromLocal8Bit(ba.constData(), ba.size());
464 if (s.isEmpty()) {
465 s = GCS_TEST_DIR;
466 s.append("/tests");
468 s = QDir::cleanPath(s);
469 return s;
472 // ============PluginManagerPrivate===========
475 \fn PluginSpec *PluginManagerPrivate::createSpec()
476 \internal
478 PluginSpec *PluginManagerPrivate::createSpec()
480 return new PluginSpec();
484 \fn PluginSpecPrivate *PluginManagerPrivate::privateSpec(PluginSpec *spec)
485 \internal
487 PluginSpecPrivate *PluginManagerPrivate::privateSpec(PluginSpec *spec)
489 return spec->d;
493 \fn PluginManagerPrivate::PluginManagerPrivate(PluginManager *pluginManager)
494 \internal
496 PluginManagerPrivate::PluginManagerPrivate(PluginManager *pluginManager)
497 : extension("xml"), q(pluginManager)
501 \fn PluginManagerPrivate::~PluginManagerPrivate()
502 \internal
504 PluginManagerPrivate::~PluginManagerPrivate()
506 stopAll();
507 qDeleteAll(pluginSpecs);
508 if (!allObjects.isEmpty()) {
509 qDebug() << "There are" << allObjects.size() << "objects left in the plugin manager pool: " << allObjects;
513 void PluginManagerPrivate::stopAll()
515 QList<PluginSpec *> queue = loadQueue();
516 foreach(PluginSpec * spec, queue) {
517 loadPlugin(spec, PluginSpec::Stopped);
519 QListIterator<PluginSpec *> it(queue);
520 it.toBack();
521 while (it.hasPrevious()) {
522 loadPlugin(it.previous(), PluginSpec::Deleted);
527 \fn void PluginManagerPrivate::addObject(QObject *obj)
528 \internal
530 void PluginManagerPrivate::addObject(QObject *obj)
533 QWriteLocker lock(&(q->m_lock));
534 if (obj == 0) {
535 qWarning() << "PluginManagerPrivate::addObject(): trying to add null object";
536 return;
538 if (allObjects.contains(obj)) {
539 qWarning() << "PluginManagerPrivate::addObject(): trying to add duplicate object";
540 return;
543 if (debugLeaks) {
544 qDebug() << "PluginManagerPrivate::addObject" << obj << obj->objectName();
547 allObjects.append(obj);
549 emit q->objectAdded(obj);
553 \fn void PluginManagerPrivate::removeObject(QObject *obj)
554 \internal
556 void PluginManagerPrivate::removeObject(QObject *obj)
558 if (obj == 0) {
559 qWarning() << "PluginManagerPrivate::removeObject(): trying to remove null object";
560 return;
563 if (!allObjects.contains(obj)) {
564 qWarning() << "PluginManagerPrivate::removeObject(): object not in list:"
565 << obj << obj->objectName();
566 return;
568 if (debugLeaks) {
569 qDebug() << "PluginManagerPrivate::removeObject" << obj << obj->objectName();
572 emit q->aboutToRemoveObject(obj);
573 QWriteLocker lock(&(q->m_lock));
574 allObjects.removeAll(obj);
578 \fn void PluginManagerPrivate::loadPlugins()
579 \internal
581 void PluginManagerPrivate::loadPlugins()
583 QList<PluginSpec *> queue = loadQueue();
584 foreach(PluginSpec * spec, queue) {
585 loadPlugin(spec, PluginSpec::Loaded);
587 foreach(PluginSpec * spec, queue) {
588 loadPlugin(spec, PluginSpec::Initialized);
590 QListIterator<PluginSpec *> it(queue);
591 it.toBack();
592 while (it.hasPrevious()) {
593 PluginSpec *plugin = it.previous();
594 emit q->pluginAboutToBeLoaded(plugin);
595 loadPlugin(plugin, PluginSpec::Running);
597 emit q->pluginsChanged();
598 q->m_allPluginsLoaded = true;
599 emit q->pluginsLoadEnded();
603 \fn void PluginManagerPrivate::loadQueue()
604 \internal
606 QList<PluginSpec *> PluginManagerPrivate::loadQueue()
608 QList<PluginSpec *> queue;
609 foreach(PluginSpec * spec, pluginSpecs) {
610 QList<PluginSpec *> circularityCheckQueue;
611 loadQueue(spec, queue, circularityCheckQueue);
613 return queue;
617 \fn bool PluginManagerPrivate::loadQueue(PluginSpec *spec, QList<PluginSpec *> &queue, QList<PluginSpec *> &circularityCheckQueue)
618 \internal
620 bool PluginManagerPrivate::loadQueue(PluginSpec *spec, QList<PluginSpec *> &queue,
621 QList<PluginSpec *> &circularityCheckQueue)
623 if (queue.contains(spec)) {
624 return true;
626 // check for circular dependencies
627 if (circularityCheckQueue.contains(spec)) {
628 spec->d->hasError = true;
629 spec->d->errorString = PluginManager::tr("Circular dependency detected:\n");
630 int index = circularityCheckQueue.indexOf(spec);
631 for (int i = index; i < circularityCheckQueue.size(); ++i) {
632 spec->d->errorString.append(PluginManager::tr("%1(%2) depends on\n")
633 .arg(circularityCheckQueue.at(i)->name()).arg(circularityCheckQueue.at(i)->version()));
635 spec->d->errorString.append(PluginManager::tr("%1(%2)").arg(spec->name()).arg(spec->version()));
636 return false;
638 circularityCheckQueue.append(spec);
639 // check if we have the dependencies
640 if (spec->state() == PluginSpec::Invalid || spec->state() == PluginSpec::Read) {
641 spec->d->hasError = true;
642 spec->d->errorString += "\n";
643 spec->d->errorString += PluginManager::tr("Cannot load plugin because dependencies are not resolved");
644 return false;
646 // add dependencies
647 foreach(PluginSpec * depSpec, spec->dependencySpecs()) {
648 if (!loadQueue(depSpec, queue, circularityCheckQueue)) {
649 spec->d->hasError = true;
650 spec->d->errorString =
651 PluginManager::tr("Cannot load plugin because dependency failed to load: %1(%2)\nReason: %3")
652 .arg(depSpec->name()).arg(depSpec->version()).arg(depSpec->errorString());
653 return false;
656 // add self
657 queue.append(spec);
658 return true;
662 \fn void PluginManagerPrivate::loadPlugin(PluginSpec *spec, PluginSpec::State destState)
663 \internal
665 void PluginManagerPrivate::loadPlugin(PluginSpec *spec, PluginSpec::State destState)
667 if (spec->hasError()) {
668 return;
670 if (destState == PluginSpec::Running) {
671 spec->d->initializeExtensions();
672 return;
673 } else if (destState == PluginSpec::Deleted) {
674 spec->d->kill();
675 return;
677 foreach(PluginSpec * depSpec, spec->dependencySpecs()) {
678 if (depSpec->state() != destState) {
679 spec->d->hasError = true;
680 spec->d->errorString =
681 PluginManager::tr("Cannot load plugin because dependency failed to load: %1(%2)\nReason: %3")
682 .arg(depSpec->name()).arg(depSpec->version()).arg(depSpec->errorString());
683 return;
686 if (destState == PluginSpec::Loaded) {
687 spec->d->loadLibrary();
688 } else if (destState == PluginSpec::Initialized) {
689 spec->d->initializePlugin();
690 } else if (destState == PluginSpec::Stopped) {
691 spec->d->stop();
696 \fn void PluginManagerPrivate::setPluginPaths(const QStringList &paths)
697 \internal
699 void PluginManagerPrivate::setPluginPaths(const QStringList &paths)
701 pluginPaths = paths;
702 readPluginPaths();
706 \fn void PluginManagerPrivate::readPluginPaths()
707 \internal
709 void PluginManagerPrivate::readPluginPaths()
711 qDeleteAll(pluginSpecs);
712 pluginSpecs.clear();
714 QStringList specFiles;
715 QStringList searchPaths = pluginPaths;
716 while (!searchPaths.isEmpty()) {
717 const QDir dir(searchPaths.takeFirst());
718 const QFileInfoList files = dir.entryInfoList(QStringList() << QString("*.%1").arg(extension), QDir::Files);
719 foreach(const QFileInfo &file, files)
720 specFiles << file.absoluteFilePath();
721 const QFileInfoList dirs = dir.entryInfoList(QDir::Dirs | QDir::NoDotAndDotDot);
722 foreach(const QFileInfo &subdir, dirs)
723 searchPaths << subdir.absoluteFilePath();
725 foreach(const QString &specFile, specFiles) {
726 PluginSpec *spec = new PluginSpec;
728 spec->d->read(specFile);
729 pluginSpecs.append(spec);
731 resolveDependencies();
732 // ensure deterministic plugin load order by sorting
733 qSort(pluginSpecs.begin(), pluginSpecs.end(), lessThanByPluginName);
734 emit q->pluginsChanged();
737 void PluginManagerPrivate::resolveDependencies()
739 foreach(PluginSpec * spec, pluginSpecs) {
740 spec->d->resolveDependencies(pluginSpecs);
744 // Look in argument descriptions of the specs for the option.
745 PluginSpec *PluginManagerPrivate::pluginForOption(const QString &option, bool *requiresArgument) const
747 // Look in the plugins for an option
748 typedef PluginSpec::PluginArgumentDescriptions PluginArgumentDescriptions;
750 *requiresArgument = false;
751 const PluginSpecSet::const_iterator pcend = pluginSpecs.constEnd();
752 for (PluginSpecSet::const_iterator pit = pluginSpecs.constBegin(); pit != pcend; ++pit) {
753 PluginSpec *ps = *pit;
754 const PluginArgumentDescriptions pargs = ps->argumentDescriptions();
755 if (!pargs.empty()) {
756 const PluginArgumentDescriptions::const_iterator acend = pargs.constEnd();
757 for (PluginArgumentDescriptions::const_iterator ait = pargs.constBegin(); ait != acend; ++ait) {
758 if (ait->name == option) {
759 *requiresArgument = !ait->parameter.isEmpty();
760 return ps;
765 return 0;
768 PluginSpec *PluginManagerPrivate::pluginByName(const QString &name) const
770 foreach(PluginSpec * spec, pluginSpecs)
771 if (spec->name() == name) {
772 return spec;
774 return 0;