summaryrefslogtreecommitdiffstats
path: root/docs/ObjectiveCLiterals.rst
diff options
context:
space:
mode:
authorAlex Denisov <1101.debian@gmail.com>2015-06-26 05:28:36 +0000
committerAlex Denisov <1101.debian@gmail.com>2015-06-26 05:28:36 +0000
commit3849076ca69f4277bfac55479c2fc0929f5bbd9d (patch)
tree875afa6435e12b75a70d5d2cf7b929859ce94c0e /docs/ObjectiveCLiterals.rst
parentb839522da5dbe5253c9258c49ece2024960a1358 (diff)
[ObjC] Add NSValue support for objc_boxed_expressions
Patch extends ObjCBoxedExpr to accept records (structs and unions): typedef struct __attribute__((objc_boxable)) _Color { int r, g, b; } Color; Color color; NSValue *boxedColor = @(color); // [NSValue valueWithBytes:&color objCType:@encode(Color)]; git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@240761 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'docs/ObjectiveCLiterals.rst')
-rw-r--r--docs/ObjectiveCLiterals.rst62
1 files changed, 60 insertions, 2 deletions
diff --git a/docs/ObjectiveCLiterals.rst b/docs/ObjectiveCLiterals.rst
index 8907c1efc7..1b5868e7d2 100644
--- a/docs/ObjectiveCLiterals.rst
+++ b/docs/ObjectiveCLiterals.rst
@@ -119,8 +119,8 @@ Objective-C provides a new syntax for boxing C expressions:
@( <expression> )
-Expressions of scalar (numeric, enumerated, BOOL) and C string pointer
-types are supported:
+Expressions of scalar (numeric, enumerated, BOOL), C string pointer
+and some C structures (via NSValue) are supported:
.. code-block:: objc
@@ -136,6 +136,12 @@ types are supported:
NSString *path = @(getenv("PATH")); // [NSString stringWithUTF8String:(getenv("PATH"))]
NSArray *pathComponents = [path componentsSeparatedByString:@":"];
+ // NS structs
+ NSValue *center = @(view.center); // Point p = view.point;
+ // [NSValue valueWithBytes:&p objCType:@encode(Point)];
+ NSValue *frame = @(view.frame); // Rect r = view.frame;
+ // [NSValue valueWithBytes:&r objCType:@encode(Rect)];
+
Boxed Enums
-----------
@@ -218,6 +224,42 @@ character data is valid. Passing ``NULL`` as the character pointer will
raise an exception at runtime. When possible, the compiler will reject
``NULL`` character pointers used in boxed expressions.
+Boxed C Structures
+------------------
+
+Boxed expressions support construction of NSValue objects.
+It said that C structures can be used, the only requirement is:
+structure should be marked with ``objc_boxable`` attribute.
+To support older version of frameworks and/or third-party libraries
+you may need to add the attribute via ``typedef``.
+
+.. code-block:: objc
+
+ struct __attribute__((objc_boxable)) Point {
+ // ...
+ };
+
+ typedef struct __attribute__((objc_boxable)) _Size {
+ // ...
+ } Size;
+
+ typedef struct _Rect {
+ // ...
+ } Rect;
+
+ struct Point p;
+ NSValue *point = @(p); // ok
+ Size s;
+ NSValue *size = @(s); // ok
+
+ Rect r;
+ NSValue *bad_rect = @(r); // error
+
+ typedef struct __attribute__((objc_boxable)) _Rect Rect;
+
+ NSValue *good_rect = @(r); // ok
+
+
Container Literals
==================
@@ -539,6 +581,22 @@ checks. Here are examples of their use:
}
#endif
+ #if __has_attribute(objc_boxable)
+ typedef struct __attribute__((objc_boxable)) _Rect Rect;
+ #endif
+
+ #if __has_feature(objc_boxed_nsvalue_expressions)
+ CABasicAnimation animation = [CABasicAnimation animationWithKeyPath:@"position"];
+ animation.fromValue = @(layer.position);
+ animation.toValue = @(newPosition);
+ [layer addAnimation:animation forKey:@"move"];
+ #else
+ CABasicAnimation animation = [CABasicAnimation animationWithKeyPath:@"position"];
+ animation.fromValue = [NSValue valueWithCGPoint:layer.position];
+ animation.toValue = [NSValue valueWithCGPoint:newPosition];
+ [layer addAnimation:animation forKey:@"move"];
+ #endif
+
Code can use also ``__has_feature(objc_bool)`` to check for the
availability of numeric literals support. This checks for the new
``__objc_yes / __objc_no`` keywords, which enable the use of