我一直在为 Windows 开发一个 GUI 库(作为个人业余项目,不希望有用)。 对于我的主窗口类,我设置了选项类的层次结构(使用 命名参数习惯用法),因为某些选项是共享的,而其他选项则特定于特定类型的窗口(如对话框)。
命名参数习惯用法的工作方式是,参数类的函数必须返回它们所调用的对象。 问题在于,在层次结构中,每个类都必须是不同的类 - 标准窗口的 createWindowOpts 类、createDialogOpts 类用于对话框等。 我已经通过制作所有选项类模板来解决这个问题。 这是一个例子:
template <class T>
class _sharedWindowOpts: public detail::_baseCreateWindowOpts {
public: ///////////////////////////////////////////////////////////////
// No required parameters in this case.
_sharedWindowOpts() { };
typedef T optType;
// Commonly used options
optType& at(int x, int y) { mX=x; mY=y; return static_cast<optType&>(*this); }; // Where to put the upper-left corner of the window; if not specified, the system sets it to a default position
optType& at(int x, int y, int width, int height) { mX=x; mY=y; mWidth=width; mHeight=height; return static_cast<optType&>(*this); }; // Sets the position and size of the window in a single call
optType& background(HBRUSH b) { mBackground=b; return static_cast<optType&>(*this); }; // Sets the default background to this brush
optType& background(INT_PTR b) { mBackground=HBRUSH(b+1); return static_cast<optType&>(*this); }; // Sets the default background to one of the COLOR_* colors; defaults to COLOR_WINDOW
optType& cursor(HCURSOR c) { mCursor=c; return static_cast<optType&>(*this); }; // Sets the default mouse cursor for this window; defaults to the standard arrow
optType& hidden() { mStyle&=~WS_VISIBLE; return static_cast<optType&>(*this); }; // Windows are visible by default
optType& icon(HICON iconLarge, HICON iconSmall=0) { mIcon=iconLarge; mSmallIcon=iconSmall; return static_cast<optType&>(*this); }; // Specifies the icon, and optionally a small icon
// ...Many others removed...
};
template <class T>
class _createWindowOpts: public _sharedWindowOpts<T> {
public: ///////////////////////////////////////////////////////////////
_createWindowOpts() { };
// These can't be used with child windows, or aren't needed
optType& menu(HMENU m) { mMenuOrId=m; return static_cast<optType&>(*this); }; // Gives the window a menu
optType& owner(HWND hwnd) { mParentOrOwner=hwnd; return static_cast<optType&>(*this); }; // Sets the optional parent/owner
};
class createWindowOpts: public _createWindowOpts<createWindowOpts> {
public: ///////////////////////////////////////////////////////////////
createWindowOpts() { };
};
它可以工作,但正如您所看到的,它需要大量的额外工作:每个函数的返回类型的类型转换、额外的模板类等等。
我的问题是,在这种情况下是否有一种更简单的方法来实现命名参数惯用语,不需要所有额外的东西?
I've been developing a GUI library for Windows (as a personal side project, no aspirations of usefulness). For my main window class, I've set up a hierarchy of option classes (using the Named Parameter Idiom), because some options are shared and others are specific to particular types of windows (like dialogs).
The way the Named Parameter Idiom works, the functions of the parameter class have to return the object they're called on. The problem is that, in the hierarchy, each one has to be a different class -- the createWindowOpts
class for standard windows, the createDialogOpts
class for dialogs, and the like. I've dealt with that by making all the option classes templates. Here's an example:
template <class T>
class _sharedWindowOpts: public detail::_baseCreateWindowOpts {
public: ///////////////////////////////////////////////////////////////
// No required parameters in this case.
_sharedWindowOpts() { };
typedef T optType;
// Commonly used options
optType& at(int x, int y) { mX=x; mY=y; return static_cast<optType&>(*this); }; // Where to put the upper-left corner of the window; if not specified, the system sets it to a default position
optType& at(int x, int y, int width, int height) { mX=x; mY=y; mWidth=width; mHeight=height; return static_cast<optType&>(*this); }; // Sets the position and size of the window in a single call
optType& background(HBRUSH b) { mBackground=b; return static_cast<optType&>(*this); }; // Sets the default background to this brush
optType& background(INT_PTR b) { mBackground=HBRUSH(b+1); return static_cast<optType&>(*this); }; // Sets the default background to one of the COLOR_* colors; defaults to COLOR_WINDOW
optType& cursor(HCURSOR c) { mCursor=c; return static_cast<optType&>(*this); }; // Sets the default mouse cursor for this window; defaults to the standard arrow
optType& hidden() { mStyle&=~WS_VISIBLE; return static_cast<optType&>(*this); }; // Windows are visible by default
optType& icon(HICON iconLarge, HICON iconSmall=0) { mIcon=iconLarge; mSmallIcon=iconSmall; return static_cast<optType&>(*this); }; // Specifies the icon, and optionally a small icon
// ...Many others removed...
};
template <class T>
class _createWindowOpts: public _sharedWindowOpts<T> {
public: ///////////////////////////////////////////////////////////////
_createWindowOpts() { };
// These can't be used with child windows, or aren't needed
optType& menu(HMENU m) { mMenuOrId=m; return static_cast<optType&>(*this); }; // Gives the window a menu
optType& owner(HWND hwnd) { mParentOrOwner=hwnd; return static_cast<optType&>(*this); }; // Sets the optional parent/owner
};
class createWindowOpts: public _createWindowOpts<createWindowOpts> {
public: ///////////////////////////////////////////////////////////////
createWindowOpts() { };
};
It works, but as you can see, it requires a noticeable amount of extra work: a type-cast on the return type for each function, extra template classes, etcetera.
My question is, is there an easier way to implement the Named Parameter Idiom in this case, one that doesn't require all the extra stuff?
发布评论
评论(6)
我知道我迟到了一年而且还缺一美元,但无论如何我都会提出我的解决方案。
C 派生自 B,B 派生自 A。我重复了参数以表明它们可以按所需的任何顺序排列。
I know I'm a year late and a dollar short, but I'll pitch in my solution anyways.
C derives from B, and B derives from A. I've repeated parameters to show they can put in any order desired.
模板很热门。
但 POP(普通多态性)并没有消亡。
为什么不返回一个指向子类的(智能)指针?
Templates are hot.
But POP (Plain old Polymorphism) isn't dead.
Why not return a (smart)pointer to the subclass?
我不知道我是否喜欢这个答案,但这里有使用模板参数推导的可能性。 注意我身上没有编译器,明天我会仔细检查它,除非其他人想尝试一下。
然后您将像这样调用 CreateWindow:
当然,令人讨厌的事情是必须使用静态方法调用语法和所有额外的括号。 如果将静态成员函数替换为非成员函数,则可以消除这种情况。 不过,它确实避免了类型转换和额外的模板类。
就我个人而言,我宁愿在库中使用与您的方法一样的奇怪代码,而不是像我的方法一样在库中使用任何地方。
I don't know if I'm in love with this answer, but here's a possibility using template argument deduction. NOTE I do not have my compiler on me, I'll double-check it tomorrow unless somebody else out there wants to give it a whirl.
Then you would call CreateWindow like this:
The obnoxious things about this, of course, are having to use the static method calling syntax and all the extra parentheses. If you replace the static member functions with non-member functions this can be eliminated. It does avoid the type-casting and the extra template classes, though.
Personally, I'd rather have the odd code in the library as with your method, than everywhere the library is being used like in mine.
您可以通过继承的相反顺序来链接方法调用吗?
因此,在您的示例中,您会执行类似
Window window = CreateWindow("foo").menu(hmenu).owner(hwnd).at(0,0).background(hbr); 的操作
我意识到它不是 100% 透明,但看起来更容易并且几乎正确。
Could you just chain the method calls by reverse order of inheritance?
So in your example you'd do something like
Window window = CreateWindow("foo").menu(hmenu).owner(hwnd).at(0,0).background(hbr);
I realize it's not 100% transparent but seems a little easier and almost correct.
怎么样...?
How about...?
也许不是您想听到的,但我认为在库代码中存在大量丑陋的类型转换和模板参数是可以的,这些代码(或多或少)对客户端隐藏只要它很安全并且让客户的生活变得更加轻松。 库代码的美妙之处不在于代码本身,而在于它使客户端能够编写的代码。 以STL为例。
我还开发了一个小型 GUI 库作为个人项目,与您的愿望基本相同,其中一些代码变得非常丑陋,但最终它允许我编写漂亮的客户端代码(至少在我的(可能是变态的)眼睛)这才是重要的恕我直言。
Maybe not what you want to hear, but I for one think it's ok to have lots of ugly type-casts and template parameters in library-code that's (more or less) hidden from the client as long as it is safe and makes the life of the client a lot easier. The beauty in library code is not in the code itself, but in the code it enables the clients to write. Take STL for example.
I've also developed a small GUI-library as a personal project with basically the same aspirations as you and some of the code gets pretty ugly in it, but in the end it allows me to write beautiful client code (at least in my (possibly perverted) eyes) and that's what counts IMHO.