MASUtilities.h 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. //
  2. // MASUtilities.h
  3. // Masonry
  4. //
  5. // Created by Jonas Budelmann on 19/08/13.
  6. // Copyright (c) 2013 Jonas Budelmann. All rights reserved.
  7. //
  8. #import <Foundation/Foundation.h>
  9. #if TARGET_OS_IPHONE || TARGET_OS_TV
  10. #import <UIKit/UIKit.h>
  11. #define MAS_VIEW UIView
  12. #define MAS_VIEW_CONTROLLER UIViewController
  13. #define MASEdgeInsets UIEdgeInsets
  14. typedef UILayoutPriority MASLayoutPriority;
  15. static const MASLayoutPriority MASLayoutPriorityRequired = UILayoutPriorityRequired;
  16. static const MASLayoutPriority MASLayoutPriorityDefaultHigh = UILayoutPriorityDefaultHigh;
  17. static const MASLayoutPriority MASLayoutPriorityDefaultMedium = 500;
  18. static const MASLayoutPriority MASLayoutPriorityDefaultLow = UILayoutPriorityDefaultLow;
  19. static const MASLayoutPriority MASLayoutPriorityFittingSizeLevel = UILayoutPriorityFittingSizeLevel;
  20. #elif TARGET_OS_MAC
  21. #import <AppKit/AppKit.h>
  22. #define MAS_VIEW NSView
  23. #define MASEdgeInsets NSEdgeInsets
  24. typedef NSLayoutPriority MASLayoutPriority;
  25. static const MASLayoutPriority MASLayoutPriorityRequired = NSLayoutPriorityRequired;
  26. static const MASLayoutPriority MASLayoutPriorityDefaultHigh = NSLayoutPriorityDefaultHigh;
  27. static const MASLayoutPriority MASLayoutPriorityDragThatCanResizeWindow = NSLayoutPriorityDragThatCanResizeWindow;
  28. static const MASLayoutPriority MASLayoutPriorityDefaultMedium = 501;
  29. static const MASLayoutPriority MASLayoutPriorityWindowSizeStayPut = NSLayoutPriorityWindowSizeStayPut;
  30. static const MASLayoutPriority MASLayoutPriorityDragThatCannotResizeWindow = NSLayoutPriorityDragThatCannotResizeWindow;
  31. static const MASLayoutPriority MASLayoutPriorityDefaultLow = NSLayoutPriorityDefaultLow;
  32. static const MASLayoutPriority MASLayoutPriorityFittingSizeCompression = NSLayoutPriorityFittingSizeCompression;
  33. #endif
  34. /**
  35. * Allows you to attach keys to objects matching the variable names passed.
  36. *
  37. * view1.mas_key = @"view1", view2.mas_key = @"view2";
  38. *
  39. * is equivalent to:
  40. *
  41. * MASAttachKeys(view1, view2);
  42. */
  43. #define MASAttachKeys(...) \
  44. { \
  45. NSDictionary *keyPairs = NSDictionaryOfVariableBindings(__VA_ARGS__); \
  46. for (id key in keyPairs.allKeys) { \
  47. id obj = keyPairs[key]; \
  48. NSAssert([obj respondsToSelector:@selector(setMas_key:)], \
  49. @"Cannot attach mas_key to %@", obj); \
  50. [obj setMas_key:key]; \
  51. } \
  52. }
  53. /**
  54. * Used to create object hashes
  55. * Based on http://www.mikeash.com/pyblog/friday-qa-2010-06-18-implementing-equality-and-hashing.html
  56. */
  57. #define MAS_NSUINT_BIT (CHAR_BIT * sizeof(NSUInteger))
  58. #define MAS_NSUINTROTATE(val, howmuch) ((((NSUInteger)val) << howmuch) | (((NSUInteger)val) >> (MAS_NSUINT_BIT - howmuch)))
  59. /**
  60. * Given a scalar or struct value, wraps it in NSValue
  61. * Based on EXPObjectify: https://github.com/specta/expecta
  62. */
  63. static inline id _MASBoxValue(const char *type, ...) {
  64. va_list v;
  65. va_start(v, type);
  66. id obj = nil;
  67. if (strcmp(type, @encode(id)) == 0) {
  68. id actual = va_arg(v, id);
  69. obj = actual;
  70. } else if (strcmp(type, @encode(CGPoint)) == 0) {
  71. CGPoint actual = (CGPoint)va_arg(v, CGPoint);
  72. obj = [NSValue value:&actual withObjCType:type];
  73. } else if (strcmp(type, @encode(CGSize)) == 0) {
  74. CGSize actual = (CGSize)va_arg(v, CGSize);
  75. obj = [NSValue value:&actual withObjCType:type];
  76. } else if (strcmp(type, @encode(MASEdgeInsets)) == 0) {
  77. MASEdgeInsets actual = (MASEdgeInsets)va_arg(v, MASEdgeInsets);
  78. obj = [NSValue value:&actual withObjCType:type];
  79. } else if (strcmp(type, @encode(double)) == 0) {
  80. double actual = (double)va_arg(v, double);
  81. obj = [NSNumber numberWithDouble:actual];
  82. } else if (strcmp(type, @encode(float)) == 0) {
  83. float actual = (float)va_arg(v, double);
  84. obj = [NSNumber numberWithFloat:actual];
  85. } else if (strcmp(type, @encode(int)) == 0) {
  86. int actual = (int)va_arg(v, int);
  87. obj = [NSNumber numberWithInt:actual];
  88. } else if (strcmp(type, @encode(long)) == 0) {
  89. long actual = (long)va_arg(v, long);
  90. obj = [NSNumber numberWithLong:actual];
  91. } else if (strcmp(type, @encode(long long)) == 0) {
  92. long long actual = (long long)va_arg(v, long long);
  93. obj = [NSNumber numberWithLongLong:actual];
  94. } else if (strcmp(type, @encode(short)) == 0) {
  95. short actual = (short)va_arg(v, int);
  96. obj = [NSNumber numberWithShort:actual];
  97. } else if (strcmp(type, @encode(char)) == 0) {
  98. char actual = (char)va_arg(v, int);
  99. obj = [NSNumber numberWithChar:actual];
  100. } else if (strcmp(type, @encode(bool)) == 0) {
  101. bool actual = (bool)va_arg(v, int);
  102. obj = [NSNumber numberWithBool:actual];
  103. } else if (strcmp(type, @encode(unsigned char)) == 0) {
  104. unsigned char actual = (unsigned char)va_arg(v, unsigned int);
  105. obj = [NSNumber numberWithUnsignedChar:actual];
  106. } else if (strcmp(type, @encode(unsigned int)) == 0) {
  107. unsigned int actual = (unsigned int)va_arg(v, unsigned int);
  108. obj = [NSNumber numberWithUnsignedInt:actual];
  109. } else if (strcmp(type, @encode(unsigned long)) == 0) {
  110. unsigned long actual = (unsigned long)va_arg(v, unsigned long);
  111. obj = [NSNumber numberWithUnsignedLong:actual];
  112. } else if (strcmp(type, @encode(unsigned long long)) == 0) {
  113. unsigned long long actual = (unsigned long long)va_arg(v, unsigned long long);
  114. obj = [NSNumber numberWithUnsignedLongLong:actual];
  115. } else if (strcmp(type, @encode(unsigned short)) == 0) {
  116. unsigned short actual = (unsigned short)va_arg(v, unsigned int);
  117. obj = [NSNumber numberWithUnsignedShort:actual];
  118. }
  119. va_end(v);
  120. return obj;
  121. }
  122. #define MASBoxValue(value) _MASBoxValue(@encode(__typeof__((value))), (value))