]> git.ozlabs.org Git - ccan/blob - ccan/cast/_info
f5cd37fc337bf42159f3f4fa66fc846031eb6d20
[ccan] / ccan / cast / _info
1 #include "config.h"
2 #include <stdio.h>
3 #include <string.h>
4
5 /**
6  * cast - routines for safer casting.
7  *
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.
11  *
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.
15  *
16  * Based on Jan Engelhardt's libHX macros: http://libhx.sourceforge.net/
17  *
18  * Author: Jan Engelhardt
19  * Maintainer: Rusty Russell <rusty@rustcorp.com.au>
20  * License: LGPL (v2.1 or any later version)
21  *
22  * Example:
23  *      // Given "test" contains "3 t's in 'test string'
24  *      #include <ccan/cast/cast.h>
25  *      #include <stdint.h>
26  *      #include <stdio.h>
27  *
28  *      // Find char @orig in @str, if @repl, replace them.  Return number.
29  *      static size_t find_chars(char *str, char orig, char repl)
30  *      {
31  *              size_t i, count = 0;
32  *              for (i = 0; str[i]; i++) {
33  *                      if (str[i] == orig) {
34  *                              count++;
35  *                              if (repl)
36  *                                      str[i] = repl;
37  *                      }
38  *              }
39  *              return count;
40  *      }
41  *
42  *      // Terrible hash function.
43  *      static uint64_t hash_string(const unsigned char *str)
44  *      {
45  *              size_t i;
46  *              uint64_t hash = 0;
47  *              for (i = 0; str[i]; i++)
48  *                      hash += str[i];
49  *              return hash;
50  *      }
51  *
52  *      int main(int argc, char *argv[])
53  *      {
54  *              uint64_t hash;
55  *
56  *              // find_chars wants a non-const string, but doesn't
57  *              // need it if repl == 0.
58  *              printf("%zu %c's in 'test string'\n",
59  *                     find_chars(cast_const(char *, "test string"),
60  *                                argv[1][0], 0),
61  *                     argv[1][0]);
62  *
63  *              // hash_string wants an unsigned char.
64  *              hash = hash_string(cast_signed(unsigned char *, argv[1]));
65  *
66  *              // Need a long long to hand to printf.
67  *              printf("Hash of '%s' = %llu\n", argv[1],
68  *                     cast_static(unsigned long long, hash));
69  *              return 0;
70  *      }
71  *              
72  */
73 int main(int argc, char *argv[])
74 {
75         /* Expect exactly one argument */
76         if (argc != 2)
77                 return 1;
78
79         if (strcmp(argv[1], "depends") == 0) {
80                 printf("ccan/build_assert\n");
81                 return 0;
82         }
83
84         return 1;
85 }