|
| 1 | +using System.Collections.Generic; |
| 2 | +using System.Linq; |
| 3 | +using CppSharp.AST; |
| 4 | +using CppSharp.Passes; |
| 5 | + |
| 6 | +namespace QtSharp |
| 7 | +{ |
| 8 | + public class RemoveQObjectMembersPass : TranslationUnitPass |
| 9 | + { |
| 10 | + public override bool VisitClassDecl(Class @class) |
| 11 | + { |
| 12 | + if (AlreadyVisited(@class) || @class.Name == "QObject") |
| 13 | + { |
| 14 | + return false; |
| 15 | + } |
| 16 | + |
| 17 | + IEnumerable<MacroExpansion> expansions = @class.PreprocessedEntities.OfType<MacroExpansion>(); |
| 18 | + |
| 19 | + bool isQObject = expansions.Any(e => e.Text == "Q_OBJECT"); |
| 20 | + if (isQObject) |
| 21 | + { |
| 22 | + RemoveQObjectMembers(@class); |
| 23 | + } |
| 24 | + return true; |
| 25 | + } |
| 26 | + |
| 27 | + private static void RemoveQObjectMembers(Class @class) |
| 28 | + { |
| 29 | + // Every Qt object "inherits" a lot of members via the Q_OBJECT macro. |
| 30 | + // See the define of Q_OBJECT in qobjectdefs.h for a list of the members. |
| 31 | + // We cannot use the Qt defines for disabling the expansion of these |
| 32 | + // because it would mess up with the object layout size. |
| 33 | + |
| 34 | + RemoveMethodOverloads(@class, "tr"); |
| 35 | + RemoveMethodOverloads(@class, "trUtf8"); |
| 36 | + RemoveMethodOverloads(@class, "qt_static_metacall"); |
| 37 | + RemoveVariables(@class, "staticMetaObject"); |
| 38 | + } |
| 39 | + |
| 40 | + private static void RemoveMethodOverloads(Class @class, string originalName) |
| 41 | + { |
| 42 | + var overloads = @class.FindMethodByOriginalName(originalName).ToList(); |
| 43 | + foreach (var method in overloads) |
| 44 | + @class.Methods.Remove(method); |
| 45 | + } |
| 46 | + |
| 47 | + private static void RemoveVariables(Class @class, string originalName) |
| 48 | + { |
| 49 | + var variables = @class.FindVariableByOriginalName(originalName).ToList(); |
| 50 | + foreach (var variable in variables) |
| 51 | + variable.ExplicitlyIgnore(); |
| 52 | + } |
| 53 | + } |
| 54 | +} |
0 commit comments