5 * cast - routines for safer casting.
7 * Often you want to cast in a limited way, such as removing a const or
8 * switching between integer types. However, normal casts will work on
9 * almost any type, making them dangerous when the code changes.
11 * These C++-inspired macros serve two purposes: they make it clear the
12 * exact reason for the cast, and they also (with some compilers) cause
13 * errors when misused.
15 * Based on Jan Engelhardt's libHX macros: http://libhx.sourceforge.net/
17 * Author: Jan Engelhardt
18 * Maintainer: Rusty Russell <rusty@rustcorp.com.au>
19 * License: LGPL (v2.1 or any later version)
22 * // Given "test" contains "3 t's in 'test string'
23 * #include <ccan/cast/cast.h>
27 * // Find char @orig in @str, if @repl, replace them. Return number.
28 * static size_t find_chars(char *str, char orig, char repl)
30 * size_t i, count = 0;
31 * for (i = 0; str[i]; i++) {
32 * if (str[i] == orig) {
41 * // Terrible hash function.
42 * static uint64_t hash_string(const unsigned char *str)
46 * for (i = 0; str[i]; i++)
51 * int main(int argc, char *argv[])
55 * // find_chars wants a non-const string, but doesn't
56 * // need it if repl == 0.
57 * printf("%zu %c's in 'test string'\n",
58 * find_chars(cast_const(char *, "test string"),
62 * // hash_string wants an unsigned char.
63 * hash = hash_string(cast_signed(unsigned char *, argv[1]));
65 * // Need a long long to hand to printf.
66 * printf("Hash of '%s' = %llu\n", argv[1],
67 * cast_static(unsigned long long, hash));
72 int main(int argc, char *argv[])
74 /* Expect exactly one argument */
78 if (strcmp(argv[1], "depends") == 0) {
79 printf("ccan/build_assert\n");