File: cppclasshelper.cpp

package info (click to toggle)
kdevelop 4%3A4.7.0-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 18,360 kB
  • ctags: 11,484
  • sloc: cpp: 105,371; python: 522; ansic: 488; lex: 422; sh: 139; ruby: 120; makefile: 51; xml: 42; php: 12
file content (219 lines) | stat: -rw-r--r-- 6,310 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
/*
 * KDevelop C++ Language Support
 *
 * Copyright 2008 Hamish Rodda <rodda@kde.org>
 * Copyright 2012 Miha Čančula <miha@noughmad.eu>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Library General Public License as
 * published by the Free Software Foundation; either version 2 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public
 * License along with this program; if not, write to the
 * Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 */

#include "cppclasshelper.h"
#include <codecompletion/missingincludeitem.h>
#include <language/duchain/declaration.h>
#include <language/codegen/documentchangeset.h>
#include <language/codegen/codedescription.h>

#include <KTemporaryFile>

using namespace KDevelop;

CppClassHelper::CppClassHelper()
{

}

CppClassHelper::~CppClassHelper()
{

}

TemplateClassGenerator* CppClassHelper::createGenerator(const KUrl& baseUrl)
{
    return new CppTemplateNewClass(baseUrl);
}

QList<DeclarationPointer> CppClassHelper::defaultMethods(const QString& name) const
{
    KTemporaryFile file;
    file.setSuffix( ".cpp" );
    file.setAutoRemove(false);
    file.open();
    QTextStream stream(&file);
    stream << "class " << name << " {\n"
    << "  public:\n"
    // default ctor
    << "    " << name << "();\n"
    // copy ctor
    << "    " << name << "(const " << name << "& other);\n"
    // default dtor
    << "    ~" << name << "();\n"
    // assignment operator
    << "    " << name << "& operator=(const " << name << "& other);\n"
    // equality operator
    << "    bool operator==(const " << name << "& other) const;\n"
    << "};\n";
    file.close();
    ReferencedTopDUContext context(DUChain::self()->waitForUpdate( IndexedString(file.fileName()),
                                                                   TopDUContext::AllDeclarationsAndContexts ));
    DUChainReadLocker lock;

    QList<DeclarationPointer> methods;

    if (context && context->childContexts().size() == 1) {
        foreach (Declaration* declaration, context->childContexts().first()->localDeclarations())
        {
            methods << DeclarationPointer(declaration);
        }
    }

    file.remove();
    return methods;
}

CppTemplateNewClass::CppTemplateNewClass(const KUrl& url)
: TemplateClassGenerator(url)
{

}

CppTemplateNewClass::~CppTemplateNewClass()
{

}

QVariantHash CppTemplateNewClass::extraVariables()
{
    QVariantHash variables;

    QMap<QString, VariableDescriptionList> variableDescriptions;

    QMap<QString, FunctionDescriptionList> functionDescriptions;
    QMap<QString, FunctionDescriptionList> slotDescriptions;
    FunctionDescriptionList signalDescriptions;

    foreach (const FunctionDescription& function, description().methods)
    {
        QString access = function.access;
        if (access.isEmpty())
        {
            access = "public";
        }
        if (function.isSignal)
        {
            signalDescriptions << function;
        }
        else if (function.isSlot)
        {
            slotDescriptions[access] << function;
        }
        else
        {
            functionDescriptions[access] << function;
        }
    }

    foreach (const VariableDescription& variable, description().members)
    {
        QString access = variable.access;
        if (access.isEmpty())
        {
            access = "public";
        }
        variableDescriptions[access] << variable;
    }

    QMap<QString, VariableDescriptionList>::const_iterator vit, vend;
    vit = variableDescriptions.constBegin();
    vend = variableDescriptions.constEnd();
    for (; vit != vend; ++vit)
    {
        variables[vit.key() + "_members"] = CodeDescription::toVariantList(vit.value());
    }

    QMap<QString, FunctionDescriptionList>::const_iterator fit, fend;
    fit = functionDescriptions.constBegin();
    fend = functionDescriptions.constEnd();
    for (; fit != fend; ++fit)
    {
        variables[fit.key() + "_functions"] = CodeDescription::toVariantList(fit.value());
    }

    fit = slotDescriptions.constBegin();
    fend = slotDescriptions.constEnd();
    for (; fit != fend; ++fit)
    {
        variables[fit.key() + "_slots"] = CodeDescription::toVariantList(fit.value());
    }

    variables["signals"] = CodeDescription::toVariantList(signalDescriptions);
    variables["needs_qobject_macro"] = !slotDescriptions.isEmpty() || !signalDescriptions.isEmpty();

    QStringList includedFiles;
    DUChainReadLocker locker(DUChain::lock());

    KUrl sourceUrl;
    QHash<QString, KUrl> urls = fileUrls();
    if (!urls.isEmpty())
    {
        sourceUrl = urls.constBegin().value();
    }
    else
    {
        // includeDirectiveFromUrl() expects a header URL
        sourceUrl = baseUrl();
        sourceUrl.addPath(name().toLower() + ".h");
    }

    foreach (const DeclarationPointer& base, directBaseClasses())
    {
        if (!base)
        {
            continue;
        }
        kDebug() << "Looking for includes for class" << base->identifier().toString();
        KSharedPtr<Cpp::MissingIncludeCompletionItem> item = Cpp::includeDirectiveFromUrl(sourceUrl, IndexedDeclaration(base.data()));
        if(item)
        {
            kDebug() << "Found one in" << item->m_canonicalPath;
            includedFiles << item->m_addedInclude;
        }
    }
    variables["included_files"] = includedFiles;

    return variables;
}


DocumentChangeSet CppTemplateNewClass::generate()
{
  addVariables(extraVariables());
  return TemplateClassGenerator::generate();
}

void CppTemplateNewClass::addBaseClass(const QString& base)
{
  //strip access specifier
  QStringList splitBase = base.split(' ', QString::SkipEmptyParts);

  //if no access specifier is found use public by default
  if(splitBase.size() == 1)
  {
    splitBase.prepend("public");
  }

  //Call base function with the access specifier
  TemplateClassGenerator::addBaseClass(splitBase.join(" "));
}