layout: Specify the reason for not activating windows
[src/agl-compositor.git] / shared / helpers.h
1 /*
2  * Copyright © 2015 Samsung Electronics Co., Ltd
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining
5  * a copy of this software and associated documentation files (the
6  * "Software"), to deal in the Software without restriction, including
7  * without limitation the rights to use, copy, modify, merge, publish,
8  * distribute, sublicense, and/or sell copies of the Software, and to
9  * permit persons to whom the Software is furnished to do so, subject to
10  * the following conditions:
11   *
12  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
13  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
14  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
15  * NONINFRINGEMENT.  IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
16  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
17  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
18  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
19  * SOFTWARE.
20  */
21
22 #ifndef WESTON_HELPERS_H
23 #define WESTON_HELPERS_H
24
25 #ifdef  __cplusplus
26 extern "C" {
27 #endif
28
29 /**
30  * @file
31  * Simple misc helper macros.
32  */
33
34 /**
35  * Compile-time computation of number of items in a hardcoded array.
36  *
37  * @param a the array being measured.
38  * @return the number of items hardcoded into the array.
39  */
40 #ifndef ARRAY_LENGTH
41 #define ARRAY_LENGTH(a) (sizeof (a) / sizeof (a)[0])
42 #endif
43
44 /**
45  * Returns the smaller of two values.
46  *
47  * @param x the first item to compare.
48  * @param y the second item to compare.
49  * @return the value that evaluates to lesser than the other.
50  */
51 #ifndef MIN
52 #define MIN(x,y) (((x) < (y)) ? (x) : (y))
53 #endif
54
55 /**
56  * Returns the bigger of two values.
57  *
58  * @param x the first item to compare.
59  * @param y the second item to compare.
60  * @return the value that evaluates to more than the other.
61  */
62 #ifndef MAX
63 #define MAX(x,y) (((x) > (y)) ? (x) : (y))
64 #endif
65
66 /**
67  * Returns a pointer to the containing struct of a given member item.
68  *
69  * To demonstrate, the following example retrieves a pointer to
70  * `example_container` given only its `destroy_listener` member:
71  *
72  * @code
73  * struct example_container {
74  *     struct wl_listener destroy_listener;
75  *     // other members...
76  * };
77  *
78  * void example_container_destroy(struct wl_listener *listener, void *data)
79  * {
80  *     struct example_container *ctr;
81  *
82  *     ctr = wl_container_of(listener, ctr, destroy_listener);
83  *     // destroy ctr...
84  * }
85  * @endcode
86  *
87  * @param ptr A valid pointer to the contained item.
88  *
89  * @param type A pointer to the type of content that the list item
90  * stores. Type does not need be a valid pointer; a null or
91  * an uninitialised pointer will suffice.
92  *
93  * @param member The named location of ptr within the sample type.
94  *
95  * @return The container for the specified pointer.
96  */
97 #ifndef container_of
98 #define container_of(ptr, type, member) ({                              \
99         const __typeof__( ((type *)0)->member ) *__mptr = (ptr);        \
100         (type *)( (char *)__mptr - offsetof(type,member) );})
101 #endif
102
103 /**
104  * Build-time static assertion support
105  *
106  * A build-time equivalent to assert(), will generate a compilation error
107  * if the supplied condition does not evaluate true.
108  *
109  * The following example demonstrates use of static_assert to ensure that
110  * arrays which are supposed to mirror each other have a consistent
111  * size.
112  *
113  * This is only a fallback definition; support must be provided by the
114  * compiler itself.
115  *
116  * @code
117  * int small[4];
118  * long expanded[4];
119  *
120  * static_assert(ARRAY_LENGTH(small) == ARRAY_LENGTH(expanded),
121  *               "size mismatch between small and expanded arrays");
122  * for (i = 0; i < ARRAY_LENGTH(small); i++)
123  *     expanded[i] = small[4];
124  * @endcode
125  *
126  * @param condition Expression to check for truth
127  * @param msg Message to print on failure
128  */
129 #ifndef static_assert
130 # ifdef _Static_assert
131 #  define static_assert(cond, msg) _Static_assert(cond, msg)
132 # else
133 #  define static_assert(cond, msg)
134 # endif
135 #endif
136
137 #ifdef  __cplusplus
138 }
139 #endif
140
141 #endif /* WESTON_HELPERS_H */