MASUtilities.h 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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
  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. NSDictionary *keyPairs = NSDictionaryOfVariableBindings(__VA_ARGS__); \
  45. for (id key in keyPairs.allKeys) { \
  46. id obj = keyPairs[key]; \
  47. NSAssert([obj respondsToSelector:@selector(setMas_key:)], \
  48. @"Cannot attach mas_key to %@", obj); \
  49. [obj setMas_key:key]; \
  50. }
  51. /**
  52. * Used to create object hashes
  53. * Based on http://www.mikeash.com/pyblog/friday-qa-2010-06-18-implementing-equality-and-hashing.html
  54. */
  55. #define MAS_NSUINT_BIT (CHAR_BIT * sizeof(NSUInteger))
  56. #define MAS_NSUINTROTATE(val, howmuch) ((((NSUInteger)val) << howmuch) | (((NSUInteger)val) >> (MAS_NSUINT_BIT - howmuch)))
  57. /**
  58. * Given a scalar or struct value, wraps it in NSValue
  59. * Based on EXPObjectify: https://github.com/specta/expecta
  60. */
  61. static inline id _MASBoxValue(const char *type, ...) {
  62. va_list v;
  63. va_start(v, type);
  64. id obj = nil;
  65. if (strcmp(type, @encode(id)) == 0) {
  66. id actual = va_arg(v, id);
  67. obj = actual;
  68. } else if (strcmp(type, @encode(CGPoint)) == 0) {
  69. CGPoint actual = (CGPoint)va_arg(v, CGPoint);
  70. obj = [NSValue value:&actual withObjCType:type];
  71. } else if (strcmp(type, @encode(CGSize)) == 0) {
  72. CGSize actual = (CGSize)va_arg(v, CGSize);
  73. obj = [NSValue value:&actual withObjCType:type];
  74. } else if (strcmp(type, @encode(MASEdgeInsets)) == 0) {
  75. MASEdgeInsets actual = (MASEdgeInsets)va_arg(v, MASEdgeInsets);
  76. obj = [NSValue value:&actual withObjCType:type];
  77. } else if (strcmp(type, @encode(double)) == 0) {
  78. double actual = (double)va_arg(v, double);
  79. obj = [NSNumber numberWithDouble:actual];
  80. } else if (strcmp(type, @encode(float)) == 0) {
  81. float actual = (float)va_arg(v, double);
  82. obj = [NSNumber numberWithFloat:actual];
  83. } else if (strcmp(type, @encode(int)) == 0) {
  84. int actual = (int)va_arg(v, int);
  85. obj = [NSNumber numberWithInt:actual];
  86. } else if (strcmp(type, @encode(long)) == 0) {
  87. long actual = (long)va_arg(v, long);
  88. obj = [NSNumber numberWithLong:actual];
  89. } else if (strcmp(type, @encode(long long)) == 0) {
  90. long long actual = (long long)va_arg(v, long long);
  91. obj = [NSNumber numberWithLongLong:actual];
  92. } else if (strcmp(type, @encode(short)) == 0) {
  93. short actual = (short)va_arg(v, int);
  94. obj = [NSNumber numberWithShort:actual];
  95. } else if (strcmp(type, @encode(char)) == 0) {
  96. char actual = (char)va_arg(v, int);
  97. obj = [NSNumber numberWithChar:actual];
  98. } else if (strcmp(type, @encode(bool)) == 0) {
  99. bool actual = (bool)va_arg(v, int);
  100. obj = [NSNumber numberWithBool:actual];
  101. } else if (strcmp(type, @encode(unsigned char)) == 0) {
  102. unsigned char actual = (unsigned char)va_arg(v, unsigned int);
  103. obj = [NSNumber numberWithUnsignedChar:actual];
  104. } else if (strcmp(type, @encode(unsigned int)) == 0) {
  105. unsigned int actual = (unsigned int)va_arg(v, unsigned int);
  106. obj = [NSNumber numberWithUnsignedInt:actual];
  107. } else if (strcmp(type, @encode(unsigned long)) == 0) {
  108. unsigned long actual = (unsigned long)va_arg(v, unsigned long);
  109. obj = [NSNumber numberWithUnsignedLong:actual];
  110. } else if (strcmp(type, @encode(unsigned long long)) == 0) {
  111. unsigned long long actual = (unsigned long long)va_arg(v, unsigned long long);
  112. obj = [NSNumber numberWithUnsignedLongLong:actual];
  113. } else if (strcmp(type, @encode(unsigned short)) == 0) {
  114. unsigned short actual = (unsigned short)va_arg(v, unsigned int);
  115. obj = [NSNumber numberWithUnsignedShort:actual];
  116. }
  117. va_end(v);
  118. return obj;
  119. }
  120. #define MASBoxValue(value) _MASBoxValue(@encode(__typeof__((value))), (value))