Lepszy sposób używania idiomu nazwanych parametrów C ++?
Zajmuję się tworzeniem biblioteki GUI dla Windows (jako osobistego projektu pobocznego, bez aspiracji przydatności). Dla mojej głównej klasy okna ustawiłem hierarchię klas opcji (używającIdiom nazwanego parametru), ponieważ niektóre opcje są udostępniane, a inne są specyficzne dla określonych typów okien (takich jak okna dialogowe).
Sposób, w jaki działa idiom nazwanych parametrów, funkcje klasy parametrów muszą zwracać obiekt, do którego są wywoływane. Problem polega na tym, że w hierarchii każdy musi byćróżne klasa -createWindowOpts
klasa dla standardowych okien,createDialogOpts
klasa dla okien dialogowych i tym podobne. Zajmowałem się tym, tworząc wszystkie szablony klas opcji. Oto przykład:
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() { };
};
Działa, ale jak widać, wymaga zauważalnej ilości dodatkowej pracy: rzutowania typu zwracanego dla każdej funkcji, dodatkowych klas szablonów itp.
Moje pytanie brzmi: czy istnieje łatwiejszy sposób na wdrożenie idiomu nazwanych parametrów w tym przypadku, który nie wymaga wszystkich dodatkowych rzeczy?