From 37634082be3a5bff40da1571870cdc19ca25eb82 Mon Sep 17 00:00:00 2001 From: schodet Date: Sat, 11 Feb 2006 14:29:35 +0000 Subject: Ajout de plus d'information sur un bad_any_cast. --- i/marvin/src/utils/any.hh | 46 +++++++++++++++++++++++++++------------------- i/marvin/src/utils/any.tcc | 27 +++++++++++++++++++++------ 2 files changed, 48 insertions(+), 25 deletions(-) (limited to 'i/marvin/src/utils') diff --git a/i/marvin/src/utils/any.hh b/i/marvin/src/utils/any.hh index eabee96..e61f312 100644 --- a/i/marvin/src/utils/any.hh +++ b/i/marvin/src/utils/any.hh @@ -1,9 +1,13 @@ #ifndef any_hh #define any_hh // any.hh -// robert - programme du robot 2005. {{{ +// marvin - programme du robot 2006. {{{ // -// Copyright (C) 2004 Nicolas Schodet +// Copyright (C) 2003-2006 Nicolas Schodet +// +// Robot APB Team/Efrei 2006. +// Web: http://assos.efrei.fr/robot/ +// Email: robot AT efrei DOT fr // // This program is free software; you can redistribute it and/or modify // it under the terms of the GNU General Public License as published by @@ -19,16 +23,15 @@ // along with this program; if not, write to the Free Software // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. // -// Contact : -// Web: http://perso.efrei.fr/~schodet/ -// Email: // }}} #include #include #include -/// This class can contain a data of any type. +/// This class can contain a data of almost any type. This very type must be +/// CopyConstructible and OutputStreamable. The OutputStreamable restriction +/// could be removed, but it is so convenient to output an any... class any { class AbstractHolder; @@ -38,7 +41,7 @@ class any any (void); /// Constructor. template - explicit any (const T &value); + explicit any (const T &value); /// Copy constructor. explicit any (const any &other); /// Destructor. @@ -49,14 +52,14 @@ class any any &operator= (const any &other); /// Copy another object into this any. template - any &operator= (const T &value); + any &operator= (const T &value); /// Test if the any object is empty. bool empty (void) const; /// Return the std::type_info of the contained object. const std::type_info &type (void) const; private: template - friend T *any_cast (any *rhs); + friend T *any_cast (any *rhs); friend std::ostream &operator<< (std::ostream &os, const any &rhs); /// Abstract holder class. class AbstractHolder @@ -82,16 +85,6 @@ class any }; }; -/// Object throw if a any_cast returning a reference fail. -class bad_any_cast : public std::bad_cast -{ - public: - virtual const char * what() const throw() - { - return "bad_any_cast: illegal conversion"; - } -}; - /// Return a pointer to the contained object or 0 on faillure. template T * @@ -108,6 +101,21 @@ template const T & any_cast (const any &rhs); +/// Object throw if a any_cast returning a reference fail. +class bad_any_cast : public std::bad_cast +{ + std::string what_; + public: + /// Constructor. + bad_any_cast (const std::type_info &from, const std::type_info &to); + /// Destructor which should throw nothing. + virtual ~bad_any_cast() throw() + { } + /// Get the error text. + virtual const char *what() const throw() + { return what_.c_str (); } +}; + /// Print the contained object. std::ostream & operator<< (std::ostream &os, const any &rhs); diff --git a/i/marvin/src/utils/any.tcc b/i/marvin/src/utils/any.tcc index fd3dca6..e942156 100644 --- a/i/marvin/src/utils/any.tcc +++ b/i/marvin/src/utils/any.tcc @@ -1,7 +1,13 @@ +#ifndef any_tcc +#define any_tcc // any.tcc -// robert - programme du robot 2005. {{{ +// marvin - programme du robot 2006. {{{ // -// Copyright (C) 2004 Nicolas Schodet +// Copyright (C) 2003-2006 Nicolas Schodet +// +// Robot APB Team/Efrei 2006. +// Web: http://assos.efrei.fr/robot/ +// Email: robot AT efrei DOT fr // // This program is free software; you can redistribute it and/or modify // it under the terms of the GNU General Public License as published by @@ -17,9 +23,6 @@ // along with this program; if not, write to the Free Software // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. // -// Contact : -// Web: http://perso.efrei.fr/~schodet/ -// Email: // }}} #include "list_ostream_output.hh" @@ -155,10 +158,22 @@ any_cast (const any &rhs) { const T *value = any_cast (&rhs); if (!value) - throw bad_any_cast (); + throw bad_any_cast (typeid (T), rhs.type ()); return *value; } +/// Constructor. +inline +bad_any_cast::bad_any_cast (const std::type_info &from, + const std::type_info &to) +{ + what_ = "illegal conversion from \'"; + what_ += from.name (); + what_ += "\' to \'"; + what_ += to.name (); + what_ += "\'"; +} + /// Print the contained object. inline std::ostream & -- cgit v1.2.3