6 * cast - routines for safer casting.
8 * Often you want to cast in a limited way, such as removing a const or
9 * switching between integer types. However, normal casts will work on
10 * almost any type, making them dangerous when the code changes.
12 * These C++-inspired macros serve two purposes: they make it clear the
13 * exact reason for the cast, and they also (with some compilers) cause
14 * errors when misused.
16 * Based on Jan Engelhardt's libHX macros: http://libhx.sourceforge.net/
18 * Author: Jan Engelhardt
19 * Maintainer: Rusty Russell <rusty@rustcorp.com.au>
20 * License: LGPL (v2.1 or any later version)
23 * // Given "test" output contains "3 t's in 'test string'"
24 * #include <ccan/cast/cast.h>
29 * // Find char @orig in @str, if @repl, replace them. Return number.
30 * static size_t find_chars(char *str, char orig, char repl)
32 * size_t i, count = 0;
33 * for (i = 0; str[i]; i++) {
34 * if (str[i] == orig) {
43 * // Terrible hash function.
44 * static uint64_t hash_string(const unsigned char *str)
48 * for (i = 0; str[i]; i++)
53 * int main(int argc, char *argv[])
58 * fprintf(stderr, "Needs argument\n"); exit(1);
60 * // find_chars wants a non-const string, but doesn't
61 * // need it if repl == 0.
62 * printf("%zu %c's in 'test string'\n",
63 * find_chars(cast_const(char *, "test string"),
67 * // hash_string wants an unsigned char.
68 * hash = hash_string(cast_signed(unsigned char *, argv[1]));
70 * // Need a long long to hand to printf.
71 * printf("Hash of '%s' = %llu\n", argv[1],
72 * cast_static(unsigned long long, hash));
77 int main(int argc, char *argv[])
79 /* Expect exactly one argument */
83 if (strcmp(argv[1], "depends") == 0) {
84 printf("ccan/build_assert\n");