Boost logo

Boost :

From: Brad King (brad.king_at_[hidden])
Date: 2006-07-31 18:19:37


Emil Dotchevski wrote:
> I posted the new version of the proposed Boost Exception library last
> Friday, you can get the new documentation and code here:
>
> http://www.revergestudios.com/boost-exception/boost-exception.htm.

I've just briefly glanced through the current implementation and some of
the discussion earlier on the boost list. It looks pretty nice so far.

If it is not mentioned already in the documentation you should add an
explanation of why boost::exception does not derive from std::exception.
 I'm guessing it's so that enable_exception_info works for types that
derive from std::exception without diamond inheritance. User-defined
subclasses of boost::exception can also derive from std::exception anyway.

> I have received two suggestions by email to remove the use of macros for
> type registration.
[snip]
> struct tag_foo: exception_info_tag<int> { };

This version will allow custom display of the complete set of
information tag/value pairs in an exception object. You'll have to
convert the internal

  type_info => any

map to be

  type_info => exception_info_wrapper_base*

or something like that. The code of interest might look like this:

-------------------------------------------------------------------------
// Base class for exception information tag/value pairs.
struct exception_info_wrapper_base {
  // Method to describe information tag/value pair
  // in a human-readable form.
  virtual void print(std::ostream& os)=0;

  // Virtual destructor so that value deletion works properly.
  virtual ~exception_info_wrapper_base() {}
};

// Store an exception information tag/value pair.
template <class Tag>
struct exception_info_wrapper: exception_info_wrapper_base {
  // Get the information value type from the tag.
  typedef typename Tag::value_type value_type;

  // Construct with an exception information value.
  explicit exception_info_wrapper(value_type const& value):
    value_(value) {}

  // Store the exception information value.
  value_type value_;

  // Forward a print request to the static method provided by the tag.
  virtual void print(std::ostream& os) {
    Tag::print(os, typeid(Tag), value_);
    }
};

// Base class for exception information tags.
template <class T>
struct exception_info_tag {
  // Type of value associated with the tag.
  typedef T value_type;

  // Default implementation of tag/value pair display.
  static void print(std::ostream& os, std::type_info const& t,
                    value_type& value) {
    os << "Tag " << t.name() << " with value \"" << value << "\"";
    }
};

// Example tag using default pair display.
struct my_example_tag1: boost::exception_info_tag<int> {};

// Example tag using custom pair display.
struct my_example_tag2: boost::exception_info_tag<double> {
  static void print(std::ostream& os,
                    std::type_info const&,
                    value_type& value) {
    os << "Custom print of example tag 2: " << value;
    }
};
-------------------------------------------------------------------------

Now the exception object can loop through its map and invoke the virtual
print method on each entry. This could be useful for debugging or when
the set of potential information entries is not known and a message
describing the error is desired.

-Brad


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