]> git.ozlabs.org Git - ccan/blobdiff - ccan/strmap/strmap.h
io: io_close_taken_fd to steal fd from conn.
[ccan] / ccan / strmap / strmap.h
index 77197507c742707a5c8ff3e3e41d36ad27d635c5..0b32e6befc0ca2974ad7304dd967e1c95fae75d3 100644 (file)
@@ -21,7 +21,7 @@ struct strmap {
 };
 
 /**
- * STRMAP_MEMBERS - declare members for a type-specific strmap.
+ * STRMAP - declare a type-specific strmap
  * @type: type for this map's values, or void * for any pointer.
  *
  * You use this to create your own typed strmap for a particular type.
@@ -29,14 +29,11 @@ struct strmap {
  * value!
  *
  * Example:
- *     struct strmap_intp {
- *             STRMAP_MEMBERS(int *);
- *     };
+ *     STRMAP(int *) int_strmap;
+ *     strmap_init(&int_strmap);
  */
-#define STRMAP_MEMBERS(type)                   \
-       struct strmap raw;                      \
-       TCON(type canary)
-
+#define STRMAP(type)                           \
+       TCON_WRAP(struct strmap, type canary)
 
 /**
  * strmap_init - initialize a string map (empty)
@@ -46,11 +43,11 @@ struct strmap {
  * need this.
  *
  * Example:
- *     struct strmap_intp map;
+ *     STRMAP(int *) map;
  *
  *     strmap_init(&map);
  */
-#define strmap_init(map) strmap_init_(&(map)->raw)
+#define strmap_init(map) strmap_init_(tcon_unwrap(map))
 
 static inline void strmap_init_(struct strmap *map)
 {
@@ -65,7 +62,7 @@ static inline void strmap_init_(struct strmap *map)
  *     if (!strmap_empty(&map))
  *             abort();
  */
-#define strmap_empty(map) strmap_empty_(&(map)->raw)
+#define strmap_empty(map) strmap_empty_(tcon_unwrap(map))
 
 static inline bool strmap_empty_(const struct strmap *map)
 {
@@ -77,7 +74,7 @@ static inline bool strmap_empty_(const struct strmap *map)
  * @map: the typed strmap to search.
  * @member: the string to search for.
  *
- * Returns the value, or NULL if it isn't in the map.
+ * Returns the value, or NULL if it isn't in the map (and sets errno = ENOENT).
  *
  * Example:
  *     int *val = strmap_get(&map, "hello");
@@ -85,7 +82,7 @@ static inline bool strmap_empty_(const struct strmap *map)
  *             printf("hello => %i\n", *val);
  */
 #define strmap_get(map, member) \
-       tcon_cast((map), canary, strmap_get_(&(map)->raw, (member)))
+       tcon_cast((map), canary, strmap_get_(tcon_unwrap(map), (member)))
 void *strmap_get_(const struct strmap *map, const char *member);
 
 /**
@@ -94,8 +91,8 @@ void *strmap_get_(const struct strmap *map, const char *member);
  * @member: the string to place in the map.
  * @v: the (non-NULL) value.
  *
- * This returns false if we run out of memory, or (more normally) if that
- * string already appears in the map.
+ * This returns false if we run out of memory (errno = ENOMEM), or
+ * (more normally) if that string already appears in the map (EEXIST).
  *
  * Note that the pointer is placed in the map, the string is not copied.  If
  * you want a copy in the map, use strdup().  Similarly for the value.
@@ -106,8 +103,8 @@ void *strmap_get_(const struct strmap *map, const char *member);
  *     if (!strmap_add(&map, "goodbye", val))
  *             printf("goodbye was already in the map\n");
  */
-#define strmap_add(map, member, value)                         \
-       strmap_add_(&tcon_check((map), canary, (value))->raw,   \
+#define strmap_add(map, member, value)                                 \
+       strmap_add_(tcon_unwrap(tcon_check((map), canary, (value))),    \
                    (member), (void *)(value))
 
 bool strmap_add_(struct strmap *map, const char *member, const void *value);
@@ -118,7 +115,9 @@ bool strmap_add_(struct strmap *map, const char *member, const void *value);
  * @member: the string to remove from the map.
  * @valuep: the value (if non-NULL)
  *
- * This returns the string which was passed to strmap_map(), or NULL.
+ * This returns the string which was passed to strmap_map(), or NULL if
+ * it was not in the map (and sets errno = ENOENT).
+ *
  * This means that if you allocated a string (eg. using strdup()), you
  * can free it here.  Similarly, the value is returned in @valuep if
  * @valuep is not NULL.
@@ -128,7 +127,7 @@ bool strmap_add_(struct strmap *map, const char *member, const void *value);
  *             printf("goodbye was not in the map?\n");
  */
 #define strmap_del(map, member, valuep)                                        \
-       strmap_del_(&tcon_check_ptr((map), canary, valuep)->raw,        \
+       strmap_del_(tcon_unwrap(tcon_check_ptr((map), canary, valuep)), \
                    (member), (void **)valuep)
 char *strmap_del_(struct strmap *map, const char *member, void **valuep);
 
@@ -141,7 +140,7 @@ char *strmap_del_(struct strmap *map, const char *member, void **valuep);
  * Example:
  *     strmap_clear(&map);
  */
-#define strmap_clear(map) strmap_clear_(&(map)->raw)
+#define strmap_clear(map) strmap_clear_(tcon_unwrap(map))
 
 void strmap_clear_(struct strmap *map);
 
@@ -154,23 +153,21 @@ void strmap_clear_(struct strmap *map);
  * @handle's prototype should be:
  *     bool @handle(const char *member, type value, typeof(arg) arg)
  *
- * If @handle returns true, the iteration will stop.
+ * If @handle returns false, the iteration will stop.
  * You should not alter the map within the @handle function!
  *
  * Example:
- *     struct strmap_intp {
- *             STRMAP_MEMBERS(int *);
- *     };
+ *     typedef STRMAP(int *) strmap_intp;
  *     static bool dump_some(const char *member, int *value, int *num)
  *     {
  *             // Only dump out num nodes.
  *             if (*(num--) == 0)
- *                     return true;
+ *                     return false;
  *             printf("%s=>%i\n", member, *value);
- *             return false;
+ *             return true;
  *     }
  *
- *     static void dump_map(const struct strmap_intp *map)
+ *     static void dump_map(const strmap_intp *map)
  *     {
  *             int max = 100;
  *             strmap_iterate(map, dump_some, &max);
@@ -179,7 +176,7 @@ void strmap_clear_(struct strmap *map);
  *     }
  */
 #define strmap_iterate(map, handle, arg)                               \
-       strmap_iterate_(&(map)->raw,                                    \
+       strmap_iterate_(tcon_unwrap(map),                               \
                        typesafe_cb_cast(bool (*)(const char *,         \
                                                  void *, void *),      \
                                         bool (*)(const char *,         \
@@ -187,8 +184,8 @@ void strmap_clear_(struct strmap *map);
                                                  __typeof__(arg)), (handle)), \
                        (arg))
 void strmap_iterate_(const struct strmap *map,
-                    bool (*handle)(const char *, void *, void *), void *data);
-
+                    bool (*handle)(const char *, void *, void *),
+                    const void *data);
 
 /**
  * strmap_prefix - return a submap matching a prefix
@@ -200,7 +197,7 @@ void strmap_iterate_(const struct strmap *map,
  * strmap_empty() on the returned pointer.
  *
  * Example:
- *     static void dump_prefix(const struct strmap_intp *map,
+ *     static void dump_prefix(const strmap_intp *map,
  *                             const char *prefix)
  *     {
  *             int max = 100;
@@ -212,10 +209,10 @@ void strmap_iterate_(const struct strmap *map,
  */
 #if HAVE_TYPEOF
 #define strmap_prefix(map, prefix) \
-       ((const __typeof__(map))strmap_prefix_(&(map)->raw, (prefix)))
+       ((const __typeof__(map))strmap_prefix_(tcon_unwrap(map), (prefix)))
 #else
 #define strmap_prefix(map, prefix) \
-       ((const void *)strmap_prefix_(&(map)->raw, (prefix)))
+       ((const void *)strmap_prefix_(tcon_unwrap(map), (prefix)))
 #endif
 
 const struct strmap *strmap_prefix_(const struct strmap *map,