Overview
Examples
Screenshots
Comparisons
Applications
Download
Documentation
Tutorials
Bazaar
Status & Roadmap
FAQ
Authors & License
Forums
Funding Ultimate++
Search on this site
Search in forums












SourceForge.net Logo
Home » U++ Library support » U++ Widgets - General questions or Mixed problems » Overriding Display methods too complicated due to high amount of arguments (Making Display class easier to use)
Overriding Display methods too complicated due to high amount of arguments [message #55204] Sun, 18 October 2020 00:01 Go to previous message
Klugier is currently offline  Klugier
Messages: 1075
Registered: September 2012
Location: Poland, Kraków
Senior Contributor
Hello,

I would like to tell you what I don't like about Display class (CtrlLib). It's two virtual methods are complicated to override due to the need to explicitly repeat the parameter list. It is exactly 6 parameters. Let's take a look at what we have now:
class Display {
public:
...
virtual void PaintBackground(Draw& w, const Rect& r, const Value& q,
	                     Color ink, Color paper, dword style) const;
virtual void Paint(Draw& w, const Rect& r, const Value& q,
		   Color ink, Color paper, dword style) const;


The solution for that is to introduce simply DisplayPaintContext that will contain all parameters variables:
class Display {
public:
...
virtual void PaintBackground(const DisplayPaintContext& ctx) const;
virtual void Paint(const DisplayPaintContext& ctx) const;

// Or
virtual void PaintBackground(Draw& w, const DisplayPaintContext& ctx) const;
virtual void Paint(Draw& w, const DisplayPaintContext& ctx) const;


Of course, it can be implemented in alternative way for example by providing first Draw parameter and rest as a context. Of course we can not simply change the declaration of this method. To fix the problem we should introduce the new one and deprecate the old.

Sample replacment in Display and Array examplex:
struct FontFaceDisplay : Display {
	virtual void Paint(Draw& w, const DisplayPaintContext& ctx) const override {
                auto r = ctx.GetRect();

                Font fnt = Font(ctx.GetValue(), r.Height() - 2);
                String txt = Font::GetFaceName(ctx.GetValue());
                w.DrawRect(r, ctx.Paper());
                w.DrawText(r.left + 2, r.top + (r.Height() - GetTextSize(txt, fnt).cy) / 2, txt, fnt, ctx.GetInk());
	}
};

struct MyDisplay : public Display {
    virtual void Paint(Draw& w, const DisplayPaintContext& ctx) const override
        w.DrawRect(ctx.GetRect(), paper);
        w.DrawEllipse(ctx.GetRect(), ctx.GetValue());
    }

};


Reference:
- Coding Revolution - Long Parameter List
- Refactoring Guru - Long Parameter List
- SonarSource - Static analyzer - Functions should not have too many parameters (4 is maximum limit)

Klugier


U++ - one framework to rule them all.

[Updated on: Sun, 18 October 2020 01:03]

Report message to a moderator

 
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Previous Topic: How to use multi-tab control?
Next Topic: Can I make round form
Goto Forum:
  


Current Time: Fri Mar 29 13:41:30 CET 2024

Total time taken to generate the page: 0.01683 seconds