Introduce Units singleton
ClosedPublic

Authored by broulik on Jan 8 2017, 11:48 AM.

Details

Summary

Every single (Frame)SvgItem would creates its own Units instance which in turn would create a property map for icon sizes and lots of other stuff. Avoid this.

Test Plan

I no longer get a tonne of Units objects created

Diff Detail

Repository
R242 Plasma Framework (Library)
Lint
Automatic diff as part of commit; lint not applicable.
Unit
Automatic diff as part of commit; unit tests not applicable.
broulik updated this revision to Diff 9841.Jan 8 2017, 11:48 AM
broulik retitled this revision from to Introduce Units singleton.
broulik updated this object.
broulik edited the test plan for this revision. (Show Details)
broulik added a reviewer: Plasma.
broulik set the repository for this revision to R242 Plasma Framework (Library).
Restricted Application added projects: Plasma, Frameworks. · View Herald TranscriptJan 8 2017, 11:48 AM
Restricted Application added subscribers: Frameworks, plasma-devel. · View Herald Transcript
broulik updated this revision to Diff 9846.Jan 8 2017, 12:27 PM
  • Q_DISABLE_COPY
  • Make constructor private
davidedmundson accepted this revision.Jan 8 2017, 1:31 PM
davidedmundson added a reviewer: davidedmundson.
This revision is now accepted and ready to land.Jan 8 2017, 1:31 PM
markg requested changes to this revision.Jan 8 2017, 2:36 PM
markg added a reviewer: markg.
markg added a subscriber: markg.
markg added inline comments.
src/declarativeimports/core/units.cpp
61

Remove this line if you go for the comment i made below.

97–103

I think you need to do more, or rather less.
In C++11 making a singleton is really easy! All you need is:

Units &Units::self()
{
  static Units units;
  return units;
}

And done. It gives you a thread safe singleton (which your current version isn't).
I also think the current compiler requirements for plasma and frameworks allow you to use the code I've just given.. So.. Use it :)
Note: i changed the return value to a reference.

Also, why use "self" as getter for a singleton? I think "instance" or "getInstance" is the most used name for this, but i might be wrong here.

src/declarativeimports/core/units.h
122

Add documentation please.

185–186

If this were pre C++11, you'd be done :)
But we have move semantics now. You need to disable moving as well!

Units(Units const&) = delete;                       // Copy construct
Units(Units&&) = delete;                              // Move construct
Units& operator=(Units const&) = delete;  // Copy assign
Units& operator=(Units &&) = delete;         // Move assign

I'd drop the Q_DISABLE_COPY and go for the above "= delete" lines, but that's up for you to decide. You have to add them for move semantics anyway (Q_DISABLE_COPY does not do that for you).

200

Remove this line if you go for the comment i made below.

This revision now requires changes to proceed.Jan 8 2017, 2:36 PM

I was actually thinking about using the "proper" singleton way but didn't want it to be inconsistent with everywhere else. But, yeah, I'll do that.

broulik updated this revision to Diff 9857.Jan 8 2017, 2:42 PM
broulik edited edge metadata.
  • Return reference instead of ptr
  • Rename to "instance"
  • Disable move and stuff
broulik marked 3 inline comments as done.Jan 8 2017, 2:43 PM
broulik marked an inline comment as done.
markg accepted this revision.Jan 8 2017, 2:51 PM
markg edited edge metadata.

Looks nice and clean to me now :)
Nice job!

This revision is now accepted and ready to land.Jan 8 2017, 2:51 PM
This revision was automatically updated to reflect the committed changes.