Boost logo

Boost :

From: Christopher Kohlhoff (chris_at_[hidden])
Date: 2006-11-16 07:37:11


Hi Johan, Johan Nilsson <r.johan.nilsson_at_[hidden]> wrote: > I'd be happy enough if such wrappers would be documented and > existed in <boost/system/convenience.hpp> or similar. Here's the sort of convenience wrapper that I would be happy to see (just a 4-argument Windows version that I hacked up): template < typename R, typename A1, typename A2, typename A3, typename A4> R system_call(R (__stdcall *f)(A1, A2, A3, A4), typename boost::mpl::identity<A1>::type a1, typename boost::mpl::identity<A2>::type a2, typename boost::mpl::identity<A3>::type a3, typename boost::mpl::identity<A4>::type a4, boost::system::error_code& ec, typename boost::mpl::identity<R>::type failure_retval = R()) { R r = f(a1, a2, a3, a4); if (r == failure_retval) { DWORD last_error = ::GetLastError(); ec = boost::system::error_code( last_error, boost::system::native_ecat); } else { ec = boost::system::error_code(); } return r; } template < typename R, typename A1, typename A2, typename A3, typename A4> R system_call(R (__stdcall *f)(A1, A2, A3, A4), typename boost::mpl::identity<A1>::type a1, typename boost::mpl::identity<A2>::type a2, typename boost::mpl::identity<A3>::type a3, typename boost::mpl::identity<A4>::type a4, typename boost::mpl::identity<R>::type failure_retval = R()) { boost::system::error_code ec; R r = system_call(f, a1, a2, a3, a4, ec, failure_retval); if (ec) throw boost::system::system_error(ec); return r; } And you can use it like this: system_call(::MessageBox, hwnd, "A", "B", MB_OK); or: boost::system::error_code ec; system_call(::MessageBox, hwnd, "A", "B", MB_OK, ec); It seems pretty convenient to use, does not have the issues with thread-specific error codes that I am concerned about, and provides a portable interface over the top of platform-specific error handling APIs. It'll even save people from making the error of forgetting to set the error_code out parameter 'ec'. Cheers, Chris


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