Changeset 373 for trunk


Ignore:
Timestamp:
05/25/15 07:31:57 (10 years ago)
Author:
epyon
Message:
  • further type_traits implementations
  • we no longer depend on STL type_traits
Location:
trunk/nv
Files:
6 edited

Legend:

Unmodified
Added
Removed
  • trunk/nv/core/common.hh

    r372 r373  
    230230        };
    231231
     232        template <typename TYPE>
     233        void construct_object( void* object )
     234        {
     235                new (object)TYPE;
     236        }
     237        template <typename TYPE>
     238        void destroy_object( void* object )
     239        {
     240                ( (TYPE*)object )->TYPE::~TYPE();
     241        }
    232242
    233243} // namespace nv
  • trunk/nv/core/types.hh

    r372 r373  
    1010#include <nv/stl/cstring_store.hh>
    1111#include <nv/stl/type_traits.hh>
     12#include <nv/stl/type_info.hh>
    1213#include <unordered_map>
    1314#include <vector>
     
    8586
    8687                template< typename TOBJECT, typename TFIELD >
    87                 type_creator field( const char* aname, TFIELD TOBJECT::*field, typename std::enable_if< is_container<TFIELD>::value, void* >::type = nullptr );
     88                type_creator field( const char* aname, TFIELD TOBJECT::*field, typename enable_if< is_container<TFIELD>::value, void* >::type = nullptr );
    8889
    8990                template< typename TOBJECT, typename TFIELD >
    90                 type_creator field( const char* aname, TFIELD TOBJECT::*field, typename std::enable_if< !is_container<TFIELD>::value, void* >::type = nullptr );
     91                type_creator field( const char* aname, TFIELD TOBJECT::*field, typename enable_if< !is_container<TFIELD>::value, void* >::type = nullptr );
    9192
    9293                type_creator value( const char* name, sint32 value );
     
    161162
    162163        template< typename TOBJECT, typename TFIELD >
    163         type_creator type_creator::field( const char* aname, TFIELD TOBJECT::*field, typename std::enable_if< is_container<TFIELD>::value, void* >::type )
     164        type_creator type_creator::field( const char* aname, TFIELD TOBJECT::*field, typename enable_if< is_container<TFIELD>::value, void* >::type )
    164165        {
    165166                NV_ASSERT( m_entry->enum_list.empty(), "Type cannot have both enums and fields!" );
    166167                type_field f;
    167168                f.name = m_entry->names.insert( name );
    168                 f.raw_type = &typeid( typename std::remove_pointer<typename TFIELD::value_type>::type );
     169                f.raw_type = &typeid( typename remove_pointer<typename TFIELD::value_type>::type );
    169170                f.type = type_db->get_type( *( f.raw_type ) );
    170171                f.flags = TF_CONTAINER |
    171                         ( std::is_pointer<typename TFIELD::value_type>::value ? TF_POINTER : 0 ) |
    172                         ( std::is_pod<typename TFIELD::value_type>::value ? TF_SIMPLETYPE : 0 );
     172                        ( is_pointer<typename TFIELD::value_type>::value ? TF_POINTER : 0 ) |
     173                        ( is_pod<typename TFIELD::value_type>::value ? TF_SIMPLETYPE : 0 );
    173174                f.offset = offset_of( field );
    174175                m_entry->field_list.push_back( f );
     
    177178
    178179        template< typename TOBJECT, typename TFIELD >
    179         type_creator type_creator::field( const char* aname, TFIELD TOBJECT::*field, typename std::enable_if< !is_container<TFIELD>::value, void* >::type )
     180        type_creator type_creator::field( const char* aname, TFIELD TOBJECT::*field, typename enable_if< !is_container<TFIELD>::value, void* >::type )
    180181        {
    181182                NV_ASSERT( m_entry->enum_list.empty(), "Type cannot have both enums and fields!" );
    182183                type_field f;
    183184                f.name = m_entry->names.insert( name );
    184                 f.raw_type = &typeid( typename std::remove_pointer<TFIELD>::type );
     185                f.raw_type = &typeid( typename remove_pointer<TFIELD>::type );
    185186                f.type = type_db->get_type( *( f.raw_type ) );
    186187                f.flags =
    187                         ( std::is_pointer<TFIELD>::value ? TF_POINTER : 0 ) |
    188                         ( std::is_pod<TFIELD>::value ? TF_SIMPLETYPE : 0 );
     188                        ( is_pointer<TFIELD>::value ? TF_POINTER : 0 ) |
     189                        ( is_pod<TFIELD>::value ? TF_SIMPLETYPE : 0 );
    189190                f.offset = offset_of( field );
    190191                m_entry->field_list.push_back( f );
  • trunk/nv/lua/lua_values.hh

    r368 r373  
    178178                        struct type_degrade
    179179                        {
    180                                 typedef typename std::remove_cv< typename std::remove_reference<T>::type >::type type;
    181                         };
    182 
    183                         template <typename T>
    184                         struct type_degrade< T, typename std::enable_if< std::is_floating_point< T >::value >::type >
     180                                typedef typename remove_cv< typename remove_reference<T>::type >::type type;
     181                        };
     182
     183                        template <typename T>
     184                        struct type_degrade< T, typename enable_if< is_floating_point< T >::value >::type >
    185185                        {
    186186                                typedef lnumber type;
     
    188188
    189189                        template <typename T>
    190                         struct type_degrade< T, typename std::enable_if< std::is_integral< T >::value && std::is_signed< T >::value >::type >
     190                        struct type_degrade< T, typename enable_if< is_integral< T >::value && is_signed< T >::value >::type >
    191191                        {
    192192                                typedef linteger type;
     
    194194
    195195                        template <typename T>
    196                         struct type_degrade< T, typename std::enable_if< std::is_integral< T >::value && std::is_unsigned< T >::value >::type >
     196                        struct type_degrade< T, typename enable_if< is_integral< T >::value && is_unsigned< T >::value >::type >
    197197                        {
    198198                                typedef lunsigned type;
     
    200200
    201201                        template <typename T>
    202                         struct type_degrade< T, typename std::enable_if< is_cstring< T >::value >::type >
     202                        struct type_degrade< T, typename enable_if< is_cstring< T >::value >::type >
    203203                        {
    204204                                typedef const char* type;
     
    206206
    207207                        template <typename T>
    208                         struct type_degrade< T, typename std::enable_if< is_stdstring< T >::value >::type >
     208                        struct type_degrade< T, typename enable_if< is_stdstring< T >::value >::type >
    209209                        {
    210210                                typedef std::string type;
  • trunk/nv/stl/string.hh

    r372 r373  
    2727#include <fstream>
    2828#include <nv/core/common.hh>
     29#include <nv/stl/type_traits.hh>
    2930#include <nv/stl/memory.hh>
    3031#include <nv/stl/exception.hh>
     
    285286        template< typename T >
    286287        struct string_length : public detail::string_length_impl <
    287                 typename std::remove_cv < typename std::remove_reference< T >::type >::type >
     288                typename remove_cv < typename remove_reference< T >::type >::type >
    288289        {
    289290        };
     
    291292        template< typename T >
    292293        using string_length = detail::string_length_impl <
    293                 typename std::remove_cv < typename std::remove_reference< T >::type >::type >;
     294                typename remove_cv < typename remove_reference< T >::type >::type >;
    294295#endif
    295296
     
    298299        struct is_cstring
    299300                : public integral_constant < bool,
    300                 is_same<       char *, typename std::decay< T >::type >::value ||
    301                 is_same< const char *, typename std::decay< T >::type >::value >
     301                is_same<       char *, typename decay< T >::type >::value ||
     302                is_same< const char *, typename decay< T >::type >::value >
    302303        {
    303304        };
     
    306307        struct is_stdstring
    307308                : public integral_constant < bool,
    308                 is_same< std::string, typename std::decay< T >::type >::value
     309                is_same< std::string, typename remove_cvr< T >::type >::value
    309310                >
    310311        {
     
    505506                // Non-literal constructors
    506507                template< typename U >
    507                 inline string_ref( U str, typename std::enable_if<std::is_same<U, const char*>::value>::type* = nullptr ) : string_base( str, std::strlen( str ) ) {}
     508                inline string_ref( U str, typename enable_if<is_same<U, const char*>::value>::type* = nullptr ) : string_base( str, std::strlen( str ) ) {}
    508509
    509510                // Non-literal constructors
    510511                template< typename U >
    511                 inline string_ref( U str, typename std::enable_if<std::is_same<U, char*>::value>::type* = nullptr ) : string_base( str, std::strlen( str ) ) {}
     512                inline string_ref( U str, typename enable_if<is_same<U, char*>::value>::type* = nullptr ) : string_base( str, std::strlen( str ) ) {}
    512513
    513514                inline string_ref& operator=( const string_ref &rhs )
  • trunk/nv/stl/type_traits.hh

    r372 r373  
    1 // Copyright (C) 2012-2014 ChaosForge Ltd
     1// Copyright (C) 2012-2015 ChaosForge Ltd
    22// http://chaosforge.org/
    33//
     
    99 * @brief type traits
    1010 */
    11 // TODO: function_traits support only up to 4 parameters because:
    12 //    -- if you have more than 4 params, you're doing something wrong
    13 //    -- once the Variadic Templates cometh, for great justice we will win!
    14 
    15 #ifndef NV_CORE_TYPE_TRAITS_HH
    16 #define NV_CORE_TYPE_TRAITS_HH
     11// TODO: "......" function match version?
     12// TODO: remove type_traits
     13// TODO: remove typeinfo?
     14
     15#ifndef NV_STL_TYPE_TRAITS_HH
     16#define NV_STL_TYPE_TRAITS_HH
    1717
    1818#include <nv/core/common.hh>
    19 #include <type_traits>
    20 #include <typeinfo>
    2119
    2220namespace nv
     
    2927                typedef T value_type;
    3028                typedef integral_constant<T, VALUE> type;
    31                 operator value_type() const { return ( value ); }
     29                NV_CONSTEXPR operator value_type() const { return ( value ); }
    3230        };
    3331
     
    6058
    6159        template< typename T >
    62         struct is_same < T, T > : true_type{};
     60        struct is_same < T, T > : true_type {};
     61
     62        template< typename T >
     63        struct is_array : false_type {};
     64
     65        template< typename T, size_t N >
     66        struct is_array < T[N] > : true_type {};
     67
     68        template< typename T >
     69        struct is_array < T[] > : true_type {};
    6370
    6471        template< typename T >
     
    7683        template < typename T >
    7784        struct is_enum : integral_constant < bool, __is_enum( T ) > {};
     85
     86        template < typename T >
     87        struct is_pod : integral_constant < bool, __is_pod( T ) > {};
    7888
    7989        template< typename T >
     
    150160        };
    151161
     162        template< typename T >
     163        struct remove_cvr
     164        {
     165                typedef typename remove_cv< typename remove_reference<T>::type >::type
     166                        type;
     167        };
     168
     169        template< typename T >
     170        struct remove_pointer
     171        {
     172                typedef T type;
     173        };
     174
     175        template< typename T > struct remove_pointer < T* >                { typedef T type; };
     176        template< typename T > struct remove_pointer < T* const >          { typedef T type; };
     177        template< typename T > struct remove_pointer < T* volatile >       { typedef T type; };
     178        template< typename T > struct remove_pointer < T* const volatile > { typedef T type; };
     179
     180        template< typename T >
     181        struct remove_extent
     182        {       
     183                typedef T type;
     184        };
     185
     186        template< typename T, unsigned int N >
     187        struct remove_extent < T[N] >
     188        {
     189                typedef T type;
     190        };
     191
     192        template< typename T >
     193        struct remove_extent < T[] >
     194        {
     195                typedef T type;
     196        };
     197
     198        template< typename T >
     199        struct remove_all_extents
     200        {
     201                typedef T type;
     202        };
     203
     204        template< typename T, unsigned int N >
     205        struct remove_all_extents < T[N] >
     206        {
     207                typedef typename remove_all_extents<T>::type type;
     208        };
     209
     210        template< typename T >
     211        struct remove_all_extents < T[] >
     212        {
     213                typedef typename remove_all_extents<T>::type type;
     214        };
     215
     216        template< typename T >
     217        struct add_pointer
     218        {
     219                typedef typename remove_reference<T>::type *type;
     220        };
     221
    152222        namespace detail
    153223        {
     
    155225                // detail - research why it is so.
    156226                template < typename T >
    157                 struct is_const : public false_type {};
    158                 template < typename T >
    159                 struct is_const < T const > : public true_type {};
     227                struct is_const : false_type {};
     228                template < typename T >
     229                struct is_const < T const > : true_type {};
    160230                template < typename T >
    161                 struct is_volatile : public false_type {};
    162                 template < typename T >
    163                 struct is_volatile < T volatile > : public true_type {};
     231                struct is_volatile : false_type {};
     232                template < typename T >
     233                struct is_volatile < T volatile > : true_type {};
    164234                // TODO END
    165235
     
    186256                        typedef typename cv_selector< TARGET, CONST, VOLATILE >::type type;
    187257                };
     258
     259                template < typename T >
     260                struct is_void_impl : false_type{};
     261
     262                template <>
     263                struct is_void_impl< void > : true_type {};
    188264
    189265                template< typename T >
     
    287363        }
    288364
     365        template <typename T> struct is_signed : false_type {};
     366
     367        template <> struct is_signed<signed char> : true_type {};
     368        template <> struct is_signed<const signed char> : true_type {};
     369        template <> struct is_signed<signed short> : true_type {};
     370        template <> struct is_signed<const signed short> : true_type {};
     371        template <> struct is_signed<signed int> : true_type {};
     372        template <> struct is_signed<const signed int> : true_type {};
     373        template <> struct is_signed<signed long> : true_type {};
     374        template <> struct is_signed<const signed long> : true_type {};
     375        template <> struct is_signed<sint64> : true_type {};
     376        template <> struct is_signed<const sint64> : true_type{};
     377
     378        template <typename T> struct is_unsigned : false_type {};
     379
     380        template <> struct is_unsigned<unsigned char> : true_type{};
     381        template <> struct is_unsigned<const unsigned char> : true_type{};
     382        template <> struct is_unsigned<unsigned short> : true_type{};
     383        template <> struct is_unsigned<const unsigned short> : true_type{};
     384        template <> struct is_unsigned<unsigned int> : true_type{};
     385        template <> struct is_unsigned<const unsigned int> : true_type{};
     386        template <> struct is_unsigned<unsigned long> : true_type{};
     387        template <> struct is_unsigned<const unsigned long> : true_type{};
     388        template <> struct is_unsigned<uint64> : true_type{};
     389        template <> struct is_unsigned<const uint64> : true_type{};
     390
     391        template <> struct is_signed<char> : integral_constant<bool, ( char( 0 ) > char( -1 ) ) >{};
     392        template <> struct is_unsigned<char> : integral_constant<bool, ( char( 0 ) < char( -1 ) ) > {};
     393
     394
    289395        template < typename T >
    290396        struct make_signed
     
    303409        template <> struct make_unsigned < bool > ;
    304410
     411        template< typename T >
     412        struct is_void : detail::is_void_impl < typename remove_cv<T>::type >
     413        {
     414        };
    305415
    306416        template< typename T >
     
    311421        template< typename T >
    312422        struct is_floating_point : detail::is_floating_point_impl< typename remove_cv<T>::type >
     423        {
     424        };
     425
     426        template < typename T >
     427        struct is_arithmetic : integral_constant < bool,
     428                is_integral<T>::value || is_floating_point<T>::value >
     429        {
     430        };
     431
     432        template <typename T>
     433        struct is_fundamental : integral_constant < bool,
     434                is_void<T>::value || is_integral<T>::value || is_floating_point<T>::value >
    313435        {
    314436        };
     
    377499                };
    378500
     501                template< typename T, typename IS_ENUM >
     502                struct base_underlying_type_impl
     503                {
     504                        typedef T type;
     505                };
     506
     507                template< typename T >
     508                struct base_underlying_type_impl < T, true_type >
     509                {
     510                        typedef typename underlying_type<T>::type type;
     511                };
     512
     513                template < typename T > struct is_pointer_impl : false_type {};
     514
     515                template < typename T > struct is_pointer_impl< T* > : true_type{};
     516                template < typename T > struct is_pointer_impl< T* const > : true_type{};
     517                template < typename T > struct is_pointer_impl< T* volatile > : true_type{};
     518                template < typename T > struct is_pointer_impl< T* const volatile > : true_type{};
     519
    379520        }
    380521
    381522        template < typename F >
    382         struct function_traits : public detail::function_traits_impl< F >
     523        struct function_traits : detail::function_traits_impl< F >
    383524        {
    384525
     
    397538        };
    398539
    399         template <typename TYPE>
    400         void construct_object(void* object)
    401         {
    402                 new (object) TYPE;
    403         }
    404         template <typename TYPE>
    405         void destroy_object(void* object)
    406         {
    407                 ((TYPE*)object)->TYPE::~TYPE();
    408         }
    409 
    410         template< typename T, typename IS_ENUM >
    411         struct base_underlying_type_helper
    412         {
    413                 typedef T type;
    414         };
    415 
    416         template< typename T >
    417         struct base_underlying_type_helper< T, true_type >
    418         {
    419                 typedef typename underlying_type<T>::type type;
    420         };
    421 
    422 
    423         template< typename T >
     540        template < typename T >
    424541        struct base_underlying_type
    425542        {
    426                 typedef typename base_underlying_type_helper< T, typename is_enum<T>::type >::type type;
    427         };
    428 
    429         class type_index
    430         {
    431         public:
    432                 inline type_index( const std::type_info& info ) : m_type_info( &info ) {}
    433                 inline size_t hash_code() const { return m_type_info->hash_code(); }
    434                 inline const char *name() const { return m_type_info->name(); }
    435 
    436                 inline bool operator==( const type_index& rhs ) const
    437                 {
    438                         return ( *m_type_info == *rhs.m_type_info );
    439                 }
    440 
    441                 inline bool operator!=( const type_index& rhs ) const
    442                 {
    443                         return ( !( *this == rhs ) );
    444                 }
    445 
    446                 inline bool operator<( const type_index& rhs ) const
    447                 {
    448                         return ( m_type_info->before( *rhs.m_type_info ) );
    449                 }
    450 
    451                 inline bool operator>=( const type_index& rhs ) const
    452                 {
    453                         return ( !( *this < rhs ) );
    454                 }
    455 
    456                 inline bool operator>( const type_index& rhs ) const
    457                 {
    458                         return ( rhs < *this );
    459                 }
    460 
    461                 inline bool operator<=( const type_index& rhs ) const
    462                 {
    463                         return ( !( rhs < *this ) );
    464                 }
    465 
    466         private:
    467                 const std::type_info* m_type_info;
     543                typedef typename detail::base_underlying_type_impl< T, typename is_enum<T>::type >::type type;
     544        };
     545
     546        template < typename F > struct is_function_pointer : false_type {};
     547#if NV_COMPILER == NV_MSVC
     548        template < typename R, typename... Args > struct is_function_pointer< R( __cdecl * )( Args... ) > : true_type{};
     549        template < typename R, typename... Args > struct is_function_pointer< R( __stdcall * )( Args... ) > : true_type{};
     550        template < typename R, typename... Args > struct is_function_pointer< R( __fastcall * )( Args... ) > : true_type{};
     551        template < typename R, typename... Args > struct is_function_pointer< R( __vectorcall * )( Args... ) > : true_type{};
     552#else
     553        template < typename R, typename... Args > struct is_function_pointer< R( *)( Args... ) > : true_type{};
     554#endif
     555
     556        template < typename C > struct is_member_function_pointer : false_type {};
     557#if NV_COMPILER == NV_MSVC
     558#define NV_IS_MEMFNPTR( call_conv ) \
     559        template < typename R, typename C, typename... Args > struct is_member_function_pointer< R( call_conv C::* )( Args... ) > : true_type{}; \
     560        template < typename R, typename C, typename... Args > struct is_member_function_pointer< R( call_conv C::* )( Args... ) const > : true_type{}; \
     561        template < typename R, typename C, typename... Args > struct is_member_function_pointer< R( call_conv C::* )( Args... ) volatile > : true_type{}; \
     562        template < typename R, typename C, typename... Args > struct is_member_function_pointer< R( call_conv C::* )( Args... ) const volatile > : true_type{};
     563
     564NV_IS_MEMFNPTR( __thiscall )
     565NV_IS_MEMFNPTR( __cdecl )
     566NV_IS_MEMFNPTR( __stdcall )
     567NV_IS_MEMFNPTR( __fastcall )
     568NV_IS_MEMFNPTR( __vectorcall )
     569
     570#undef NV_IS_MEMFNPTR
     571#else
     572        template < typename R, typename C, typename... Args > struct is_member_function_pointer< R( C::* )( Args... ) > : true_type{};
     573        template < typename R, typename C, typename... Args > struct is_member_function_pointer< R( C::* )( Args... ) const > : true_type{};
     574        template < typename R, typename C, typename... Args > struct is_member_function_pointer< R( C::* )( Args... ) volatile > : true_type{};
     575        template < typename R, typename C, typename... Args > struct is_member_function_pointer< R( C::* )( Args... ) const volatile > : true_type{};
     576#endif
     577
     578        template < typename F > struct is_member_pointer : integral_constant < bool, is_member_function_pointer<F>::value > {};
     579        template < typename C, typename R > struct is_member_pointer<R C::*> : true_type {};
     580
     581        template < typename T >
     582        struct is_pointer : integral_constant< bool, (detail::is_pointer_impl<T>::value) && !(is_member_pointer<T>::value) > {};
     583
     584        template< typename T >
     585        struct is_function : integral_constant< bool, is_function_pointer< typename remove_cv<T>::type *>::value > {};
     586
     587        template< typename T >
     588        struct is_function < T& > : false_type {};
     589
     590        template< typename T >
     591        struct is_function < T&& > : false_type {};
     592
     593        template< typename T >
     594        struct decay
     595        {
     596                typedef typename remove_reference<T>::type U;
     597                typedef typename conditional <
     598                        is_array<U>::value,
     599                        typename remove_extent<U>::type*,
     600                        typename conditional <
     601                                is_function<U>::value,
     602                                typename add_pointer<U>::type,
     603                                typename remove_cv<U>::type
     604                        > ::type
     605                > ::type type;
    468606        };
    469607
    470608}
    471609
    472 #endif // NV_CORE_TYPE_TRAITS_HH
     610#endif // NV_STL_TYPE_TRAITS_HH
  • trunk/nv/stl/utility.hh

    r372 r373  
    1515
    1616#include <nv/core/common.hh>
     17#include <nv/stl/type_traits.hh>
    1718
    1819namespace nv
Note: See TracChangeset for help on using the changeset viewer.