Boost logo

Boost :

From: Dean Michael Berris (mikhailberis_at_[hidden])
Date: 2006-07-07 18:22:55


On 7/8/06, Jeremy Day <jeremy.day_at_[hidden]> wrote:
>
> If you go with references, what would happen in this situation:
>
<snipped code>

It all really boils down to the implementation of get_exception_info(T
&) and exception_info. Consider that let's say exception_info is
defined this way (for simplicity's sake): [untested]

class default_exception;

class exception_info : boost::noncopyable {
private:
  bool is_valid;

protected:
  exception_info() : is_valid(true) { };
  void set_valid(bool b) { is_valid = b ; };

public:
  static default_exception _default;

  operator bool() {
    return is_valid;
  };

  // ... other operations ...
};

class default_exception : public exception_info {
public:
  default_exception () {
    set_valid(false);
  }
};

template < typename T >
exception_info & get_exception_info (T & e) const {
  exception_info * p = dynamic_cast<exception_info>(&e);
  return (p == NULL) ? exception_info::_default : *p;
};

So when you always get a reference to an object of type exception_info
or derivatives, you can specialize the null object case -- and give
the client code a consistent handle to the exception.

It might count as too much voodoo to get something to return a
reference, but the benefits outweight the costs IMO. It just makes the
interface less "fool proof" and makes it harder to "shoot yourself in
the foot" when using a library of this utility.

HTH

-- 
Dean Michael C. Berris
C/C++ Software Architect
Orange and Bronze Software Labs
http://3w-agility.blogspot.com/
http://cplusplus-soup.blogspot.com/
Mobile: +639287291459
Email: dean [at] orangeandbronze [dot] com

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