Feature: Add file template for QObject with pimpl
ClosedPublic

Authored by kossebau on Feb 4 2017, 9:48 PM.

Details

Summary

The current QObject template has all private members exposed
in the class, and there are reasons to use a pimpl variant
instead.
As the current file template logic does not allow to control
the number of files generated (like only generating the private
header if the user toggles a config option), the QObject pimpl
template is a complete separate template.

Both QObject templates share template snippets where useful.

Diff Detail

Repository
R32 KDevelop
Lint
Automatic diff as part of commit; lint not applicable.
Unit
Automatic diff as part of commit; unit tests not applicable.
kossebau updated this revision to Diff 10918.Feb 4 2017, 9:48 PM
kossebau retitled this revision from to Feature: Add file template for QObject with pimpl.
kossebau updated this object.
kossebau added a reviewer: KDevelop.
Restricted Application added a subscriber: kdevelop-devel. · View Herald TranscriptFeb 4 2017, 9:48 PM
kossebau added a comment.EditedFeb 4 2017, 9:53 PM

The QObject non-pimpl changes are supposed to be in a different commit, only added here for the big picture. That commit extracts reusable snippets and adds creation of signals for the properties,

Example generation:
name: KDevelop::Foo
member: QString bar
methods selected: constructor, copy constructor, destructor, event()

foo.h:

/*
 * <one line to give the library's name and an idea of what it does.>
 * Copyright (C) 2017  Hans Entwickler <email>
 * 
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 * 
 * This library 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
 * Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 * 
 */

#ifndef KDEVELOP_FOO_H
#define KDEVELOP_FOO_H

#include <qt5/QtCore/QObject>

namespace KDevelop {

/**
 * @todo write docs
 */
class Foo : public QObject
{
    Q_OBJECT
    Q_PROPERTY(QString bar READ bar WRITE setBar NOTIFY barChanged)

public:
    /**
     * Default constructor
     */
    Foo();

    /**
     * Copy Constructor
     *
     * @param other TODO
     */
    Foo(const Foo& other);

    /**
     * Destructor
     */
    ~Foo();

    /**
     * @todo write docs
     *
     * @param event TODO
     * @return TODO
     */
    virtual bool event(QEvent* event);

    /**
     * @return the bar
     */
    QString bar() const;

public Q_SLOTS:
    /**
     * Sets the bar.
     *
     * @param bar the new bar
     */
    void setBar(const QString& bar);

Q_SIGNALS:
    void barChanged(const QString& bar);

private:
    class FooPrivate* const d_ptr;
    Q_DECLARE_PRIVATE(Foo)
};

}

#endif // KDEVELOP_FOO_H

foo_p.h:

/*
 * <one line to give the library's name and an idea of what it does.>
 * Copyright (C) 2017  Hans Entwickler <email>
 * 
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 * 
 * This library 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
 * Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 * 
 */

#ifndef KDEVELOP_FOO_P_H
#define KDEVELOP_FOO_P_H

namespace KDevelop {

/**
 * @todo write docs
 */
class FooPrivate
{
public:
    QString bar;
};

}

#endif // KDEVELOP_FOO_P_H

foo.cpp: (Updated to latest version with copy constructor handling

/*
 * <one line to give the library's name and an idea of what it does.>
 * Copyright (C) 2017  Hans Entwickler <email>
 * 
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 * 
 * This library 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
 * Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 * 
 */

#include "foo.h"
#include "foo_p.h"

using namespace KDevelop;

Foo::Foo()
    : d_ptr(new FooPrivate())
{

}

Foo::Foo(const Foo& other)
    : d_ptr(new FooPrivate(*other.d_ptr))
{

}

Foo::~Foo()
{
    delete d_ptr;
    
}

bool Foo::event(QEvent* event)
{

}

QString Foo::bar() const
{
    Q_D(const Foo);
    return d->bar;
}

void Foo::setBar(const QString& bar)
{
    Q_D(Foo);
    if (d->bar == bar) {
        return;
    }

    d->bar = bar;
    emit barChanged(d->bar);
}

Ah, still to do, but needs some more brain cycles, the template as is should already help a long distance:
add code which for the copy constructor adds code which does the right thing for the d instances. Planned for some later look at the template, not now.

kossebau updated this revision to Diff 10920.Feb 4 2017, 10:51 PM

add special handling for copy constructor
add missing CMakeLists.txt changes

kfunk accepted this revision.Feb 15 2017, 1:12 PM
kfunk added a reviewer: kfunk.
kfunk added a subscriber: kfunk.

Generated code looks good to me.

And thanks for factoring out individual features into separate files!

This revision is now accepted and ready to land.Feb 15 2017, 1:12 PM
mwolff accepted this revision.Feb 15 2017, 10:15 PM
mwolff added a reviewer: mwolff.
mwolff added a subscriber: mwolff.

lgtm also

This revision was automatically updated to reflect the committed changes.