Implicitly shared class template: improve method declarations
ClosedPublic

Authored by kossebau on May 4 2017, 12:50 PM.

Details

Summary
  • Create special definitions for
    • default constructor
    • copy constructor
    • assign operator
  • Use common macros for method signature lines instead of own copies
  • Add definitions for any {public,protected,private}_functions

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 created this revision.May 4 2017, 12:50 PM
Restricted Application added a subscriber: kdevelop-devel. · View Herald TranscriptMay 4 2017, 12:50 PM
kossebau added a comment.EditedMay 4 2017, 12:57 PM

Example result, for class name = "KDevelop::Foo", one member "QString bar", all default methods enabled:

foo.cpp (updated to latest version):

#include "foo.h"
#include <QSharedData>

using namespace KDevelop;

class KDevelop::FooData : public QSharedData
{
public:
    QString bar;
};

Foo::Foo()
    : d(new FooData())
{
}

Foo::Foo(const Foo& other)
    : d(other.d)
{
}

Foo::~Foo()
{
}

Foo& Foo::operator=(const Foo& other)
{
    d = other.d;
    return *this;
}

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

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

Respective foo.h, as before:

#ifndef KDEVELOP_FOO_H
#define KDEVELOP_FOO_H

#include <QSharedDataPointer>

namespace KDevelop {

class FooData;

/**
 * @todo write docs
 */
class Foo
{
public:
    /**
     * Default constructor
     */
    Foo();

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

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

    /**
     * @todo write docs
     *
     * @param other TODO
     * @return TODO
     */
    Foo& operator=(const Foo& other);

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

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

private:
    QSharedDataPointer<FooData> d;
};

}

#endif // KDEVELOP_FOO_H
mwolff requested changes to this revision.May 4 2017, 1:39 PM
mwolff added a subscriber: mwolff.

http://doc.qt.io/qt-5/qshareddatapointer.html -> your change looks sane for *explicitly* shared data members, but not for implicit ones which will call detach in non-const operator->() automatically

this also means that your setter will always detach, the equality check is really not needed and just code noise.

This revision now requires changes to proceed.May 4 2017, 1:39 PM

http://doc.qt.io/qt-5/qshareddatapointer.html -> your change looks sane for *explicitly* shared data members, but not for implicit ones which will call detach in non-const operator->() automatically

this also means that your setter will always detach, the equality check is really not needed and just code noise.

Eek, was not aware of that automatic detach for non-const methods. Will have to check some code of mine then :) Though first update of this patch...

kossebau updated this revision to Diff 14135.May 4 2017, 2:07 PM
kossebau edited edge metadata.
  • remove useless explicit detach
  • remove no-change check (optimization only depending on usage)
  • simplify operator= code, rely on self-check of QSharedDataPointer
mwolff accepted this revision.May 4 2017, 2:17 PM
This revision is now accepted and ready to land.May 4 2017, 2:17 PM
kossebau edited the summary of this revision. (Show Details)May 4 2017, 2:20 PM
This revision was automatically updated to reflect the committed changes.