| 1 | |
| 2 | // Copyright (c) 2003-2019 Xsens Technologies B.V. or subsidiaries worldwide. |
| 3 | // All rights reserved. |
| 4 | // |
| 5 | // Redistribution and use in source and binary forms, with or without modification, |
| 6 | // are permitted provided that the following conditions are met: |
| 7 | // |
| 8 | // 1. Redistributions of source code must retain the above copyright notice, |
| 9 | // this list of conditions, and the following disclaimer. |
| 10 | // |
| 11 | // 2. Redistributions in binary form must reproduce the above copyright notice, |
| 12 | // this list of conditions, and the following disclaimer in the documentation |
| 13 | // and/or other materials provided with the distribution. |
| 14 | // |
| 15 | // 3. Neither the names of the copyright holders nor the names of their contributors |
| 16 | // may be used to endorse or promote products derived from this software without |
| 17 | // specific prior written permission. |
| 18 | // |
| 19 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY |
| 20 | // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF |
| 21 | // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL |
| 22 | // THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 23 | // SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT |
| 24 | // OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 25 | // HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY OR |
| 26 | // TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 27 | // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.THE LAWS OF THE NETHERLANDS |
| 28 | // SHALL BE EXCLUSIVELY APPLICABLE AND ANY DISPUTES SHALL BE FINALLY SETTLED UNDER THE RULES |
| 29 | // OF ARBITRATION OF THE INTERNATIONAL CHAMBER OF COMMERCE IN THE HAGUE BY ONE OR MORE |
| 30 | // ARBITRATORS APPOINTED IN ACCORDANCE WITH SAID RULES. |
| 31 | // |
| 32 | |
| 33 | #ifndef ALIASCAST_H |
| 34 | #define ALIASCAST_H |
| 35 | |
| 36 | #ifdef __cplusplus |
| 37 | #ifndef XSENS_MATH_FIRMWARE |
| 38 | |
| 39 | #include <type_traits> |
| 40 | |
| 41 | namespace Internal |
| 42 | { |
| 43 | /*! \class AliasCaster |
| 44 | Alias caster can be used to reinterpret a memory locatation as a different type without breaking strict aliasing rules. |
| 45 | For example casting a pointer to a struct to a pointer to a uint16_t. |
| 46 | AliasCaster only supports casting pointers. |
| 47 | Example: |
| 48 | MyStruct myStruct; |
| 49 | uint16_t* myUint16ptr = AliasCaster<uint16_t*, MyStruct>(&myStruct); |
| 50 | uint16_t firstUint = *myUint16ptr; |
| 51 | The helper function alias_cast (see below) is provided for easier usage |
| 52 | \note Strictly speaking, according to the standard, assigning to alias.from and the reading alias.to is undefined behaviour |
| 53 | \note Putting only pointers in the union an invalid solution according to the link below |
| 54 | http://cellperformance.beyond3d.com/articles/2006/06/understanding-strict-aliasing.html |
| 55 | http://blog.regehr.org/archives/959 |
| 56 | |
| 57 | The chosen solution is to cast the source pointer to a pointer to a union of the 'base' types of the source and destination pointers |
| 58 | and then return the adress of the destination field of the union. |
| 59 | */ |
| 60 | template <typename U, typename V> |
| 61 | class AliasCaster { |
| 62 | public: |
| 63 | typedef U to_type; //!< This is a pointer to type |
| 64 | typedef V from_type; //!< This is a pointer from type |
| 65 | typedef typename std::remove_pointer<to_type>::type base_to_type; //!< This is a base pointer to type |
| 66 | typedef typename std::remove_pointer<from_type>::type base_from_type; //!< This is a base pointer from type |
| 67 | |
| 68 | static_assert(std::is_pointer<to_type>::value && std::is_pointer<from_type>::value, "alias_caster requires pointer types as arguments"); |
| 69 | static_assert(!std::is_const<base_from_type>::value || (std::is_const<base_from_type>::value && std::is_const<base_to_type>::value), "alias_caster requires a const destination type here"); |
| 70 | |
| 71 | static to_type cast(from_type from) {return &(reinterpret_cast<AliasType*>(from)->to);} //!< This is a pointer type cast |
| 72 | private: |
| 73 | // The union to cast the from_type to |
| 74 | typedef union { |
| 75 | base_from_type from; |
| 76 | base_to_type to; |
| 77 | } AliasUnion; |
| 78 | |
| 79 | // Depending on the const-ness of the from_type, we need to cast to a const union or not |
| 80 | typedef typename std::conditional< |
| 81 | std::is_const<base_from_type>::value, |
| 82 | typename std::add_const<AliasUnion>::type, |
| 83 | AliasUnion |
| 84 | >::type AliasType; |
| 85 | }; |
| 86 | } // namespace Internal |
| 87 | |
| 88 | /*! Casts one pointer to another without breaking strict aliasing rules. |
| 89 | \see Internal::AliasCaster |
| 90 | Example: |
| 91 | MyStruct myStruct; |
| 92 | uint16_t* myUint16ptr = alias_cast<uint16_t*>(&myStruct); |
| 93 | uint16_t firstUint = *myUint16ptr; |
| 94 | */ |
| 95 | template <typename U, typename V> |
| 96 | U alias_cast(V fromPtr) { |
| 97 | return Internal::AliasCaster<U, V>::cast(fromPtr); |
| 98 | } |
| 99 | |
| 100 | #endif // XSENS_MATH_FIRMWARE |
| 101 | |
| 102 | #endif // __cplusplus |
| 103 | |
| 104 | #endif // ALIASCAST_H |