tags:

views:

222

answers:

2

I would like to have something like this:

class Foo {
private:
  int bar;
public:
  void setBar(int bar);
  int getBar() const;
}

class MyDialog : public CDialogImpl<MyDialog> {
  BEGIN_MODEL_MAPPING()
    MAP_INT_EDITOR(m_editBar, m_model, getBar, setBar);
  END_MODEL_MAPPING()
  // other methods and message map
private:
  Foo * m_model;
  CEdit m_editBar;
}

Also it would be great if I could provide my custom validations:

MAP_VALIDATED_INT_EDITOR(m_editBar, m_model, getBar, setBar, validateBar)
...
bool validateBar (int value) {
  // custom validation
}

Have anybody seen something like this?

P.S. I don't like DDX because it's old and it's not flexible, and I cannot use getters and setters.

A: 

The Cocoa Bindings provide exactly what you want, but they are only available in the Mac / Objective-C word. GNUstep is a free version of it, but it's still Objective-C, not C++.

However, it might be a good inspiration for an own framework, or a good starting point for further research.

vog
It's nice to hear that it *can* be implemented. "Research" using google bring no results. If there will be no more answers, probably I will have to write my own framework.
cos
+2  A: 

The DDX map is just a series of if statements, so you can easily write your own DDX macro.

#define DDX_MAP_VALIDATED_INT_EDITOR(control, variable, getter, setter, validator)\
    if(nCtlID==control.GetDlgCtrlID())\
    {\
        if(bSaveAndValidate)\
        {\
            int const value=control.GetDlgItemInt();\
            if(validator(value))\
            {\
                variable->setter(value);\
            }\
            else\
            {\
                return false;\
            }\
        }\
        else\
        {\
            control.SetDlgItemInt(variable->getter());\
        }\
    }

This is untested, but should work as per your example, if you put it in the DDX map. It should give you the idea. Of course you could extract this into a function, which is what the standard DDX macros do: they just do the outer if and then call a function. This would allow you to overload the function for different types of the variable (e.g. pointer vs reference/value)

Anthony Williams