Commit fdf01772 authored by Davis King's avatar Davis King

Made the get_id() function public and renamed it to get_type_id(). I also

added a comment explaining the serialization format of type_safe_union objects.

--HG--
extra : convert_revision : svn%3Afdd8eb12-d10e-0410-9acb-85c331704f74/trunk%403451
parent a2798bc8
......@@ -75,6 +75,12 @@ namespace
DLIB_TEST(a.contains<std::string>() == false);
DLIB_TEST(a.contains<long>() == false);
DLIB_TEST(a.get_type_id<int>() == -1);
DLIB_TEST(a.get_type_id<float>() == 1);
DLIB_TEST(a.get_type_id<double>() == 2);
DLIB_TEST(a.get_type_id<char>() == 3);
DLIB_TEST(a.get_type_id<std::string>() == 4);
DLIB_TEST(a.get_type_id<tsu>() == -1);
f_val = 4.345f;
......
......@@ -42,7 +42,7 @@ namespace dlib
/*!
CONVENTION
- is_empty() == (type_identity == 0)
- contains<T>() == (type_identity == get_id<T>())
- contains<T>() == (type_identity == get_type_id<T>())
- mem.get() == the block of memory on the stack which is
where objects in the union are stored
!*/
......@@ -71,10 +71,14 @@ namespace dlib
sizeof(T19)>::value,
sizeof(T20)>::value;
// --------------------------------------------
// member data
stack_based_memory_block<max_size> mem;
int type_identity;
// --------------------------------------------
int type_identity;
struct destruct_helper
{
......@@ -99,48 +103,15 @@ namespace dlib
type_identity = 0;
}
template <typename T>
int get_id (
) const
{
if (is_same_type<T,T1>::value) return 1;
if (is_same_type<T,T2>::value) return 2;
if (is_same_type<T,T3>::value) return 3;
if (is_same_type<T,T4>::value) return 4;
if (is_same_type<T,T5>::value) return 5;
if (is_same_type<T,T6>::value) return 6;
if (is_same_type<T,T7>::value) return 7;
if (is_same_type<T,T8>::value) return 8;
if (is_same_type<T,T9>::value) return 9;
if (is_same_type<T,T10>::value) return 10;
if (is_same_type<T,T11>::value) return 11;
if (is_same_type<T,T12>::value) return 12;
if (is_same_type<T,T13>::value) return 13;
if (is_same_type<T,T14>::value) return 14;
if (is_same_type<T,T15>::value) return 15;
if (is_same_type<T,T16>::value) return 16;
if (is_same_type<T,T17>::value) return 17;
if (is_same_type<T,T18>::value) return 18;
if (is_same_type<T,T19>::value) return 19;
if (is_same_type<T,T20>::value) return 20;
// return a number that doesn't match any of the
// valid states of type_identity
return 10000;
}
template <typename T>
void construct (
)
{
if (type_identity != get_id<T>())
if (type_identity != get_type_id<T>())
{
destruct();
new(mem.get()) T();
type_identity = get_id<T>();
type_identity = get_type_id<T>();
}
}
......@@ -165,11 +136,44 @@ namespace dlib
destruct();
}
template <typename T>
static int get_type_id (
)
{
if (is_same_type<T,T1>::value) return 1;
if (is_same_type<T,T2>::value) return 2;
if (is_same_type<T,T3>::value) return 3;
if (is_same_type<T,T4>::value) return 4;
if (is_same_type<T,T5>::value) return 5;
if (is_same_type<T,T6>::value) return 6;
if (is_same_type<T,T7>::value) return 7;
if (is_same_type<T,T8>::value) return 8;
if (is_same_type<T,T9>::value) return 9;
if (is_same_type<T,T10>::value) return 10;
if (is_same_type<T,T11>::value) return 11;
if (is_same_type<T,T12>::value) return 12;
if (is_same_type<T,T13>::value) return 13;
if (is_same_type<T,T14>::value) return 14;
if (is_same_type<T,T15>::value) return 15;
if (is_same_type<T,T16>::value) return 16;
if (is_same_type<T,T17>::value) return 17;
if (is_same_type<T,T18>::value) return 18;
if (is_same_type<T,T19>::value) return 19;
if (is_same_type<T,T20>::value) return 20;
// return a number that doesn't match any of the
// valid states of type_identity
return -1;
}
template <typename T>
bool contains (
) const
{
return type_identity == get_id<T>();
return type_identity == get_type_id<T>();
}
bool is_empty (
......
......@@ -75,6 +75,18 @@ namespace dlib
- all resources associated with this object have been freed
!*/
template <typename T>
static int get_type_id (
);
/*!
ensures
- if (T is the same type as one of the template arguments) then
- returns a number indicating which template argument it is.
(e.g. if T is the same type as T3 then this function returns 3)
- else
- returns -1
!*/
template <typename T>
bool contains (
) const;
......@@ -175,6 +187,13 @@ namespace dlib
);
/*!
provides serialization support
Note that type_safe_union objects are serialized as follows:
- if (item.is_empty()) then
- perform: serialize(0, out)
- else
- perform: serialize(item.get_type_id<type_of_object_in_item>(), out);
serialize(item.get<type_of_object_in_item>(), out);
!*/
template < ... >
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment