Hea*_*eek 9 c++ named-parameters
我一直在为Windows开发一个GUI库(作为个人辅助项目,没有实用性的愿望).对于我的主窗口类,我已经设置了一个选项类的层次结构(使用命名参数成语),因为一些选项是共享的,而其他选项是特定于特定类型的窗口(如对话框).
命名参数Idiom的工作方式,参数类的函数必须返回它们被调用的对象.问题是,在层次结构中,每个都必须是一个不同的类 - 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() { };
};
Run Code Online (Sandbox Code Playgroud)
它可以工作,但正如你所看到的,它需要大量的额外工作:每个函数的返回类型的类型转换,额外的模板类等等.
我的问题是,在这种情况下是否有更简单的方法来实现命名参数成语,一个不需要所有额外的东西?
也许不是你想听到的,但是我觉得在库代码中有很多丑陋的类型转换和模板参数,只要它是安全的并且可以生活,它会(或多或少)隐藏在客户端之外客户端更容易.库代码中的优点不在于代码本身,而是在代码中它使客户端能够编写代码.以STL为例.
我还开发了一个小型GUI库作为个人项目,与你的愿望基本相同,其中一些代码变得非常丑陋,但最终它允许我编写漂亮的客户端代码(至少在我的(可能变态的眼睛)这就是恕我直言.
怎么样...?
template <class T>
class _sharedWindowOpts: public detail::_baseCreateWindowOpts {
protected: // (protected so the inheriting classes may also use it)
T & me() { return static_cast<T&>(*this); } // !
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 me(); }; // !
// ...
};
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2932 次 |
| 最近记录: |