Allan Rae <[EMAIL PROTECTED]> writes:

| On 23 Apr 2001, Lars Gullik [iso-8859-1] Bjønnes wrote:
| 
| > Allan Rae <[EMAIL PROTECTED]> writes:
| > | struct LFUNAction {
| > |   void operator() (pair<LFUN, string>);
| > | };
| > |
| > | for_each(macro.begin(), macro.end(), LFUNAction());
| >
| > Still don't like it.
| >
| > std::for_each(macro.begin(), macro.end(),
| >               lyx::class_fun(string, lyxfunc, LyXFunc::Dispatch));
| >
| > seems better to me, more obvious and open.
| 
| Then how about:
| 
| struct LFUNAction {
|       LFUNAction(LyXFunc &);
|       string const operator() (pair<int, string const &> lfun) {
|               return lf_.Dispatch(lfun.first, lfun.second);
|       }
| private:
|       LyXFunc lf_;
| };
| 
| for_each(macro.begin(), macro.end(), LFUNAction(lyxfunc));

Of course we would hide the macro inside a macro object too.

class Macro {
        typedef vector<pair<int, string const &> > Cmds;
        void execute(LyXFunc & lf) const {
                for_each(cmds_.begin, cmds_.end(), LFUNAction(lf));
        }
        void execute(LyXFunc & lf, vector<string> & res) const {
                // accumulate all returned strings in res
                // can std::accumulate be used here?
                Cmds::const_iterator cit = cmds_.begin();
                Cmds::const_iterator end = cmds_.end();
                LFUNAction la(lf);
                for (; cit != end; ++cit) {
                        res.push_back(la(*cit));
                }
        }
        void addCmd(int, string const & arg) {
                cmds_.push_back(make_pair(int, arg));
        }
private:
        struct LFUNAction {
                LFUNAction(LyXFunc & lf) : lf_(lf) {}
                string const operator() (Cmds::value_type const & vt) const {
                        return lf_.Dispatch(lfun.first, lfun.second);
                }
        private:
                LyXFunc lf_;
        };
        Cmds cmds_;
};

I don't really se the point in hiding the dispatching further:

...
typedef vector<LFUNAction> Cmds;
...
void execute
| This leaves the problem of what happens to the returned string.  I'm
| pretty sure we can ignore it completely.

Yes. The returned string can be ignored in almost all cases.
 
| It also seems that you are overlooking the extra string arguement as your
| solution sets the return type as string and only has one input, an
| int.

Yes, you are right.

-- 
        Lgb

Reply via email to