scide: refactor lookup dialog
[supercollider.git] / editors / sc-ide / core / sc_introspection.hpp
blob7aebaddbcc11d8023273a38acbfb0c1c947b2229
1 /*
2 SuperCollider Qt IDE
3 Copyright (c) 2012 Jakob Leben & Tim Blechmann
4 http://www.audiosynth.com
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 #ifndef SCIDE_CORE_SC_INTROSPECTION_HPP_INCLUDED
22 #define SCIDE_CORE_SC_INTROSPECTION_HPP_INCLUDED
24 #include <QHash>
25 #include <QList>
26 #include <QMap>
27 #include <QMetaType>
28 #include <QSharedPointer>
29 #include <QString>
30 #include <QVector>
32 #include <string>
33 #include <map>
35 #include <boost/flyweight.hpp>
38 static inline std::size_t hash_value(QString const& b)
40 return qHash(b);
43 namespace ScIDE {
44 namespace ScLanguage {
46 typedef boost::flyweight<QString> FlyweightString;
48 typedef std::map<QString, QSharedPointer<class Class> > ClassMap;
49 typedef std::multimap<QString, QSharedPointer<class Method> > MethodMap;
50 typedef QVector<class Argument> ArgumentVector;
51 typedef QVector<class Method*> MethodVector;
53 struct Argument {
54 FlyweightString name;
55 FlyweightString defaultValue;
58 struct Class {
59 FlyweightString name;
60 Class *metaClass;
61 Class *superClass;
62 MethodVector methods;
63 struct {
64 FlyweightString path;
65 int position;
66 } definition;
68 bool isSubclassOf(const Class * parentClass) const
70 if (superClass == parentClass)
71 return true;
73 if (superClass == NULL)
74 return false;
76 return superClass->isSubclassOf(parentClass);
80 struct Method {
81 enum SignatureStyle {
82 SignatureWithoutArguments,
83 SignatureWithArguments,
84 SignatureWithArgumentsAndDefaultValues
87 QString signature( SignatureStyle style ) const;
89 Class *ownerClass;
90 FlyweightString name;
91 ArgumentVector arguments;
92 struct {
93 FlyweightString path;
94 int position;
95 } definition;
98 static inline QString makeFullMethodName(QString const & className, QString const & methodName)
100 QString ret (className);
101 if (ret.startsWith("Meta_")) {
102 ret.remove(0, 5);
103 ret.append("-*");
104 } else
105 ret.append("-");
106 ret.append(methodName);
107 return ret;
110 class Introspection
112 public:
113 Introspection();
114 Introspection( QString const & yamlString );
115 ~Introspection();
117 typedef QMap< QString, QList<Method*> > ClassMethodMap; // maps Path to List of Methods
119 const ClassMap & classMap() const { return mClassMap; }
120 const MethodMap & methodMap() const { return mMethodMap; }
122 const Class * findClass( QString const & className ) const;
123 ClassMethodMap constructMethodMap( const Class * klass ) const;
124 ClassMethodMap constructMethodMap( QString const & className ) const
126 return constructMethodMap(findClass(className));
129 QString const & classLibraryPath() const
131 return mClassLibraryPath;
134 // remove class library path, userExtensionDir and systemExtensionDir
135 QString compactLibraryPath(QString const & path) const;
137 bool isClassMethod (const Method * method) const
139 return (method->ownerClass->name.get().startsWith("Meta_"));
142 bool introspectionAvailable() const
144 return !mClassMap.empty();
147 private:
148 void initPaths();
149 bool parse(const QString & yamlString );
150 void inferClassLibraryPath();
152 void clear()
154 mClassMap.clear();
155 mMethodMap.clear();
156 mClassLibraryPath.clear();
159 ClassMap mClassMap;
160 MethodMap mMethodMap;
161 QString mClassLibraryPath;
162 QString mUserExtensionDir;
163 QString mSystemExtensionDir;
166 } // namespace ScLanguage
168 } // namespace ScIDE
170 Q_DECLARE_METATYPE(ScIDE::ScLanguage::Class*)
171 Q_DECLARE_METATYPE(const ScIDE::ScLanguage::Method*)
173 #endif // SCIDE_CORE_SC_INTROSPECTION_HPP_INCLUDED