更好的方式使用C ++命名参数成语? [英] Better Way To Use C++ Named Parameter Idiom?

查看:120
本文介绍了更好的方式使用C ++命名参数成语?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在开发一个用于Windows的GUI库(作为一个个人项目,没有有用的愿望)。对于我的主窗口类,我设置了一个选项类的层次结构(使用 Named Parameter Idiom ),因为一些选项是共享的,而其他选项是特定类型的窗口(如对话框)。

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).

Named Parameter Idiom工作,参数类的函数必须返回它们被调用的对象。问题是,在层次结构中,每个都必须是一个不同类 - createWindowOpts 类的标准窗口, createDialogOpts 用于对话框的类等。我已经处理了,通过所有的选项类模板。这里有一个例子:

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?

推荐答案

也许不是你想听到的,但我认为有很多丑陋的类型转换和模板参数在库代码(或多或少)从客户端隐藏只要它是安全使生活客户端很容易。图书馆代码中的美丽不在代码本身,但在代码中它使客户端能够写。以STL为例。

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.

我也开发了一个小的GUI库作为一个个人项目与基本上相同的愿望和一些代码变得很丑陋它,但最终它允许我写出漂亮的客户端代码(至少在我的(可能是变态的)的眼睛),这是什么计数IMHO。

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.

这篇关于更好的方式使用C ++命名参数成语?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆