williamr@2: /* williamr@2: * Copyright (c) 2003 williamr@2: * Francois Dumont williamr@2: * williamr@2: * This material is provided "as is", with absolutely no warranty expressed williamr@2: * or implied. Any use is at your own risk. williamr@2: * williamr@2: * Permission to use or copy this software for any purpose is hereby granted williamr@2: * without fee, provided the above notices are retained on all copies. williamr@2: * Permission to modify the code and to distribute modified code is granted, williamr@2: * provided the above notices are retained, and a notice that the code was williamr@2: * modified is included with the above copyright notice. williamr@2: * williamr@2: */ williamr@2: williamr@2: /* NOTE: This is an internal header file, included by other STL headers. williamr@2: * You should not attempt to use it directly. williamr@2: */ williamr@2: williamr@2: #ifndef _STLP_POINTERS_SPEC_TOOLS_H williamr@2: #define _STLP_POINTERS_SPEC_TOOLS_H williamr@2: williamr@2: #ifndef _STLP_TYPE_TRAITS_H williamr@2: # include williamr@2: #endif williamr@2: williamr@2: _STLP_BEGIN_NAMESPACE williamr@2: williamr@2: //Some usefull declarations: williamr@2: template struct less; williamr@2: williamr@2: _STLP_MOVE_TO_PRIV_NAMESPACE williamr@2: williamr@2: template williamr@2: struct _BinaryPredWrapper; williamr@2: williamr@2: /* williamr@2: * Since the compiler only allows at most one non-trivial williamr@2: * implicit conversion we can make use of a shim class to williamr@2: * be sure that functions below doesn't accept classes with williamr@2: * implicit pointer conversion operators williamr@2: */ williamr@2: struct _ConstVolatileVoidPointerShim williamr@2: { _ConstVolatileVoidPointerShim(const volatile void*); }; williamr@2: williamr@2: //The dispatch functions: williamr@2: struct _VoidPointerShim williamr@2: { _VoidPointerShim(void*); }; williamr@2: struct _ConstVoidPointerShim williamr@2: { _ConstVoidPointerShim(const void*); }; williamr@2: struct _VolatileVoidPointerShim williamr@2: { _VolatileVoidPointerShim(volatile void*); }; williamr@2: williamr@2: template williamr@2: char _UseVoidPtrStorageType(const __false_type& /*POD*/, const _Tp&); williamr@2: char _UseVoidPtrStorageType(const __true_type& /*POD*/, ...); williamr@2: char* _UseVoidPtrStorageType(const __true_type& /*POD*/, _VoidPointerShim); williamr@2: williamr@2: template williamr@2: char _UseConstVoidPtrStorageType(const __false_type& /*POD*/, const _Tp&); williamr@2: char _UseConstVoidPtrStorageType(const __true_type& /*POD*/, ...); williamr@2: char* _UseConstVoidPtrStorageType(const __true_type& /*POD*/, _ConstVoidPointerShim); williamr@2: williamr@2: template williamr@2: char _UseVolatileVoidPtrStorageType(const __false_type& /*POD*/, const _Tp&); williamr@2: char _UseVolatileVoidPtrStorageType(const __true_type& /*POD*/, ...); williamr@2: char* _UseVolatileVoidPtrStorageType(const __true_type& /*POD*/, _VolatileVoidPointerShim); williamr@2: williamr@2: template williamr@2: char _UseConstVolatileVoidPtrStorageType(const __false_type& /*POD*/, const _Tp&); williamr@2: char _UseConstVolatileVoidPtrStorageType(const __true_type& /*POD*/, ...); williamr@2: char* _UseConstVolatileVoidPtrStorageType(const __true_type& /*POD*/, _ConstVolatileVoidPointerShim); williamr@2: williamr@2: template williamr@2: struct _StorageType { williamr@2: typedef typename __type_traits<_Tp>::is_POD_type _PODType; williamr@2: static _Tp __null_rep(); williamr@2: williamr@2: enum { use_void_ptr = (sizeof(_UseVoidPtrStorageType(_PODType(), __null_rep())) == sizeof(char*)) }; williamr@2: enum { use_const_void_ptr = (sizeof(_UseConstVoidPtrStorageType(_PODType(), __null_rep())) == sizeof(char*)) }; williamr@2: enum { use_volatile_void_ptr = (sizeof(_UseVolatileVoidPtrStorageType(_PODType(), __null_rep())) == sizeof(char*)) }; williamr@2: enum { use_const_volatile_void_ptr = (sizeof(_UseConstVolatileVoidPtrStorageType(_PODType(), __null_rep())) == sizeof(char*)) }; williamr@2: williamr@2: typedef typename __select::_Ret >::_Ret >::_Ret >::_Ret _QualifiedType; williamr@2: williamr@2: #if !defined (_STLP_CLASS_PARTIAL_SPECIALIZATION) williamr@2: /* If the compiler do not support the iterator_traits structure we cannot wrap williamr@2: * iterators pass to container template methods. The iterator dereferenced value williamr@2: * has to be storable without any cast in the chosen storage type. To guaranty williamr@2: * that the void pointer has to be correctly qualified. williamr@2: */ williamr@2: typedef _QualifiedType _Type; williamr@2: #else williamr@2: /* With iterator_traits we can wrap passed iterators and make the necessary casts. williamr@2: * We can always use a simple void* storage type: williamr@2: */ williamr@2: typedef typename __select::_Ret _Type; williamr@2: #endif williamr@2: }; williamr@2: williamr@2: template williamr@2: struct _AssocStorageTypes { williamr@2: typedef _StorageType<_Tp> _StorageTypeInfo; williamr@2: typedef typename _StorageTypeInfo::_Type _SType; williamr@2: williamr@2: //We need to also check that the comparison functor used to instanciate the assoc container williamr@2: //is the default Standard less implementation: williamr@2: typedef typename _IsSTLportClass<_Compare>::_Ret _STLportLess; williamr@2: enum { is_default_less = __type2bool<_STLportLess>::_Ret }; williamr@2: williamr@2: typedef typename __select::_Ret _KeyStorageType; williamr@2: enum { ptr_type = _StorageTypeInfo::use_const_volatile_void_ptr }; williamr@2: typedef typename __select, williamr@2: _Compare>::_Ret _CompareStorageType; williamr@2: }; williamr@2: williamr@2: williamr@2: #if defined (_STLP_CLASS_PARTIAL_SPECIALIZATION) williamr@2: /* williamr@2: * Base struct to deal with qualifiers williamr@2: */ williamr@2: template williamr@2: struct _VoidCastTraitsAux { williamr@2: typedef _QualifiedStorageT void_cv_type; williamr@2: typedef _StorageT void_type; williamr@2: williamr@2: static void_type * uncv_ptr(void_cv_type *__ptr) williamr@2: { return __ptr; } williamr@2: static void_type const* uncv_cptr(void_cv_type const*__ptr) williamr@2: { return __ptr; } williamr@2: static void_type ** uncv_pptr(void_cv_type **__ptr) williamr@2: { return __ptr; } williamr@2: static void_type & uncv_ref(void_cv_type & __ref) williamr@2: { return __ref; } williamr@2: static void_type const& uncv_cref(void_cv_type const& __ref) williamr@2: { return __ref; } williamr@2: static void_cv_type* cv_ptr(void_type *__ptr) williamr@2: { return __ptr; } williamr@2: static void_cv_type const* cv_cptr(void_type const*__ptr) williamr@2: { return __ptr; } williamr@2: static void_cv_type ** cv_pptr(void_type **__ptr) williamr@2: { return __ptr; } williamr@2: static void_cv_type & cv_ref(void_type & __ref) williamr@2: { return __ref; } williamr@2: static void_cv_type const& cv_cref(void_type const& __ref) williamr@2: { return __ref; } williamr@2: }; williamr@2: williamr@2: template williamr@2: struct _VoidCastTraitsAuxBase { williamr@2: typedef _VoidCVType* void_cv_type; williamr@2: typedef void* void_type; williamr@2: williamr@2: static void_type* uncv_ptr(void_cv_type *__ptr) williamr@2: { return __CONST_CAST(void_type*, __ptr); } williamr@2: static void_type const* uncv_cptr(void_cv_type const*__ptr) williamr@2: { return __CONST_CAST(void_type const*, __ptr); } williamr@2: static void_type** uncv_pptr(void_cv_type **__ptr) williamr@2: { return __CONST_CAST(void_type**, __ptr); } williamr@2: static void_type& uncv_ref(void_cv_type &__ref) williamr@2: { return __CONST_CAST(void_type&, __ref); } williamr@2: static void_type const& uncv_cref(void_cv_type const& __ptr) williamr@2: { return __CONST_CAST(void_type const&, __ptr); } williamr@2: // The reverse versions williamr@2: static void_cv_type * cv_ptr(void_type *__ptr) williamr@2: { return __CONST_CAST(void_cv_type *, __ptr); } williamr@2: static void_cv_type const* cv_cptr(void_type const*__ptr) williamr@2: { return __CONST_CAST(void_cv_type const*, __ptr); } williamr@2: static void_cv_type ** cv_pptr(void_type **__ptr) williamr@2: { return __CONST_CAST(void_cv_type**, __ptr); } williamr@2: static void_cv_type & cv_ref(void_type &__ref) williamr@2: { return __CONST_CAST(void_cv_type &, __ref); } williamr@2: static void_cv_type const& cv_cref(void_type const& __ref) williamr@2: { return __CONST_CAST(void_cv_type const&, __ref); } williamr@2: }; williamr@2: williamr@2: _STLP_TEMPLATE_NULL williamr@2: struct _VoidCastTraitsAux : _VoidCastTraitsAuxBase williamr@2: {}; williamr@2: _STLP_TEMPLATE_NULL williamr@2: struct _VoidCastTraitsAux : _VoidCastTraitsAuxBase williamr@2: {}; williamr@2: _STLP_TEMPLATE_NULL williamr@2: struct _VoidCastTraitsAux : _VoidCastTraitsAuxBase williamr@2: {}; williamr@2: williamr@2: template williamr@2: struct _CastTraits { williamr@2: typedef _ValueT value_type; williamr@2: typedef typename _StorageType<_ValueT>::_QualifiedType _QualifiedStorageT; williamr@2: typedef _VoidCastTraitsAux<_StorageT, _QualifiedStorageT> cv_traits; williamr@2: typedef typename cv_traits::void_type void_type; williamr@2: typedef typename cv_traits::void_cv_type void_cv_type; williamr@2: williamr@2: static value_type * to_value_type_ptr(void_type *__ptr) williamr@2: { return __REINTERPRET_CAST(value_type *, cv_traits::cv_ptr(__ptr)); } williamr@2: static value_type const* to_value_type_cptr(void_type const*__ptr) williamr@2: { return __REINTERPRET_CAST(value_type const*, cv_traits::cv_cptr(__ptr)); } williamr@2: static value_type ** to_value_type_pptr(void_type **__ptr) williamr@2: { return __REINTERPRET_CAST(value_type **, cv_traits::cv_pptr(__ptr)); } williamr@2: static value_type & to_value_type_ref(void_type &__ref) williamr@2: { return __REINTERPRET_CAST(value_type &, cv_traits::cv_ref(__ref)); } williamr@2: static value_type const& to_value_type_cref(void_type const& __ptr) williamr@2: { return __REINTERPRET_CAST(value_type const&, cv_traits::cv_cref(__ptr)); } williamr@2: // Reverse versions williamr@2: static void_type * to_storage_type_ptr(value_type *__ptr) williamr@2: { return cv_traits::uncv_ptr(__REINTERPRET_CAST(void_cv_type *, __ptr)); } williamr@2: static void_type const* to_storage_type_cptr(value_type const*__ptr) williamr@2: { return cv_traits::uncv_cptr(__REINTERPRET_CAST(void_cv_type const*, __ptr)); } williamr@2: static void_type ** to_storage_type_pptr(value_type **__ptr) williamr@2: { return cv_traits::uncv_pptr(__REINTERPRET_CAST(void_cv_type **, __ptr)); } williamr@2: static void_type const& to_storage_type_cref(value_type const& __ref) williamr@2: { return cv_traits::uncv_cref(__REINTERPRET_CAST(void_cv_type const&, __ref)); } williamr@2: williamr@2: //Method used to treat set container template method extension williamr@2: static void_type const& to_storage_type_crefT(value_type const& __ref) williamr@2: { return to_storage_type_cref(__ref); } williamr@2: }; williamr@2: williamr@2: template williamr@2: struct _CastTraits<_Tp, _Tp> { williamr@2: typedef _Tp storage_type; williamr@2: typedef _Tp value_type; williamr@2: williamr@2: static value_type * to_value_type_ptr(storage_type *__ptr) williamr@2: { return __ptr; } williamr@2: static value_type const* to_value_type_cptr(storage_type const*__ptr) williamr@2: { return __ptr; } williamr@2: static value_type ** to_value_type_pptr(storage_type **__ptr) williamr@2: { return __ptr; } williamr@2: static value_type & to_value_type_ref(storage_type &__ref) williamr@2: { return __ref; } williamr@2: static value_type const& to_value_type_cref(storage_type const&__ref) williamr@2: { return __ref; } williamr@2: // Reverse versions williamr@2: static storage_type * to_storage_type_ptr(value_type *__ptr) williamr@2: { return __ptr; } williamr@2: static storage_type const* to_storage_type_cptr(value_type const*__ptr) williamr@2: { return __ptr; } williamr@2: static storage_type ** to_storage_type_pptr(value_type **__ptr) williamr@2: { return __ptr; } williamr@2: static storage_type const& to_storage_type_cref(value_type const& __ref) williamr@2: { return __ref; } williamr@2: williamr@2: //Method used to treat set container template method extension williamr@2: template williamr@2: static _Tp1 const& to_storage_type_crefT(_Tp1 const& __ref) williamr@2: { return __ref; } williamr@2: }; williamr@2: williamr@2: #define _STLP_USE_ITERATOR_WRAPPER williamr@2: williamr@2: template williamr@2: struct _IteWrapper { williamr@2: typedef _CastTraits<_StorageT, _ValueT> cast_traits; williamr@2: typedef iterator_traits<_Iterator> _IteTraits; williamr@2: williamr@2: typedef typename _IteTraits::iterator_category iterator_category; williamr@2: typedef _StorageT value_type; williamr@2: typedef typename _IteTraits::difference_type difference_type; williamr@2: typedef value_type* pointer; williamr@2: typedef value_type const& const_reference; williamr@2: //This wrapper won't be used for input so to avoid surprise williamr@2: //the reference type will be a const reference: williamr@2: typedef const_reference reference; williamr@2: williamr@2: typedef _IteWrapper<_StorageT, _ValueT, _Iterator> _Self; williamr@2: typedef _Self _Ite; williamr@2: williamr@2: _IteWrapper(_Iterator &__ite) : _M_ite(__ite) {} williamr@2: williamr@2: const_reference operator*() const { return cast_traits::to_storage_type_cref(*_M_ite); } williamr@2: williamr@2: _Self& operator= (_Self const& __rhs) { williamr@2: _M_ite = __rhs._M_ite; williamr@2: return *this; williamr@2: } williamr@2: williamr@2: _Self& operator++() { williamr@2: ++_M_ite; williamr@2: return *this; williamr@2: } williamr@2: williamr@2: _Self& operator--() { williamr@2: --_M_ite; williamr@2: return *this; williamr@2: } williamr@2: williamr@2: _Self& operator += (difference_type __offset) { williamr@2: _M_ite += __offset; williamr@2: return *this; williamr@2: } williamr@2: difference_type operator -(_Self const& __other) const williamr@2: { return _M_ite - __other._M_ite; } williamr@2: williamr@2: bool operator == (_Self const& __other) const williamr@2: { return _M_ite == __other._M_ite; } williamr@2: williamr@2: bool operator != (_Self const& __other) const williamr@2: { return _M_ite != __other._M_ite; } williamr@2: williamr@2: bool operator < (_Self const& __rhs) const williamr@2: { return _M_ite < __rhs._M_ite; } williamr@2: williamr@2: private: williamr@2: _Iterator _M_ite; williamr@2: }; williamr@2: williamr@2: template williamr@2: struct _IteWrapper<_Tp, _Tp, _Iterator> williamr@2: { typedef _Iterator _Ite; }; williamr@2: williamr@2: #else williamr@2: williamr@2: /* williamr@2: * In this config the storage type is qualified in respect of the williamr@2: * value_type qualification. Simple reinterpret_cast is enough. williamr@2: */ williamr@2: template williamr@2: struct _CastTraits { williamr@2: typedef _StorageT storage_type; williamr@2: typedef _ValueT value_type; williamr@2: williamr@2: static value_type * to_value_type_ptr(storage_type *__ptr) williamr@2: { return __REINTERPRET_CAST(value_type*, __ptr); } williamr@2: static value_type const* to_value_type_cptr(storage_type const*__ptr) williamr@2: { return __REINTERPRET_CAST(value_type const*, __ptr); } williamr@2: static value_type ** to_value_type_pptr(storage_type **__ptr) williamr@2: { return __REINTERPRET_CAST(value_type **, __ptr); } williamr@2: static value_type & to_value_type_ref(storage_type &__ref) williamr@2: { return __REINTERPRET_CAST(value_type&, __ref); } williamr@2: static value_type const& to_value_type_cref(storage_type const&__ref) williamr@2: { return __REINTERPRET_CAST(value_type const&, __ref); } williamr@2: // Reverse versions williamr@2: static storage_type * to_storage_type_ptr(value_type *__ptr) williamr@2: { return __REINTERPRET_CAST(storage_type*, __ptr); } williamr@2: static storage_type const* to_storage_type_cptr(value_type const*__ptr) williamr@2: { return __REINTERPRET_CAST(storage_type const*, __ptr); } williamr@2: static storage_type ** to_storage_type_pptr(value_type **__ptr) williamr@2: { return __REINTERPRET_CAST(storage_type **, __ptr); } williamr@2: static storage_type const& to_storage_type_cref(value_type const&__ref) williamr@2: { return __REINTERPRET_CAST(storage_type const&, __ref); } williamr@2: template williamr@2: static _Tp1 const& to_storage_type_crefT(_Tp1 const& __ref) williamr@2: { return __ref; } williamr@2: }; williamr@2: williamr@2: #endif williamr@2: williamr@2: //Wrapper functors: williamr@2: template williamr@2: struct _UnaryPredWrapper { williamr@2: typedef _CastTraits<_StorageT, _ValueT> cast_traits; williamr@2: williamr@2: _UnaryPredWrapper (_UnaryPredicate const& __pred) : _M_pred(__pred) {} williamr@2: williamr@2: bool operator () (_StorageT const& __ref) const williamr@2: { return _M_pred(cast_traits::to_value_type_cref(__ref)); } williamr@2: williamr@2: private: williamr@2: _UnaryPredicate _M_pred; williamr@2: }; williamr@2: williamr@2: template williamr@2: struct _BinaryPredWrapper { williamr@2: typedef _CastTraits<_StorageT, _ValueT> cast_traits; williamr@2: williamr@2: _BinaryPredWrapper () {} williamr@2: _BinaryPredWrapper (_BinaryPredicate const& __pred) : _M_pred(__pred) {} williamr@2: williamr@2: _BinaryPredicate get_pred() const { return _M_pred; } williamr@2: williamr@2: bool operator () (_StorageT const& __fst, _StorageT const& __snd) const williamr@2: { return _M_pred(cast_traits::to_value_type_cref(__fst), cast_traits::to_value_type_cref(__snd)); } williamr@2: williamr@2: //Cast operator used to transparently access underlying predicate williamr@2: //in set::key_comp() method williamr@2: operator _BinaryPredicate() const williamr@2: { return _M_pred; } williamr@2: williamr@2: private: williamr@2: _BinaryPredicate _M_pred; williamr@2: }; williamr@2: williamr@2: _STLP_MOVE_TO_STD_NAMESPACE williamr@2: williamr@2: _STLP_END_NAMESPACE williamr@2: williamr@2: #endif /* _STLP_POINTERS_SPEC_TOOLS_H */