Wed, 17 Jul 2013 15:56:01 +0200
some fixes and some documentation
1 /*
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3 *
4 * Copyright 2013 Olaf Wintermann. All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are met:
8 *
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 *
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
27 */
28 /**
29 * Main UCX Header providing most common definitions.
30 *
31 * @file ucx.h
32 * @author Mike Becker
33 * @author Olaf Wintermann
34 */
36 #ifndef UCX_H
37 #define UCX_H
39 #include <stdlib.h>
41 #ifdef __cplusplus
42 #ifndef _Bool
43 #define _Bool bool
44 #define restrict
45 #endif
46 extern "C" {
47 #endif
49 /**
50 * Generic loop statement for lists.
51 *
52 * The first argument is the type of the list and its elements (e.g. UcxList).
53 * The structure invariant of the list must be as follows:
54 * <ul>
55 * <li>a first (non-<code>NULL</code>) element</li>
56 * <li>for each element a reference to the <code>next</code> element (the
57 * variable name of the pointer MUST be <code>next</code>)</li>
58 * <li>the last element of the list MUST have the <code>next</code> pointer
59 * set to <code>NULL</code></li>
60 * </ul>
61 *
62 * The second argument is a pointer to the list. In most cases this will be the
63 * pointer to the first element of the list, but it may also be an arbitrary
64 * element of the list. The iteration will then start with that element.
65 *
66 * The third argument is the name of the iteration variable. The scope of
67 * this variable is limited to the <code>UCX_FOREACH</code> statement.
68 *
69 * @param type The type of <b>both</b> the list and the element
70 * @param list The first element of the list
71 * @param elem The variable name of the element
72 */
73 #define UCX_FOREACH(type,list,elem) \
74 for (type elem = list ; elem != NULL ; elem = elem->next)
76 /**
77 * Function pointer to a compare function.
78 *
79 * The compare function shall take three arguments: the two values that shall be
80 * compared and optional additional data.
81 * The function shall then return -1 if the first argument is less than the
82 * second argument, 1 if the first argument is greater than the second argument
83 * and 0 if both arguments are equal. If the third argument is
84 * <code>NULL</code>, it shall be ignored.
85 */
86 typedef int(*cmp_func)(void*,void*,void*);
88 /**
89 * Function pointer to a copy function.
90 *
91 * The copy function shall create a copy of the first argument and may use
92 * additional data provided by the second argument. If the second argument is
93 * <code>NULL</code>, it shall be ignored.
95 * <b>Attention:</b> if pointers returned by functions of this type may be
96 * passed to <code>free()</code> depends on the implementation of the
97 * respective <code>copy_func</code>.
98 */
99 typedef void*(*copy_func)(void*,void*);
101 /**
102 * Function pointer to a write function.
103 *
104 * The signature of the write function shall be compatible to the signature
105 * of standard <code>fwrite</code>, though it may use arbitrary data types for
106 * source and destination.
107 *
108 * The arguments shall contain (in ascending order): a pointer to the source,
109 * the length of one element, the element count and a pointer to the
110 * destination.
111 */
112 typedef size_t(*write_func)(const void*, size_t, size_t, void*);
114 /**
115 * Function pointer to a read function.
116 *
117 * The signature of the read function shall be compatible to the signature
118 * of standard <code>fread</code>, though it may use arbitrary data types for
119 * source and destination.
120 *
121 * The arguments shall contain (in ascending order): a pointer to the
122 * destination, the length of one element, the element count and a pointer to
123 * the source.
124 */
125 typedef size_t(*read_func)(void*, size_t, size_t, void*);
127 #ifdef __cplusplus
128 }
129 #endif
131 #endif /* UCX_H */