/core/externals/google-toolbox-for-mac/Foundation/GTMObjC2Runtime.h

http://macfuse.googlecode.com/ · C++ Header · 113 lines · 67 code · 11 blank · 35 comment · 3 complexity · 9a2cc3153ccac5c3248e315d18b9648a MD5 · raw file

  1. //
  2. // GTMObjC2Runtime.h
  3. //
  4. // Copyright 2007-2008 Google Inc.
  5. //
  6. // Licensed under the Apache License, Version 2.0 (the "License"); you may not
  7. // use this file except in compliance with the License. You may obtain a copy
  8. // of the License at
  9. //
  10. // http://www.apache.org/licenses/LICENSE-2.0
  11. //
  12. // Unless required by applicable law or agreed to in writing, software
  13. // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  14. // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  15. // License for the specific language governing permissions and limitations under
  16. // the License.
  17. //
  18. #import <objc/objc-api.h>
  19. #import <objc/objc-auto.h>
  20. #import "GTMDefines.h"
  21. // These functions exist for code that we want to compile on both the < 10.5
  22. // sdks and on the >= 10.5 sdks without warnings. It basically reimplements
  23. // certain parts of the objc2 runtime in terms of the objc1 runtime. It is not
  24. // a complete implementation as I've only implemented the routines I know we
  25. // use. Feel free to add more as necessary.
  26. // These functions are not documented because they conform to the documentation
  27. // for the ObjC2 Runtime.
  28. #if OBJC_API_VERSION >= 2 // Only have optional and req'd keywords in ObjC2.
  29. #define AT_OPTIONAL @optional
  30. #define AT_REQUIRED @required
  31. #else
  32. #define AT_OPTIONAL
  33. #define AT_REQUIRED
  34. #endif
  35. // The file objc-runtime.h was moved to runtime.h and in Leopard, objc-runtime.h
  36. // was just a wrapper around runtime.h. For the iPhone SDK, this objc-runtime.h
  37. // is removed in the iPhoneOS2.0 SDK.
  38. //
  39. // The |Object| class was removed in the iPhone2.0 SDK too.
  40. #if GTM_IPHONE_SDK
  41. #import <objc/message.h>
  42. #import <objc/runtime.h>
  43. #else
  44. #import <objc/objc-runtime.h>
  45. #import <objc/Object.h>
  46. #endif
  47. #import <libkern/OSAtomic.h>
  48. #if GTM_MACOS_SDK && (MAC_OS_X_VERSION_MIN_REQUIRED < MAC_OS_X_VERSION_10_5)
  49. #import "objc/Protocol.h"
  50. OBJC_EXPORT Class object_getClass(id obj);
  51. OBJC_EXPORT const char *class_getName(Class cls);
  52. OBJC_EXPORT BOOL class_conformsToProtocol(Class cls, Protocol *protocol);
  53. OBJC_EXPORT BOOL class_respondsToSelector(Class cls, SEL sel);
  54. OBJC_EXPORT Class class_getSuperclass(Class cls);
  55. OBJC_EXPORT Method *class_copyMethodList(Class cls, unsigned int *outCount);
  56. OBJC_EXPORT SEL method_getName(Method m);
  57. OBJC_EXPORT void method_exchangeImplementations(Method m1, Method m2);
  58. OBJC_EXPORT IMP method_getImplementation(Method method);
  59. OBJC_EXPORT IMP method_setImplementation(Method method, IMP imp);
  60. OBJC_EXPORT struct objc_method_description protocol_getMethodDescription(Protocol *p,
  61. SEL aSel,
  62. BOOL isRequiredMethod,
  63. BOOL isInstanceMethod);
  64. OBJC_EXPORT BOOL sel_isEqual(SEL lhs, SEL rhs);
  65. // If building for 10.4 but using the 10.5 SDK, don't include these.
  66. #if MAC_OS_X_VERSION_MAX_ALLOWED < MAC_OS_X_VERSION_10_5
  67. // atomics
  68. // On Leopard these are GC aware
  69. // Intentionally did not include the non-barrier versions, because I couldn't
  70. // come up with a case personally where you wouldn't want to use the
  71. // barrier versions.
  72. GTM_INLINE bool OSAtomicCompareAndSwapPtrBarrier(void *predicate,
  73. void *replacement,
  74. void * volatile *theValue) {
  75. #if defined(__LP64__) && __LP64__
  76. return OSAtomicCompareAndSwap64Barrier((int64_t)predicate,
  77. (int64_t)replacement,
  78. (int64_t *)theValue);
  79. #else // defined(__LP64__) && __LP64__
  80. return OSAtomicCompareAndSwap32Barrier((int32_t)predicate,
  81. (int32_t)replacement,
  82. (int32_t *)theValue);
  83. #endif // defined(__LP64__) && __LP64__
  84. }
  85. #endif // MAC_OS_X_VERSION_MAX_ALLOWED < MAC_OS_X_VERSION_10_5
  86. #endif // GTM_MACOS_SDK && (MAC_OS_X_VERSION_MIN_REQUIRED < MAC_OS_X_VERSION_10_5)
  87. #if GTM_MACOS_SDK && (MAC_OS_X_VERSION_MAX_ALLOWED < MAC_OS_X_VERSION_10_5)
  88. GTM_INLINE BOOL objc_atomicCompareAndSwapGlobalBarrier(id predicate,
  89. id replacement,
  90. volatile id *objectLocation) {
  91. return OSAtomicCompareAndSwapPtrBarrier(predicate,
  92. replacement,
  93. (void * volatile *)objectLocation);
  94. }
  95. GTM_INLINE BOOL objc_atomicCompareAndSwapInstanceVariableBarrier(id predicate,
  96. id replacement,
  97. volatile id *objectLocation) {
  98. return OSAtomicCompareAndSwapPtrBarrier(predicate,
  99. replacement,
  100. (void * volatile *)objectLocation);
  101. }
  102. #endif // GTM_MACOS_SDK && (MAC_OS_X_VERSION_MAX_ALLOWED < MAC_OS_X_VERSION_10_5)