| 1 | // helper.hpp, created on 04.11.2008 by Sebastian Mies
|
---|
| 2 | //
|
---|
| 3 | // [The FreeBSD Licence]
|
---|
| 4 | // Copyright (c) 2008
|
---|
| 5 | // Sebastian Mies, Institute of Telematics, UniversitÀt Karlsruhe (TH)
|
---|
| 6 | // All rights reserved.
|
---|
| 7 | //
|
---|
| 8 | // Redistribution and use in source and binary forms, with or without
|
---|
| 9 | // modification, are permitted provided that the following conditions are
|
---|
| 10 | // met:
|
---|
| 11 | //
|
---|
| 12 | // * Redistributions of source code must retain the above copyright notice,
|
---|
| 13 | // this list of conditions and the following disclaimer.
|
---|
| 14 | // * Redistributions in binary form must reproduce the above copyright
|
---|
| 15 | // notice, this list of conditions and the following disclaimer in the
|
---|
| 16 | // documentation and/or other materials provided with the distribution.
|
---|
| 17 | // * Neither the name of the author nor the names of its contributors may be
|
---|
| 18 | // used to endorse or promote products derived from this software without
|
---|
| 19 | // specific prior written permission.
|
---|
| 20 | //
|
---|
| 21 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
|
---|
| 22 | // IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
|
---|
| 23 | // THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
---|
| 24 | // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
|
---|
| 25 | // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
---|
| 26 | // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
---|
| 27 | // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
|
---|
| 28 | // OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
|
---|
| 29 | // WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
|
---|
| 30 | // OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
|
---|
| 31 | // ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
| 32 | //
|
---|
| 33 | #ifndef HELPER_HPP_
|
---|
| 34 | #define HELPER_HPP_
|
---|
| 35 |
|
---|
| 36 | #include <boost/cstdint.hpp>
|
---|
| 37 | #include <boost/type_traits.hpp>
|
---|
| 38 | #include <boost/utility/enable_if.hpp>
|
---|
| 39 | #include <boost/mpl/if.hpp>
|
---|
| 40 |
|
---|
| 41 | /* force inline macro */
|
---|
| 42 | #ifndef finline
|
---|
| 43 | #define finline inline __attribute__((always_inline))
|
---|
| 44 | #endif
|
---|
| 45 |
|
---|
| 46 | /* check whether X is a base class of Y */
|
---|
| 47 | #define if_is_base_of(X,Y) \
|
---|
| 48 | typename boost::enable_if<boost::is_base_of<X, Y> , int>::type __v = 0
|
---|
| 49 |
|
---|
| 50 | /* check whether X is a integer */
|
---|
| 51 | #define if_integral(X) \
|
---|
| 52 | typename boost::enable_if<boost::is_integral<X>,int>::type __i##X = 0
|
---|
| 53 |
|
---|
| 54 | /* check whether X is a unsigned integer */
|
---|
| 55 | #define if_uint(X) \
|
---|
| 56 | typename boost::enable_if<boost::is_integral<X>,int>::type __i##X = 0,\
|
---|
| 57 | typename boost::enable_if<boost::is_unsigned<X>,int>::type __u##X = 0
|
---|
| 58 |
|
---|
| 59 | /* check whether X is a signed integer */
|
---|
| 60 | #define if_int(X) \
|
---|
| 61 | typename boost::enable_if<boost::is_integral<X>,int>::type __i##X = 0,\
|
---|
| 62 | typename boost::enable_if<boost::is_signed<X>,int>::type __s##X = 0
|
---|
| 63 |
|
---|
| 64 | /* signature conversion */
|
---|
| 65 | #define CONVERT_SIGN(X,Y) \
|
---|
| 66 | finline X& _unsigned( Y& v ) { return *((X*)&v); } \
|
---|
| 67 | finline X& _unsigned( X& v ) { return v; } \
|
---|
| 68 | finline Y& _signed( X& v ) { return *((Y*)&v); } \
|
---|
| 69 | finline Y& _signed( Y& v ) { return v; }
|
---|
| 70 |
|
---|
| 71 | CONVERT_SIGN( uint8_t, int8_t );
|
---|
| 72 | CONVERT_SIGN( uint16_t, int16_t );
|
---|
| 73 | CONVERT_SIGN( uint32_t, int32_t );
|
---|
| 74 |
|
---|
| 75 | /* bijective integer conversion */
|
---|
| 76 | #define CONVERT_TO_TYPE( __method, __type ) \
|
---|
| 77 | template<class T> __type& __method(T& x, typename \
|
---|
| 78 | boost::enable_if< boost::mpl::bool_<sizeof(T) == sizeof(__type)>, int> \
|
---|
| 79 | ::type __ix = 0) { return (__type&) *(&x);}
|
---|
| 80 |
|
---|
| 81 | CONVERT_TO_TYPE( _uint, uint8_t )
|
---|
| 82 | CONVERT_TO_TYPE( _uint, uint16_t )
|
---|
| 83 | CONVERT_TO_TYPE( _uint, uint32_t )
|
---|
| 84 |
|
---|
| 85 | CONVERT_TO_TYPE( _int , int8_t )
|
---|
| 86 | CONVERT_TO_TYPE( _int , int16_t )
|
---|
| 87 | CONVERT_TO_TYPE( _int , int32_t )
|
---|
| 88 |
|
---|
| 89 | #endif /* HELPER_HPP_ */
|
---|