diff --git a/dlib/algs.h b/dlib/algs.h index f387fc3da..0fdc77c77 100644 --- a/dlib/algs.h +++ b/dlib/algs.h @@ -480,6 +480,23 @@ namespace dlib // ---------------------------------------------------------------------------------------- + /*!A is_any + + This is a template where is_any::value == true when T is + the same type as any one of the types in Rest... + !*/ + + template + struct is_any : std::false_type {}; + + template + struct is_any : std::is_same {}; + + template + struct is_any : std::integral_constant::value || is_any::value> {}; + +// ---------------------------------------------------------------------------------------- + /*!A is_float_type This is a template that can be used to determine if a type is one of the built diff --git a/dlib/type_safe_union/type_safe_union_kernel.h b/dlib/type_safe_union/type_safe_union_kernel.h index f0ddb6dfe..c20699293 100644 --- a/dlib/type_safe_union/type_safe_union_kernel.h +++ b/dlib/type_safe_union/type_safe_union_kernel.h @@ -62,7 +62,7 @@ namespace dlib CONVENTION - is_empty() == (type_identity == 0) - contains() == (type_identity == get_type_id()) - - mem.get() == the block of memory on the stack which is + - mem == the aligned block of memory on the stack which is where objects in the union are stored !*/ @@ -85,33 +85,11 @@ namespace dlib { } - - const static size_t max_size = tmax::value, - sizeof(T3)>::value, - sizeof(T4)>::value, - sizeof(T5)>::value, - sizeof(T6)>::value, - sizeof(T7)>::value, - sizeof(T8)>::value, - sizeof(T9)>::value, - sizeof(T10)>::value, - sizeof(T11)>::value, - sizeof(T12)>::value, - sizeof(T13)>::value, - sizeof(T14)>::value, - sizeof(T15)>::value, - sizeof(T16)>::value, - sizeof(T17)>::value, - sizeof(T18)>::value, - sizeof(T19)>::value, - sizeof(T20)>::value; - - // -------------------------------------------- - // member data - stack_based_memory_block mem; + typename std::aligned_union<0, T1,T2,T3,T4,T5, + T6,T7,T8,T9,T10, + T11,T12,T13,T14,T15, + T16,T17,T18,T19,T20>::type mem; int type_identity; // -------------------------------------------- @@ -120,32 +98,11 @@ namespace dlib 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 - // type T isn't one of the ones given to this object's template - // arguments. - COMPILE_TIME_ASSERT(( is_same_type::value || - is_same_type::value || - is_same_type::value || - is_same_type::value || - is_same_type::value || - is_same_type::value || - is_same_type::value || - is_same_type::value || - is_same_type::value || - is_same_type::value || - - is_same_type::value || - is_same_type::value || - is_same_type::value || - is_same_type::value || - is_same_type::value || - is_same_type::value || - is_same_type::value || - is_same_type::value || - is_same_type::value || - is_same_type::value - )); - + // representable by this type_safe_union. + static_assert(is_any::value, "Type T isn't one of the ones given to this object's template arguments."); } @@ -179,7 +136,7 @@ namespace dlib if (type_identity != get_type_id()) { destruct(); - new(mem.get()) T(); + new(&mem) T(); type_identity = get_type_id(); } } @@ -193,7 +150,7 @@ namespace dlib if (type_identity != get_type_id()) { destruct(); - new(mem.get()) U(std::forward(item)); + new(&mem) U(std::forward(item)); type_identity = get_type_id(); } } @@ -208,7 +165,7 @@ namespace dlib - returns a non-const reference to the T object !*/ { - return *static_cast(mem.get()); + return *reinterpret_cast(&mem); } template @@ -221,7 +178,7 @@ namespace dlib - returns a const reference to the T object !*/ { - return *static_cast(mem.get()); + return *reinterpret_cast(&mem); } template @@ -287,29 +244,29 @@ namespace dlib static int get_type_id ( ) { - if (is_same_type::value) return 1; - if (is_same_type::value) return 2; - if (is_same_type::value) return 3; - if (is_same_type::value) return 4; - if (is_same_type::value) return 5; + if (std::is_same::value) return 1; + if (std::is_same::value) return 2; + if (std::is_same::value) return 3; + if (std::is_same::value) return 4; + if (std::is_same::value) return 5; - if (is_same_type::value) return 6; - if (is_same_type::value) return 7; - if (is_same_type::value) return 8; - if (is_same_type::value) return 9; - if (is_same_type::value) return 10; + if (std::is_same::value) return 6; + if (std::is_same::value) return 7; + if (std::is_same::value) return 8; + if (std::is_same::value) return 9; + if (std::is_same::value) return 10; - if (is_same_type::value) return 11; - if (is_same_type::value) return 12; - if (is_same_type::value) return 13; - if (is_same_type::value) return 14; - if (is_same_type::value) return 15; + if (std::is_same::value) return 11; + if (std::is_same::value) return 12; + if (std::is_same::value) return 13; + if (std::is_same::value) return 14; + if (std::is_same::value) return 15; - if (is_same_type::value) return 16; - if (is_same_type::value) return 17; - if (is_same_type::value) return 18; - if (is_same_type::value) return 19; - if (is_same_type::value) return 20; + if (std::is_same::value) return 16; + if (std::is_same::value) return 17; + if (std::is_same::value) return 18; + if (std::is_same::value) return 19; + if (std::is_same::value) return 20; // return a number that doesn't match any of the // valid states of type_identity @@ -538,7 +495,7 @@ namespace dlib { validate_type(); construct(); - return *static_cast(mem.get()); + return *reinterpret_cast(&mem); } template @@ -547,7 +504,7 @@ namespace dlib { validate_type(); if (contains()) - return *static_cast(mem.get()); + return *reinterpret_cast(&mem); else throw bad_type_safe_union_cast(); } @@ -558,7 +515,7 @@ namespace dlib { validate_type(); if (contains()) - return *static_cast(mem.get()); + return *reinterpret_cast(&mem); else throw bad_type_safe_union_cast(); }