Added cast_to() to the type_safe_union. This allows you to get the contents

of a const type_safe_union.
This commit is contained in:
Davis King 2014-10-12 22:39:31 -04:00
parent 1ee8b5dc8c
commit ad22944203
3 changed files with 83 additions and 1 deletions

View File

@ -97,6 +97,13 @@ namespace
f_val = 4.345f;
a.get<float>() = f_val;
DLIB_TEST(a.cast_to<float>() == f_val);
DLIB_TEST(const_cast<const tsu&>(a).cast_to<float>() == f_val);
bool exception_thrown = false;
try {a.cast_to<char>(); }
catch (bad_type_safe_union_cast&) { exception_thrown = true;}
DLIB_TEST(exception_thrown);
DLIB_TEST(a.is_empty() == false);
DLIB_TEST(a.contains<char>() == false);
@ -134,6 +141,12 @@ namespace
a.apply_to_contents(*this);
DLIB_TEST(last_kind == STRING);
DLIB_TEST(a.cast_to<std::string>() == s_val);
exception_thrown = false;
try {a.cast_to<float>(); }
catch (bad_type_safe_union_cast&) { exception_thrown = true;}
DLIB_TEST(exception_thrown);
// -----------
DLIB_TEST(a.is_empty() == false);
DLIB_TEST(a.contains<char>() == false);

View File

@ -13,6 +13,17 @@
namespace dlib
{
// ----------------------------------------------------------------------------------------
class bad_type_safe_union_cast : public std::bad_cast
{
public:
virtual const char * what() const throw()
{
return "bad_type_safe_union_cast";
}
};
// ----------------------------------------------------------------------------------------
struct _void{};
@ -105,7 +116,7 @@ namespace dlib
// --------------------------------------------
template <typename T>
void validate_type()
void validate_type() const
{
// ERROR: You are trying to get a type of object that isn't
// representable by this type_safe_union. I.e. The given
@ -521,6 +532,28 @@ namespace dlib
return *static_cast<T*>(mem.get());
}
template <typename T>
const T& cast_to (
) const
{
validate_type<T>();
if (contains<T>())
return *static_cast<const T*>(mem.get());
else
throw bad_type_safe_union_cast();
}
template <typename T>
T& cast_to (
)
{
validate_type<T>();
if (contains<T>())
return *static_cast<T*>(mem.get());
else
throw bad_type_safe_union_cast();
}
template <typename T>
type_safe_union& operator= ( const T& item) { get<T>() = item; return *this; }

View File

@ -9,6 +9,16 @@
namespace dlib
{
// ----------------------------------------------------------------------------------------
class bad_type_safe_union_cast : public std::bad_cast
{
/*!
This is the exception object thrown by type_safe_union::cast_to() if the
type_safe_union does not contain the type of object being requested.
!*/
};
// ----------------------------------------------------------------------------------------
template <
@ -224,6 +234,32 @@ namespace dlib
- returns a non-const reference to the newly created T object.
!*/
template <typename T>
const T& cast_to (
) const;
/*!
requires
- T must be one of the types given to this object's template arguments
ensures
- if (contains<T>() == true) then
- returns a const reference to the object contained in this type_safe_union.
- else
- throws bad_type_safe_union_cast
!*/
template <typename T>
T& cast_to (
);
/*!
requires
- T must be one of the types given to this object's template arguments
ensures
- if (contains<T>() == true) then
- returns a non-const reference to the object contained in this type_safe_union.
- else
- throws bad_type_safe_union_cast
!*/
template <typename T>
type_safe_union& operator= (
const T& item