summaryrefslogtreecommitdiff
path: root/i/marvin/src/utils
diff options
context:
space:
mode:
authorschodet2006-02-11 14:29:35 +0000
committerschodet2006-02-11 14:29:35 +0000
commit37634082be3a5bff40da1571870cdc19ca25eb82 (patch)
treeeba870d79f2ae31327061b53ec87000b2b2c6f75 /i/marvin/src/utils
parent96f701a5e5b352d0608fcf4eeaba8f467d0dfe29 (diff)
Ajout de plus d'information sur un bad_any_cast.
Diffstat (limited to 'i/marvin/src/utils')
-rw-r--r--i/marvin/src/utils/any.hh46
-rw-r--r--i/marvin/src/utils/any.tcc27
2 files changed, 48 insertions, 25 deletions
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: <contact@ni.fr.eu.org>
// }}}
#include <typeinfo>
#include <exception>
#include <iostream>
-/// 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<typename T>
- 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<typename T>
- 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<typename T>
- 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<typename T>
T *
@@ -108,6 +101,21 @@ template<typename T>
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: <contact@ni.fr.eu.org>
// }}}
#include "list_ostream_output.hh"
@@ -155,10 +158,22 @@ any_cast (const any &rhs)
{
const T *value = any_cast<T> (&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 &