OVR_Nullptr.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. /************************************************************************************
  2. PublicHeader: OVR_Kernel.h
  3. Filename : OVR_Nullptr.h
  4. Content : Implements C++11 nullptr for the case that the compiler doesn't.
  5. Created : June 19, 2014
  6. Notes :
  7. Copyright : Copyright 2014 Oculus VR, LLC All Rights reserved.
  8. Licensed under the Oculus VR Rift SDK License Version 3.2 (the "License");
  9. you may not use the Oculus VR Rift SDK except in compliance with the License,
  10. which is provided at the time of installation or download, or which
  11. otherwise accompanies this software in either electronic or hard copy form.
  12. You may obtain a copy of the License at
  13. http://www.oculusvr.com/licenses/LICENSE-3.2
  14. Unless required by applicable law or agreed to in writing, the Oculus VR SDK
  15. distributed under the License is distributed on an "AS IS" BASIS,
  16. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  17. See the License for the specific language governing permissions and
  18. limitations under the License.
  19. ************************************************************************************/
  20. #ifndef OVR_Nullptr_h
  21. #define OVR_Nullptr_h
  22. #pragma once
  23. #include "OVR_Types.h"
  24. //-----------------------------------------------------------------------------------
  25. // ***** OVR_HAVE_std_nullptr_t
  26. //
  27. // Identifies if <cstddef.h> includes std::nullptr_t.
  28. //
  29. #if !defined(OVR_HAVE_std_nullptr_t) && defined(OVR_CPP11_ENABLED)
  30. #if defined(OVR_STDLIB_LIBCPP)
  31. #define OVR_HAVE_std_nullptr_t 1
  32. #elif defined(OVR_STDLIB_LIBSTDCPP)
  33. #if (__GLIBCXX__ >= 20110325) && (__GLIBCXX__ != 20110428) && (__GLIBCXX__ != 20120702)
  34. #define OVR_HAVE_std_nullptr_t 1
  35. #endif
  36. #elif defined(_MSC_VER) && (_MSC_VER >= 1600) // VS2010+
  37. #define OVR_HAVE_std_nullptr_t 1
  38. #elif defined(__clang__)
  39. #define OVR_HAVE_std_nullptr_t 1
  40. #elif defined(OVR_CPP_GNUC) && (OVR_CC_VERSION >= 406) // GCC 4.6+
  41. #define OVR_HAVE_std_nullptr_t 1
  42. #endif
  43. #endif
  44. //-----------------------------------------------------------------------------------
  45. // ***** nullptr / std::nullptr_t
  46. //
  47. // Declares and defines nullptr and related types.
  48. //
  49. #if defined(OVR_CPP_NO_NULLPTR)
  50. namespace std
  51. {
  52. class nullptr_t
  53. {
  54. public:
  55. template <typename T>
  56. operator T*() const
  57. { return 0; }
  58. template <typename C, typename T>
  59. operator T C::*() const
  60. { return 0; }
  61. #if OVR_CPP_NO_EXPLICIT_CONVERSION_OPERATORS
  62. typedef void* (nullptr_t::*bool_)() const; // 4.12,p1. We can't portably use operator bool(){ return false; } because bool
  63. operator bool_() const // is convertable to int which breaks other required functionality.
  64. { return false; }
  65. #else
  66. operator bool() const
  67. { return false; }
  68. #endif
  69. private:
  70. void operator&() const; // 5.2.10,p9
  71. };
  72. inline nullptr_t nullptr_get()
  73. {
  74. nullptr_t n = { };
  75. return n;
  76. }
  77. #if !defined(nullptr)
  78. #define nullptr nullptr_get()
  79. #endif
  80. } // namespace std
  81. // 5.9,p2 p4
  82. // 13.6, p13
  83. template <typename T>
  84. inline bool operator==(T* pT, const std::nullptr_t)
  85. { return pT == 0; }
  86. template <typename T>
  87. inline bool operator==(const std::nullptr_t, T* pT)
  88. { return pT == 0; }
  89. template <typename T, typename U>
  90. inline bool operator==(const std::nullptr_t, T U::* pU)
  91. { return pU == 0; }
  92. template <typename T, typename U>
  93. inline bool operator==(T U::* pTU, const std::nullptr_t)
  94. { return pTU == 0; }
  95. inline bool operator==(const std::nullptr_t, const std::nullptr_t)
  96. { return true; }
  97. inline bool operator!=(const std::nullptr_t, const std::nullptr_t)
  98. { return false; }
  99. inline bool operator<(const std::nullptr_t, const std::nullptr_t)
  100. { return false; }
  101. inline bool operator<=(const std::nullptr_t, const std::nullptr_t)
  102. { return true; }
  103. inline bool operator>(const std::nullptr_t, const std::nullptr_t)
  104. { return false; }
  105. inline bool operator>=(const std::nullptr_t, const std::nullptr_t)
  106. { return true; }
  107. using std::nullptr_t;
  108. using std::nullptr_get;
  109. // Some compilers natively support C++11 nullptr but the standard library being used
  110. // doesn't declare std::nullptr_t, in which case we provide one ourselves.
  111. #elif !defined(OVR_HAVE_std_nullptr_t) && !defined(OVR_CPP_NO_DECLTYPE)
  112. namespace std { typedef decltype(nullptr) nullptr_t; }
  113. #endif
  114. #endif