Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
OpenDAS
dlib
Commits
ad229442
Commit
ad229442
authored
Oct 12, 2014
by
Davis King
Browse files
Added cast_to() to the type_safe_union. This allows you to get the contents
of a const type_safe_union.
parent
1ee8b5dc
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
83 additions
and
1 deletion
+83
-1
dlib/test/type_safe_union.cpp
dlib/test/type_safe_union.cpp
+13
-0
dlib/type_safe_union/type_safe_union_kernel.h
dlib/type_safe_union/type_safe_union_kernel.h
+34
-1
dlib/type_safe_union/type_safe_union_kernel_abstract.h
dlib/type_safe_union/type_safe_union_kernel_abstract.h
+36
-0
No files found.
dlib/test/type_safe_union.cpp
View file @
ad229442
...
@@ -97,6 +97,13 @@ namespace
...
@@ -97,6 +97,13 @@ namespace
f_val
=
4.345
f
;
f_val
=
4.345
f
;
a
.
get
<
float
>
()
=
f_val
;
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
.
is_empty
()
==
false
);
DLIB_TEST
(
a
.
contains
<
char
>
()
==
false
);
DLIB_TEST
(
a
.
contains
<
char
>
()
==
false
);
...
@@ -134,6 +141,12 @@ namespace
...
@@ -134,6 +141,12 @@ namespace
a
.
apply_to_contents
(
*
this
);
a
.
apply_to_contents
(
*
this
);
DLIB_TEST
(
last_kind
==
STRING
);
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
.
is_empty
()
==
false
);
DLIB_TEST
(
a
.
contains
<
char
>
()
==
false
);
DLIB_TEST
(
a
.
contains
<
char
>
()
==
false
);
...
...
dlib/type_safe_union/type_safe_union_kernel.h
View file @
ad229442
...
@@ -13,6 +13,17 @@
...
@@ -13,6 +13,17 @@
namespace
dlib
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
{};
struct
_void
{};
...
@@ -105,7 +116,7 @@ namespace dlib
...
@@ -105,7 +116,7 @@ namespace dlib
// --------------------------------------------
// --------------------------------------------
template
<
typename
T
>
template
<
typename
T
>
void
validate_type
()
void
validate_type
()
const
{
{
// ERROR: You are trying to get a type of object that isn't
// ERROR: You are trying to get a type of object that isn't
// representable by this type_safe_union. I.e. The given
// representable by this type_safe_union. I.e. The given
...
@@ -521,6 +532,28 @@ namespace dlib
...
@@ -521,6 +532,28 @@ namespace dlib
return
*
static_cast
<
T
*>
(
mem
.
get
());
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
>
template
<
typename
T
>
type_safe_union
&
operator
=
(
const
T
&
item
)
{
get
<
T
>
()
=
item
;
return
*
this
;
}
type_safe_union
&
operator
=
(
const
T
&
item
)
{
get
<
T
>
()
=
item
;
return
*
this
;
}
...
...
dlib/type_safe_union/type_safe_union_kernel_abstract.h
View file @
ad229442
...
@@ -9,6 +9,16 @@
...
@@ -9,6 +9,16 @@
namespace
dlib
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
<
template
<
...
@@ -224,6 +234,32 @@ namespace dlib
...
@@ -224,6 +234,32 @@ namespace dlib
- returns a non-const reference to the newly created T object.
- 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
>
template
<
typename
T
>
type_safe_union
&
operator
=
(
type_safe_union
&
operator
=
(
const
T
&
item
const
T
&
item
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment