Boost logo

Boost :

From: Dean Michael Berris (mikhailberis_at_[hidden])
Date: 2007-09-22 02:06:23


Hi Everyone,

I'm not sure if the title makes sense, but I have a problem which can
be solved with a handful of preprocessor macros, but would like to see
solved with Fusion.

Let's say I have a vector of values:

fusion::vector<int, int, int, int> ints(0, 1, 2, 3);

I then have a function such as:

struct function {
  void operator()(int value) {
    if (value < 2)
      throw runtime_error("less than 3");
    cout << value << endl;
  };
};

Would there be a way for me to iterate through every element of 'ints'
and apply the function object instance to each element, and stop the
iteration only when function does not throw?

I know using exceptions for control flow is pretty frowned upon, and I
can certainly use it the other way around -- to short-circuit the
iteration only when it throws.

If you're interested in the use case for this particular question, I'm
looking for a way to make the dispatcher library I'm working on
(http://dispatcher.sourceforge.net/) be able to generically support
the following construct:

dispatcher<void(int)> d;
d[2] = function();
invoke_(d)(10) << alt_(0, 1, 2, 3, 4);

alt_ currently returns a homogeneous tuple (if there's such a thing).

The invoker then should be able to try d[0](10) first, and since d[0]
does not map to a function, it should then try d[1](10) -- which also
isn't valid because d[1] is not defined -- until it gets to d[2](10),
which should perform the operation, and then effectively not deal with
3 and 4 anymore.

d[0](10) and d[1](10) will throw an exception of invalid_index --
currently, the implementation is hand coded to the effect of:

  try { d[get<0>(tuple)](arg1); }
  catch (invalid_index &) { d[get<1>(tuple)](arg1); }

So the more indexes to check, the deeper the try { } catch (...) { }
nesting goes -- which I know can be automated with a preprocessor, but
I'm exploring other possibilities this time using Fusion.

Pointers would be greatly appreciated.

Thanks in advance!

-- 
Dean Michael C. Berris
Software Engineer, Friendster, Inc.
[http://cplusplus-soup.blogspot.com/]
[mikhailberis_at_[hidden]]
[+63 928 7291459]
[+1 408 4049523]

Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk