_move_construct_fwk.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. /*
  2. *
  3. * Copyright (c) 2003
  4. * Francois Dumont
  5. *
  6. * This material is provided "as is", with absolutely no warranty expressed
  7. * or implied. Any use is at your own risk.
  8. *
  9. * Permission to use or copy this software for any purpose is hereby granted
  10. * without fee, provided the above notices are retained on all copies.
  11. * Permission to modify the code and to distribute modified code is granted,
  12. * provided the above notices are retained, and a notice that the code was
  13. * modified is included with the above copyright notice.
  14. *
  15. */
  16. #ifndef _STLP_MOVE_CONSTRUCT_FWK_H
  17. #define _STLP_MOVE_CONSTRUCT_FWK_H
  18. #ifndef _STLP_TYPE_TRAITS_H
  19. # include <stl/type_traits.h>
  20. #endif
  21. _STLP_BEGIN_NAMESPACE
  22. /*************************************************************
  23. * Move constructor framework
  24. *************************************************************/
  25. /*************************************************************
  26. *Partial move:
  27. *The source HAS to be a valid instance after the move!
  28. *************************************************************/
  29. template <class _Tp>
  30. class __move_source {
  31. public:
  32. explicit __move_source (_Tp &_src) : _M_data(_src)
  33. {}
  34. _Tp& get() const
  35. { return _M_data; }
  36. private:
  37. _Tp &_M_data;
  38. //We explicitely forbid assignment to avoid warning:
  39. typedef __move_source<_Tp> _Self;
  40. _Self& operator = (_Self const&);
  41. };
  42. //Class used to signal move constructor support, implementation and type.
  43. template <class _Tp>
  44. struct __move_traits {
  45. /*
  46. * implemented tells if a the special move constructor has to be called or the classic
  47. * copy constructor is just fine. Most of the time the copy constructor is fine only
  48. * if the following info is true.
  49. */
  50. #if defined (_STLP_USE_PARTIAL_SPEC_WORKAROUND) && \
  51. !defined (_STLP_CLASS_PARTIAL_SPECIALIZATION) && \
  52. !defined (_STLP_NO_MOVE_SEMANTIC)
  53. typedef typename _IsSTLportClass<_Tp>::_Ret implemented;
  54. #else
  55. typedef __false_type implemented;
  56. #endif
  57. /*
  58. * complete tells if the move is complete or partial, that is to say, does the source
  59. * needs to be destroyed once it has been moved.
  60. */
  61. # if defined (__BORLANDC__) && (__BORLANDC__ >= 0x564)
  62. typedef __type_traits<_Tp>::has_trivial_destructor _TpMoveComplete;
  63. typedef typename __bool2type<__type2bool<_TpMoveComplete>::_Ret>::_Ret complete;
  64. # else
  65. typedef typename __type_traits<_Tp>::has_trivial_destructor complete;
  66. # endif
  67. };
  68. _STLP_MOVE_TO_PRIV_NAMESPACE
  69. /*
  70. * This struct should never be used if the user has not explicitely stipulated
  71. * that its class support the full move concept. To check that the return type
  72. * in such a case will be __invalid_source<_Tp> to generate a compile error
  73. * revealing the configuration problem.
  74. */
  75. template <class _Tp>
  76. struct _MoveSourceTraits {
  77. typedef typename __move_traits<_Tp>::implemented _MvImpRet;
  78. #if defined (__BORLANDC__)
  79. typedef typename __selectT<_MvImpRet,
  80. #else
  81. enum {_MvImp = __type2bool<_MvImpRet>::_Ret};
  82. typedef typename __select<_MvImp,
  83. #endif
  84. __move_source<_Tp>,
  85. _Tp const&>::_Ret _Type;
  86. };
  87. //The helper function
  88. template <class _Tp>
  89. inline _STLP_TYPENAME_ON_RETURN_TYPE _MoveSourceTraits<_Tp>::_Type
  90. _AsMoveSource (_Tp &src) {
  91. typedef typename _MoveSourceTraits<_Tp>::_Type _SrcType;
  92. return _SrcType(src);
  93. }
  94. //Helper structs used for many class.
  95. template <class _Tp>
  96. struct __move_traits_aux {
  97. typedef typename __move_traits<_Tp>::implemented implemented;
  98. typedef typename __move_traits<_Tp>::complete complete;
  99. };
  100. template <class _Tp1, class _Tp2>
  101. struct __move_traits_aux2 {
  102. typedef __move_traits<_Tp1> _MoveTraits1;
  103. typedef __move_traits<_Tp2> _MoveTraits2;
  104. typedef typename _Lor2<typename _MoveTraits1::implemented,
  105. typename _MoveTraits2::implemented>::_Ret implemented;
  106. typedef typename _Land2<typename _MoveTraits1::complete,
  107. typename _MoveTraits2::complete>::_Ret complete;
  108. };
  109. /*
  110. * Most of the time a class implement a move constructor but its use depends
  111. * on a third party, this is what the following struct are for.
  112. */
  113. template <class _Tp>
  114. struct __move_traits_help {
  115. typedef __true_type implemented;
  116. typedef typename __move_traits<_Tp>::complete complete;
  117. };
  118. template <class _Tp1, class _Tp2>
  119. struct __move_traits_help1 {
  120. typedef __move_traits<_Tp1> _MoveTraits1;
  121. typedef __move_traits<_Tp2> _MoveTraits2;
  122. typedef typename _Lor2<typename _MoveTraits1::implemented,
  123. typename _MoveTraits2::implemented>::_Ret implemented;
  124. typedef typename _Land2<typename _MoveTraits1::complete,
  125. typename _MoveTraits2::complete>::_Ret complete;
  126. };
  127. template <class _Tp1, class _Tp2>
  128. struct __move_traits_help2 {
  129. typedef __move_traits<_Tp1> _MoveTraits1;
  130. typedef __move_traits<_Tp2> _MoveTraits2;
  131. typedef __true_type implemented;
  132. typedef typename _Land2<typename _MoveTraits1::complete,
  133. typename _MoveTraits2::complete>::_Ret complete;
  134. };
  135. _STLP_MOVE_TO_STD_NAMESPACE
  136. _STLP_END_NAMESPACE
  137. #endif /* _STLP_MOVE_CONSTRUCT_FWK_H */